objective c how to get the specific json key value in ios - ios

I have json response for all over country related json. My problem is I want to get the Algeria.png based on id key name and with out using icon name key. Anybody please guide me. And I want to get the image name dynamically not statically. I am using objective c code.
json for country array is:(
{
"dial_code" = "+93";
"icon_name" = "Afghanistan.png";
id = 1;
name = Afghanistan;
},
{
"dial_code" = "+355";
"icon_name" = "Albania.png";
id = 2;
name = Albania;
},
{
"dial_code" = "+213";
"icon_name" = "Algeria.png";
id = 3;
name = Algeria;
},
{
"dial_code" = "+1684";
"icon_name" = "American-Samoa.png";
id = 4;
name = "American Samoa";
},
{
"dial_code" = "+376";
"icon_name" = "Andorra.png";
id = 5;
name = Andorra;
},
{
"dial_code" = "+244";
"icon_name" = "Angola.png";
id = 6;
name = Angola;
},

You had array(this is JsonArray) of dictionaries. So
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"id == %#",[NSNumber numberWithInteger:3]];
(OR)
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"id == %#",3];
NSArray *tempArray = [JsonArray filteredArrayUsingPredicate:predicate];
In tempArray you will get dictionary object.
NSDcitionary *dict = [tempArray objectAtIndex:0];
NSString *imageName = [dict valueForKey:#"icon_name"];

Related

Filtering the NS array and getting the element according to the issynced value

I'm working in objective c. Here i need the array element which contains IsSynced = 0 in my NSlog. I need to filter the resultArry.
{
ActivityTime = "2018-07-24T05:48:25.017Z";
EntityId = "B88476DA-3707-4B37-A1E0-142875CFAE6E";
ErrorCode = "<null>";
ErrorData = "<null>";
ErrorMessage = "<null>";
IsSynced = 0;
Message = "Waybill 'QBZZ222220180724111804' created.";
PropertyId = "8c0ad0da-4505-40d5-8201-a7818a3d055a";
PropertyPIC = QBZZ2222;
RecentActivityId = "79A6E4D7-2D60-4635-9A70-C4E55D5A1399";
TableNames = NVD;
},
{
ActivityTime = "2018-07-23T11:31:25.905Z";
EntityId = "23AE3D5E-8423-428F-9678-7BB3A75EF320";
ErrorCode = "<null>";
ErrorData = "<null>";
ErrorMessage = "<null>";
IsSynced = 1;
Message = "Waybill 'QBZZ222220180723170035' created.";
PropertyId = "8c0ad0da-4505-40d5-8201-a7818a3d055a";
PropertyPIC = QBZZ2222;
RecentActivityId = "2C3C7047-F43C-4521-8B66-419AF31EC766";
TableNames = NVD;
},
{
ActivityTime = "2018-07-23T09:44:32.483Z";
EntityId = "49914A90-8F05-4479-B2C8-49C34637E70B";
ErrorCode = "<null>";
ErrorData = "<null>";
ErrorMessage = "<null>";
IsSynced = 1;
Message = "Waybill 'QBZZ222220180723151237' created.";
PropertyId = "8c0ad0da-4505-40d5-8201-a7818a3d055a";
PropertyPIC = QBZZ2222;
RecentActivityId = "DCCF5B03-7AD3-404F-A235-006DF3A37F97";
TableNames = NVD;
},
This is my code
SArray *resultArry = [[DBHelper getSharedInstance] getRecordsBySQL:propertyQuery];
//NSString *recentactivitylog=[[NSString alloc] initWithData:resultArry encoding:NSUTF8StringEncoding];
NSLog(#"This is the eNVDS in Recent Activity:%#" ,resultArry);
NSPredicate *notsynced = [NSPredicate predicateWithFormat:
#"resultArry.IsSynced = 1"];
NSArray *notsyncedenvds = [resultArry filteredArrayUsingPredicate:notsynced];
NSLog(#"This is the eNVDS in Recent Activity which is not synced:%#" ,notsyncedenvds);
I just need the array element which has the value IsSynced as 0 in my NSlog as you can see this element below.
{
ActivityTime = "2018-07-24T05:48:25.017Z";
EntityId = "B88476DA-3707-4B37-A1E0-142875CFAE6E";
ErrorCode = "<null>";
ErrorData = "<null>";
ErrorMessage = "<null>";
IsSynced = 0;
Message = "Waybill 'QBZZ222220180724111804' created.";
PropertyId = "8c0ad0da-4505-40d5-8201-a7818a3d055a";
PropertyPIC = QBZZ2222;
RecentActivityId = "79A6E4D7-2D60-4635-9A70-C4E55D5A1399";
TableNames = NVD;
}
I don't know what is wrong with my code I'm not getting any element after filtering the predicate. Sorry I'm new to Objective-c.
NSPredicate *notsynced = [NSPredicate predicateWithFormat:
#"IsSynced == 0 || IsSynced == %#",#"0"];
NSArray *notsyncedenvds = [resultArry filteredArrayUsingPredicate:notsynced];
NSLog(#"This is the eNVDS in Recent Activity which is not synced:%#" ,notsyncedenvds);
or
for fast enumeration with large amount of data
NSMutableArray *arr = [[NSMutableAraay alloc]init];
for(NSDictionary *dict in resultArry){
NSString *IsSynced = [NSStrinf StringWithFormat:#"%#",[dict objectforkey:#"IsSynced"]];
if([IsSynced integerValue] = 0){
[arr addObject:dict];
}
}
NSLog(#"IsSynced zero value array %#",arr);
The predicate is wrong. You must not include the array itself
NSPredicate *notsynced = [NSPredicate predicateWithFormat: #"IsSynced = 1"];
But as you need the item(s) with IsSynced = 0 then you have to filter fo that:
NSPredicate *notsynced = [NSPredicate predicateWithFormat: #"IsSynced = 0"];
In case the value for key IsSynced is a string you have to add single quotes
NSPredicate *notsynced = [NSPredicate predicateWithFormat: #"IsSynced = '0'"];

Group element in NSMutableArray which contains object

I have an NSMutableArray that contains an object of a class model in each position like this.
The class model contains 2 types of information, which we will call id and name.
So, in every location of my NSMutableArray I have an object that contains 2 information.
Then, in the first position of my NSMutableArray I have
{
id = 1;
name = "Dan"; //this is the first object in NSMutableArray
}
In the second position of NSMutableArray, I have:
{
id = 1;
name = "Luca";
}
In the third position
{
id = 2;
name = "Tom";
}
and so on..
Ok, my goal is to make the union of identical IDs between the various objects within the SNMutableArray but it's too difficult!
For example, if I have:
{
id = 1;
name = "Tom";
}
{
id = 1;
name = "Luca";
}
{
id = 2;
name = "Steve";
}
{
id = 2;
name = "Jhon";
}
{
id = 3;
name = "Andrew";
}
The goal is:
{
id = 1;
name = "Tom";
name = "Luca";
}
{
id = 2;
name = "Steve";
name = "Jhon";
}
{
id = 3;
name = "Andrew";
}
Any ideas? would like to use this in the cellForRowAtIndexPath method and I tried to write this: (cm is my class model and myArray is the NSMutableArray which contains an object of cm class)
ClassModel *cm = [myArray objectAtIndex:indexPath.row];
NSMutableArray * resultArray = [NSMutableArray new];
NSArray * groups = [array valueForKeyPath:cm.ID];
for (NSString * groupId in groups)
{
     NSMutableDictionary * entry = [NSMutableDictionary new];
     [insert setObject: groupId forKey: # "groupId"];
     NSArray * groupNames = [array filteredArrayUsingPredicate: [NSPredicate predicateWithFormat: # "groupId =% #", groupId]];
     for (int i = 0; i <groupNames.count; i ++)
     {
         NSString * name = [[groupNames objectAtIndex: i] objectForKey: # "name"];
         [entry setObject: name forKey: [NSS string stringWithFormat: # "name% d", i + 1]];
     }
     [resultArray addObject: entry];
}
NSLog (# "% #", resultArray);
But this does not work..maybe because each element in my array is an object?? .. Help!
You have the right basic idea, but you shouldn't try and do this in cellForRowAt. Rather, you need to create a new array that has the data in the required structure and use that array as the source for your tableview. You will also need to create a new class to put in the array; one that has an id and an NSMutableArray for the names (I won't show this but I will call it GroupClassModel)
Use something like:
NSMutableDictionary *groups = [NSMutableDictionary new]
for (ClassModel *cm in array) {
GroupClassModel *gcm = groups[cm.id];
if (gcm == nil) {
gcm = [GroupClassModel new];
gcm.id = cm.id
groups[cm.id] = gcm
}
[gcm.names addObject:cm.name];
}
NSArray *groupedName = [groups allValues];
// Finally, sort groupedName by id if that is required.

NSDictionary get property within list of objects - Objective C

How would I get "Dog" from the following dictionary?
{
Id = "123";
Animal = [{
Id = "456";
Type = "Dog";
Sound = "Bark";
},
{
Id = "789";
Type = "Cat";
Sound = "Meow";
}]
}
I tried
NSString *firstAnimalType = dictionary[#"Animal"][#"Type"];
However since there are multiple animals, it can't recognize what I am trying to find. How would I get the first animal out of the list so that I can access its type? Thanks!
You can use some thing like this, first get animal object and if it exists then find its type from it
NSDictionary *firstAnimal = [dictionary[#"Animal"] firstObject];
if(firstAnimal) //in case your firstAnimal is Empty or it may be nil then may create any unwanted issues in further code so just check it first.
{
NSString *firstAnimalType = firstAnimal[#"Type"];
}
Enumeration method will help and you can stop when and where you want
[myDict[#"Animal"] enumerateObjectsUsingBlock:^(id _Nonnull objList, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(#"%#",objList[#"Type"]);
*stop = YES; //You can stop where you want
}];
You can simply access the first element of the array with castings and get its Type value.
NSString *firstAnimalType = ((NSDictionary *)[((NSArray *)dictionary[#"Animal"]) objectAtIndex: 0])[#"Type"];
for (NSDictionary *animal in dictionary[#"Animal"]) {
NSString *type = animal[#"Type"];
if ([type isKindOfClass:[NSString class]] && [type isEqualToString:#"Dog"]) {
// Dog found
}
}
Here you go:
NSArray *aryFinalAni = [dicMain valueForKey:#"Animal"];
NSArray *aryType = [aryFinalAni valueForKeyPath:#"Type"];
if([aryType containsObject:#"Dog"])
{
int indexOfDog = (int)[aryType indexOfObject:#"Dog"];
NSMutableDictionary *dicDog = [aryFinalAni objectAtIndex:indexOfDog];
NSLog(#"%#",dicDog);
}
else
{
NSLog(#"There is no Dog found.");
}
Try this code:
{
Id = "123";
Animal = [{
Id = "456";
Type = "Dog";
Sound = "Bark";
},
{
Id = "789";
Type = "Cat";
Sound = "Meow";
}]
}
1> NSArray *items = dictionary["Animal"];
2>
NSPredicate *predicate1 = [NSPredicate predicateWithFormat: #"Type CONTAINS[cd] %#", "Dog"];
NSArray *arrData = [items filteredArrayUsingPredicate:predicate1];
if arrData.count > 0 {
dictionary = [arrData objectAtIndex:0];
}
Result:
{
Id = "456";
Type = "Dog";
Sound = "Bark";
}

Adding Multiple NSArrays to NSMutableDictionary with corresponding matching item in array order

I have two arrays say itemName and itemImages
NSArray *itemName = #[#"Blog", #"Missions", #"Subscriptions", #"Questions", #"Aide",#"Disconnect"];
NSArray *itemImages = #[#"ico-1",#"ico-2", #"ico-3", #"ico-4", #"ico-5", #"ico-6"];
I need to add them to a dictionary
NSMutableDictionary *menuItemNameImage = [[NSMutableDictionary alloc] init];
[menuItemNameImage setValue:itemName forKey:#"itemName"];
[menuItemNameImage setValue:itemImages forKey:#"itemImages"];
NSLog(#"%#", menuItemNameImage);
But this makes the following Dictionary
{
itemImages = (
"ico-1",
"ico-2",
"ico-3",
"ico-4",
"ico-5",
"ico-6"
);
itemName = (
Blog,
Missions,
Subscriptions,
Questions,
Aide,
Disconnect
);
}
What I rather want is some think like this :
{
{
itemImages = "icon-1",
itemName = Blog
},
{
itemImage = "icon-2",
itemName = "Missions"
}, ...
}
You cannot get that structure with a single NSDictionary - you need an NSArray of NSDictionary objects.
Here is how you can do it:
NSMutableArray *menuItemNameImage = [NSMutableArray array];
for (int i = 0 ; i != itemNames.count ; i++) {
[menuItemNameImage addObject: #{
#"itemName" : itemNames[i]
, #"itemImage" : itemImages[i]
}];
}
What you could use is a NSArray of NSDictionaries. Each dictionary would have two entries: an itemImage and itemName.
It would be something like this:
NSArray *itemName = #[#"Blog", #"Missions", #"Subscriptions", #"Questions", #"Aide",#"Disconnect"];
NSArray *itemImages = #[#"ico-1",#"ico-2", #"ico-3", #"ico-4", #"ico-5", #"ico-6"];
NSMutableArray *menuItemNameImage = [NSMutableArray new];
for (NSInteger i = 0; i < itemName.count; i++) {
NSMutableDictionary *itemNameImage = [NSMutableDictionary new];
itemNameImage[#"itemName"] = itemName[i];
//just a precaution if the array os images is smaller than the ones of names, to avoid unwanted crashes
if (i < itemImages.count) {
itemNameImage[#"itemImage"] = itemImages[i];
}
[menuItemNameImage addObject:itemNameImage];
}
NSLog(#"%#", menuItemNameImage);
The output, as you want:
(
{
itemImage = "ico-1";
itemName = Blog;
},
{
itemImage = "ico-2";
itemName = Missions;
},
{
itemImage = "ico-3";
itemName = Subscriptions;
},
{
itemImage = "ico-4";
itemName = Questions;
},
{
itemImage = "ico-5";
itemName = Aide;
},
{
itemImage = "ico-6";
itemName = Disconnect;
}
)
You can access the elements like this, for example:
NSString *firstItemName = menuItemNameImage[0][#"itemName"];
NSString *firstItemImage = menuItemNameImage[0][#"itemImage"];

NSDictionary: Find a specific object and remove it

I have NSMutableDictionary that looks like this:
category = (
{
description = (
{
id = 1;
name = Apple;
},
{
id = 5;
name = Pear;
},
{
id = 12;
name = Orange;
}
);
id = 2;
name = Fruits;
},
{
description = (
{
id = 4;
name = Milk;
},
{
id = 7;
name = Tea;
}
);
id = 5;
name = Drinks;
}
);
Now, when a user performs an action in my application, I get the #"name"-value of the object, like "Apple". I would like to remove that object from the dictionary, but how will can I reach this object with the
[myDictionary removeObjectForKey:]
method?
At a high level you need to get a reference to the "description" array. Then iterate through the array getting each dictionary. Check the dictionary to see if it has the matching "name" value. If so, remove that dictionary from the "description" array.
NSString *name = ... // the name to find and remove
NSMutableArray *description = ... // the description array to search
for (NSUInteger i = 0; i < description.count; i++) {
NSDictionary *data = description[i];
if ([data[#"name"] isEqualToString:name]) {
[description removeObjectAtIndex:i];
break;
}
}
To remove something from an INNER array:
NSString* someFood = <search argument>;
NSArray* categories = [myDictionary objectForKey:#"category"];
for (NSDictionary* category in categories) {
NSArray* descriptions = [category objectForKey:#"description"];
for (int i = descriptions.count-1; i >= 0; i--) {
NSDictionary* description = [descriptions objectForIndex:i];
if ([[description objectForKey:#"name"] isEqualToString:someFood]) {
[descriptions removeObjectAtIndex:i];
}
}
}
To remove an entire group (eg, "Fruits") from the outer array is simpler.

Resources