'Obj-c - Remove string from key in multiple dictionaries in array?
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";
}
Solution 1:[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
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | chanadboy |
