0

I have an array that contains multiple dictionaries. In each dictionary, sometimes the key 'node_title' contains a value in which an apostrophe is returned as '.

How can I remove ' and replace it with an apostrophe for that key in every dictionary inside my array (self.beautyData)?

How I'm pulling my data to the array (ViewController.m, viewDidLoad) :

 NSMutableDictionary *viewParams15 = [NSMutableDictionary new];


 [viewParams15 setValue:@"salons_beauty_wellness" forKey:@"view_name"];
 [DIOSView viewGet:viewParams15 success:^(AFHTTPRequestOperation *operation, id responseObject) {
     
     self.beautyData = [responseObject mutableCopy];

     
 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"Failure: %@", [error localizedDescription]);
 }];
      

Example of the array returned:

2022-05-15 22:26:31.433242-0700 app[26165:11407362] SOME STORES (
        {
        latitude = "49.263";
        longitude = "-123.11";
        nid = 77854;
        "node_title" = "Jon's Barber Shop";
    },
        {
        latitude = "49.2477";
        longitude = "-123.185";
        nid = 71308;
        "node_title" = "Carla's Cupcakes";
    
    }
Arasuvel
  • 2,971
  • 1
  • 25
  • 40
Brittany
  • 1,359
  • 4
  • 24
  • 63
  • https://stackoverflow.com/questions/659602/objective-c-html-escape-unescape maybe? – Larme May 16 '22 at 09:36
  • If the source is JSON replace the occurrences of `'` with `'` in the JSON string (or even the raw data) and then deserialize the object. If not converting to (JSON) `NSData`, replacing the bytes and converting it back to array might be another way. OMG Objective-C is pretty cumbersome. – vadian May 19 '22 at 16:26

1 Answers1

1

You need to convert json to Codeable Objects and in

init(from decoder: Decoder) throws {}

you need to get value of node_title and then replace with your desired result and save again with this varible then you will get proper result

Example: struct DataObjets : Codable {

   let node_title : String?

   enum CodingKeys: String, CodingKey {
     case node_title = "node_title"
   }

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        var tmp_node_title = try values.decodeIfPresent(String.self, forKey: . node_title)
    tmp_node_title = tmp_node_title.repplace("'", "'")
     node_title =  tmp_node_title
   }
}
Chandaboy
  • 1,302
  • 5
  • 10