How to sort Json value in NSDictionary Objective c? - ios

I want to sort my nested Json array by location_name, My json is in nsdictionary
Json array is
apiResult:{
Description = "List of Location";
code = 200;
locationList = (
{
"location_id" = 481;
"location_name" = "<null>";
"pre_fixied" = "$3.00";
"state_name" = Melbourne;
status = 0;
zone = "Zone 2";
"zone_id" = 30;
},
{
"location_id" = 461;
"location_name" = "O'Halloran Hill";
"pre_fixied" = "$5.00";
"state_name" = Adelaide;
status = 1;
zone = "Zone 3";
"zone_id" = 31;
},
{
"location_id" = 460;
"location_name" = "Sheidow Park";
"pre_fixied" = "$5.00";
"state_name" = Adelaide;
status = 1;
zone = "Zone 3";
"zone_id" = 31;
},
{
"location_id" = 459;
"location_name" = "Hallett Cove";
"pre_fixied" = "$5.00";
"state_name" = Adelaide;
status = 1;
zone = "Zone 3";
"zone_id" = 31;
},
{
"location_id" = 458;
"location_name" = "Eden Hills";
"pre_fixied" = "$5.00";
"state_name" = Adelaide;
status = 1;
zone = "Zone 3";
"zone_id" = 31;
},
{
"location_id" = 457;
"location_name" = Glengowrie;
"pre_fixied" = "$5.00";
"state_name" = Adelaide;
status = 1;
zone = "Zone 3";
"zone_id" = 31;
}
);
message = "List of Location";
status = Success;
}
its appearing by location id descending order i want this json by location name ascending order.
I have sort location list array separately but i want whole json and locationlist by locationname order

try this
locationDescriptor = [[NSSortDescriptor alloc] initWithKey:#"location_id" ascending:YES];
sortDescriptors = [NSArray arrayWithObject: locationDescriptor];
sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];
Now change sortedArray into any other format as required.

First short Location Array.
lets say your json result is NSDictionary *jsonResult; And filtered array is NSArray *filteredArray;
Then
NSMutableDictionary *filteredJson = [NSMutableDictionary new];
[filteredJson setObject:[jsonResult valueForKey:#"Description"] forKey:#"Description"];
[filteredJson setObject:[jsonResult valueForKey:#"code"] forKey:#"code"];
[filteredJson setObject:filteredArray forKey:#"locationList"];
[filteredJson setObject:[jsonResult valueForKey:#"message"] forKey:#"message"];
[filteredJson setObject:[jsonResult valueForKey:#"status"] forKey:#"status"];

Related

Dictionary object can't convert into a JSON object objective-c

hello I have a NSMutableArray like this.
<__NSArrayM 0x137e9f270>(
{
CanLoadMore = 0;
IsFinalLevel = 1;
NextLevelApprovers = (
);
OrgStructure = (
{
CreatedBy = 1;
CreatedDate = "2015-07-29T12:10:34.297";
Deleted = 0;
DeletedBy = 0;
DeletedDate = "1901-01-01T00:00:00";
EntityHeadCode = 17098;
EntityHeadName = "<null>";
IsPermitted = 0;
LegislativeCode = LKA;
LevelId = 1;
NodeId = 1;
OrgEntity = Group;
OrgLevelDescription = "";
OrgLevelName = "ABCD Holdings";
OrgStructureId = 1;
ParentNodeId = 0;
RefOrgLevelId = 1;
Status = 1;
UpdatedBy = 17113;
UpdatedDate = "2016-04-07T08:53:13.727";
},
{
CreatedBy = 17113;
CreatedDate = "2016-04-06T12:17:19.75";
Deleted = 0;
DeletedBy = 0;
DeletedDate = "1901-01-01T00:00:00";
EntityHeadCode = 0;
EntityHeadName = "<null>";
IsPermitted = 0;
LegislativeCode = LKA;
LevelId = 2;
NodeId = 2;
OrgEntity = Company;
OrgLevelDescription = "";
OrgLevelName = "ABCD HCM";
OrgStructureId = 2;
ParentNodeId = 1;
RefOrgLevelId = 6;
Status = 1;
UpdatedBy = 17098;
UpdatedDate = "2016-07-29T11:14:12.513";
}
);
PreviousLevelCount = 0;
RequestDetails = {
AutoId = 20;
LevelId = 1;
ModuleId = 2;
NoOfLevels = 1;
ObjectId = 20;
ObjectName = "Leave Request";
RequestForm = "{\"LeaveEntryCode\":0,\"RequestId\":0,\"EmployeeCode\":17227,\"LeaveYear\":2016,\"LeaveTypeCode\":1,\"LeaveReasonCode\":2,\"BaseType\":\"ess\",\"StartDate\":\"2016-08-02T00:00:00\",\"EndDate\":\"2016-08-02T00:00:00\",\"NoOfDays\":1.0,\"StartDateSession\":\"full\",\"EndDateSession\":\"half\",\"PreApproved\":false,\"ForDate\":\"1901-01-01T00:00:00\",\"Remarks\":\"Test 1\",\"CoveringPersonCode\":0,\"AttachedDocument\":null,\"RequestStatus\":\"P\",\"Deleted\":false,\"Status\":false,\"CreatedBy\":0,\"CreatedDate\":\"0001-01-01T00:00:00\",\"UpdatedBy\":0,\"UpdatedDate\":\"0001-01-01T00:00:00\",\"DeletedBy\":0,\"DeletedDate\":\"0001-01-01T00:00:00\",\"ModuleId\":2,\"ObjectId\":20,\"StartDateString\":\"08/02/2016\",\"EndDateString\":\"08/02/2016\",\"LeaveDayList\":[\"08/02/2016-FH,08/02/2016-SH\"],\"SystemLeaveTypeCode\":\"ANN\",\"LeaveTypeName\":\"ANNUAL\",\"Employee\":null,\"LieuDayList\":null,\"BaseLeaveType\":\"ANN\",\"CoveringPersonName\":,\"LeaveReasonName\":\"Leave TypeCasual - Leave - Leave Reason\",\"DocumentSource\":\"LEAVE\"}";
RequestId = 20;
RequestedDate = "2016-08-02T05:07:07.127";
WorkflowId = 2;
};
RequesterDetails = {
AdminRequesterName = "";
DisplayName = "<null>";
EmployeeCode = 17227;
EmployeeNumber = MM0000019;
EtfNo = "";
Gender = Female;
ImagePath = "/profile/image/759006c5e4214f0";
Name = haniAAAA;
};
}
)
I want to convert this to JSON object. So I did like this.
NSError *jsonError;
NSData *objectData = [[[[dm.mutArraySelectedReq objectAtIndex:index] objectForKey:#"RequestDetails"] valueForKey:#"RequestForm"] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
but my jsondictionary is always nil. What is the error with this RequestForm object.
Please help me.
Thanks
UPDATE
This is the string I want to convert into a json.
{\"LeaveEntryCode\":0,\"RequestId\":0,\"EmployeeCode\":17227,\"LeaveYear\":2016,\"LeaveTypeCode\":1,\"LeaveReasonCode\":2,\"BaseType\":\"ess\",\"StartDate\":\"2016-08-02T00:00:00\",\"EndDate\":\"2016-08-02T00:00:00\",\"NoOfDays\":1.0,\"StartDateSession\":\"full\",\"EndDateSession\":\"half\",\"PreApproved\":false,\"ForDate\":\"1901-01-01T00:00:00\",\"Remarks\":\"Test 1\",\"CoveringPersonCode\":0,\"AttachedDocument\":null,\"RequestStatus\":\"P\",\"Deleted\":false,\"Status\":false,\"CreatedBy\":0,\"CreatedDate\":\"0001-01-01T00:00:00\",\"UpdatedBy\":0,\"UpdatedDate\":\"0001-01-01T00:00:00\",\"DeletedBy\":0,\"DeletedDate\":\"0001-01-01T00:00:00\",\"ModuleId\":2,\"ObjectId\":20,\"StartDateString\":\"08/02/2016\",\"EndDateString\":\"08/02/2016\",\"LeaveDayList\":[\"08/02/2016-FH,08/02/2016-SH\"],\"SystemLeaveTypeCode\":\"ANN\",\"LeaveTypeName\":\"ANNUAL\",\"Employee\":null,\"LieuDayList\":null,\"BaseLeaveType\":\"ANN\",\"CoveringPersonName\":,\"LeaveReasonName\":\"Leave TypeCasual - Leave - Leave Reason\",\"DocumentSource\":\"LEAVE\"}
UPDATE - CORRECT STRING
{\"LeaveEntryCode\":0,\"RequestId\":0,\"EmployeeCode\":17167,\"LeaveYear\":2016,\"LeaveTypeCode\":2,\"LeaveReasonCode\":0,\"BaseType\":\"ess\",\"StartDate\":\"2016-08-01T00:00:00\",\"EndDate\":\"2016-08-01T00:00:00\",\"NoOfDays\":1.0,\"StartDateSession\":\"full\",\"EndDateSession\":\"full\",\"PreApproved\":false,\"ForDate\":\"1901-01-01T00:00:00\",\"Remarks\":\"\",\"CoveringPersonCode\":0,\"AttachedDocument\":null,\"RequestStatus\":\"P\",\"Deleted\":false,\"Status\":false,\"CreatedBy\":0,\"CreatedDate\":\"0001-01-01T00:00:00\",\"UpdatedBy\":0,\"UpdatedDate\":\"0001-01-01T00:00:00\",\"DeletedBy\":0,\"DeletedDate\":\"0001-01-01T00:00:00\",\"ModuleId\":2,\"ObjectId\":20,\"StartDateString\":\"08/01/2016\",\"EndDateString\":\"08/01/2016\",\"LeaveDayList\":[\"08/01/2016-FH,08/01/2016-SH\"],\"SystemLeaveTypeCode\":\"CAS\",\"LeaveTypeName\":\"CASUAL\",\"Employee\":null,\"LieuDayList\":null,\"BaseLeaveType\":\"ANN\",\"CoveringPersonName\":null,\"LeaveReasonName\":\"Leave TypeCasual - Leave - Leave Reason\",\"DocumentSource\":\"LEAVE\"}
WRONG STRING
{\"LeaveEntryCode\":0,\"RequestId\":0,\"EmployeeCode\":17227,\"LeaveYear\":2016,\"LeaveTypeCode\":1,\"LeaveReasonCode\":2,\"BaseType\":\"ess\",\"StartDate\":\"2016-08-02T00:00:00\",\"EndDate\":\"2016-08-02T00:00:00\",\"NoOfDays\":1.0,\"StartDateSession\":\"full\",\"EndDateSession\":\"half\",\"PreApproved\":false,\"ForDate\":\"1901-01-01T00:00:00\",\"Remarks\":\"Test 1\",\"CoveringPersonCode\":0,\"AttachedDocument\":null,\"RequestStatus\":\"P\",\"Deleted\":false,\"Status\":false,\"CreatedBy\":0,\"CreatedDate\":\"0001-01-01T00:00:00\",\"UpdatedBy\":0,\"UpdatedDate\":\"0001-01-01T00:00:00\",\"DeletedBy\":0,\"DeletedDate\":\"0001-01-01T00:00:00\",\"ModuleId\":2,\"ObjectId\":20,\"StartDateString\":\"08/02/2016\",\"EndDateString\":\"08/02/2016\",\"LeaveDayList\":[\"08/02/2016-FH,08/02/2016-SH\"],\"SystemLeaveTypeCode\":\"ANN\",\"LeaveTypeName\":\"ANNUAL\",\"Employee\":null,\"LieuDayList\":null,\"BaseLeaveType\":\"ANN\",\"CoveringPersonName\":,\"LeaveReasonName\":\"Leave TypeCasual - Leave - Leave Reason\",\"DocumentSource\":\"LEAVE\"}
Its working Fine
NSString *str = #"{\"LeaveEntryCode\":0,\"RequestId\":0,\"EmployeeCode\":17167,\"LeaveYear\":2016,\"LeaveTypeCode\":2,\"LeaveReasonCode\":0,\"BaseType\":\"ess\",\"StartDate\":\"2016-08-01T00:00:00\",\"EndDate\":\"2016-08-01T00:00:00\",\"NoOfDays\":1.0,\"StartDateSession\":\"full\",\"EndDateSession\":\"full\",\"PreApproved\":false,\"ForDate\":\"1901-01-01T00:00:00\",\"Remarks\":\"\",\"CoveringPersonCode\":0,\"AttachedDocument\":null,\"RequestStatus\":\"P\",\"Deleted\":false,\"Status\":false,\"CreatedBy\":0,\"CreatedDate\":\"0001-01-01T00:00:00\",\"UpdatedBy\":0,\"UpdatedDate\":\"0001-01-01T00:00:00\",\"DeletedBy\":0,\"DeletedDate\":\"0001-01-01T00:00:00\",\"ModuleId\":2,\"ObjectId\":20,\"StartDateString\":\"08/01/2016\",\"EndDateString\":\"08/01/2016\",\"LeaveDayList\":[\"08/01/2016-FH,08/01/2016-SH\"],\"SystemLeaveTypeCode\":\"CAS\",\"LeaveTypeName\":\"CASUAL\",\"Employee\":null,\"LieuDayList\":null,\"BaseLeaveType\":\"ANN\",\"CoveringPersonName\":null,\"LeaveReasonName\":\"Leave TypeCasual - Leave - Leave Reason\",\"DocumentSource\":\"LEAVE\"}" ;
NSError *jsonError;
NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&jsonError];
**OutPut**
Printing description of json:
{
AttachedDocument = "<null>";
BaseLeaveType = ANN;
BaseType = ess;
CoveringPersonCode = 0;
CoveringPersonName = "<null>";
CreatedBy = 0;
CreatedDate = "0001-01-01T00:00:00";
Deleted = 0;
DeletedBy = 0;
DeletedDate = "0001-01-01T00:00:00";
DocumentSource = LEAVE;
Employee = "<null>";
EmployeeCode = 17167;
EndDate = "2016-08-01T00:00:00";
EndDateSession = full;
EndDateString = "08/01/2016";
ForDate = "1901-01-01T00:00:00";
LeaveDayList = (
"08/01/2016-FH,08/01/2016-SH"
);
LeaveEntryCode = 0;
LeaveReasonCode = 0;
LeaveReasonName = "Leave TypeCasual - Leave - Leave Reason";
LeaveTypeCode = 2;
LeaveTypeName = CASUAL;
LeaveYear = 2016;
LieuDayList = "<null>";
ModuleId = 2;
NoOfDays = 1;
ObjectId = 20;
PreApproved = 0;
Remarks = "";
RequestId = 0;
RequestStatus = P;
StartDate = "2016-08-01T00:00:00";
StartDateSession = full;
StartDateString = "08/01/2016";
Status = 0;
SystemLeaveTypeCode = CAS;
UpdatedBy = 0;
UpdatedDate = "0001-01-01T00:00:00";
}
Try
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
options:0
error:&jsonError];
Don't give any options.

Retrieve a data from json ios

This is the content of dictionary *data
Data = {
Specialities = (
{
ApprovalStatus = Unapproved;
CurrencyCode = "<null>";
Packages = "<null>";
Photos = "<null>";
ServiceDescription = 356563;
Speciality = 22;
SpecialityName = "Wedding Photographer";
UserFRPs = "<null>";
Videos = (
{
VideoId = TWhSjpsGvPQ;
VideoLink = "http://www.youtube.com/watch?v=TWhSjpsGvPQ";
VideoType = YouTube;
},
{
VideoId = N2CJrhHEydA;
VideoLink = "http://www.youtube.com/watch?v=N2CJrhHEydA";
VideoType = YouTube;
},
{
VideoId = Lq6faQVYcwY;
VideoLink = "http://www.youtube.com/watch?v=Lq6faQVYcwY";
VideoType = YouTube;
},
{
VideoId = v8WjMiodcKo;
VideoLink = "http://www.youtube.com/watch?v=v8WjMiodcKo";
VideoType = YouTube;
}
);
},
{
ApprovalStatus = Unapproved;
CurrencyCode = "<null>";
Packages = "<null>";
Photos = "<null>";
ServiceDescription = 52454353;
Speciality = 37;
SpecialityName = "Hair and Makeup Stylist";
UserFRPs = "<null>";
Videos = "<null>";
},
{
ApprovalStatus = Unapproved;
CurrencyCode = "<null>";
Packages = "<null>";
Photos = "<null>";
ServiceDescription = 21212;
Speciality = 55;
SpecialityName = Transport;
UserFRPs = "<null>";
Videos = "<null>";
}
);
};
}
This is the response i am getting from json, what logic should i write to retrieve "VideoId" linked to a specific "Speciality" (for ex- Speciality = 22)
if i write following code, i am getting all #"VideoId"
[[[[responseDictionary valueForKey:#"Data"]valueForKey:#"Specialities"]valueForKey:#"Videos"]valueForKey:#"VideoId"]
The parsing way will be something like:
NSDictionary *dataDict = [responseDictionary valueForKey:#"Data"];
NSMutableArray *specialityArray = [dataDict valueForKey:#"Specialities"];
NSDictionary *objectDict = [specialityArray objectAtIndex:your index (0/1/2)];
int *speciality = [[objectDict ValueForKey:#"Speciality"] intValue];
if(speciality = 22)
{
NSMutableArray *videoArray = [objectDict valueForKey:#"Videos"];
NSDictionary *videoObject = [videoArray objectAtIndex:your index (0/1/2)];
NSString *videoID = [videoObject valueForKey:#"VideoId"];
}
Parse the rest accordingly as per your need.
Use this code,
NSArray *wholeArray = [[responseDictionary objectForKey:#"Data"] valueForKey:#"Specialities"];
NSArray *subArray = [[wholeArray objectAtIndex:0] valueForKey:#"Videos"];
subArray is get all the videos values, then you get single value use this code,
NSString *videoID = [[subArray objectAtIndex:i] valueForKey:#"VideoId"];
or you will get all videoID in single variable use this below code,
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (int i = 0; i < subArray.count; i++) {
[tempArray addObject:[[subArray objectAtIndex:i] valueForKey:#"VideoId"]];
}
finally tempArray get All the VideoID values, anywhere you use,
NSString *videoID = [tempArray objectAtIndex:0/1/2];
hope its helpful

Sort Descriptor not working in ios

i used the Sort Descriptor to Sort the NSMutableArray By one & multiple Values,First i tried by to sort By Price,it sort in some other Order here is my code help me,
My
i create the Dictionary by below code and added to the NSMutableArray
for(int i=0;i<[priceArray count];i++)
{
cellDict=[[NSMutableDictionary alloc]init];
[cellDict setObject:nameArray[i] forKey:#"Name"];
[cellDict setObject:splPriceArray[i] forKey:#"Percentage"];
[cellDict setObject:priceArray[i] forKey:#"Price"];
[resultArray addObject:cellDict];
}
// To Sort in Ascending Order
NSSortDescriptor *sort =[[NSSortDescriptor alloc] initWithKey:#"Price" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObjects:sort, nil];
NSArray *sortedArray=[resultArray sortedArrayUsingDescriptors:descriptors];
NSLog(#"Result %# Sorted arr %#",resultArray, sortedArray);
And Output is:
Result (
{
Name = "Black Eyed Peas";
Percentage = 0;
Price = 80;
},
{
Name = "Black Gram";
Percentage = 0;
Price = 56;
},
{
Name = "Channa White";
Percentage = 0;
Price = 100;
},
{
Name = "Double Beans";
Percentage = 0;
Price = 95;
},
{
Name = "Gram Dall";
Percentage = 0;
Price = 100;
},
{
Name = "Green Moong Dal";
Percentage = 0;
Price = 150;
},
{
Name = "Ground Nut";
Percentage = 0;
Price = 140;
},
{
Name = "Moong Dal";
Percentage = 0;
Price = 75;
},
{
Name = "Orid Dal";
Percentage = 0;
Price = 100;
},
{
Name = "Toor Dal";
Percentage = 0;
Price = 150;
}
) Sorted arr (
{
Name = "Channa White";
Percentage = 0;
Price = 100;
},
{
Name = "Gram Dall";
Percentage = 0;
Price = 100;
},
{
Name = "Orid Dal";
Percentage = 0;
Price = 100;
},
{
Name = "Ground Nut";
Percentage = 0;
Price = 140;
},
{
Name = "Green Moong Dal";
Percentage = 0;
Price = 150;
},
{
Name = "Toor Dal";
Percentage = 0;
Price = 150;
},
{
Name = "Black Gram";
Percentage = 0;
Price = 56;
},
{
Name = "Moong Dal";
Percentage = 0;
Price = 75;
},
{
Name = "Black Eyed Peas";
Percentage = 0;
Price = 80;
},
{
Name = "Double Beans";
Percentage = 0;
Price = 95;
}
)
Here The Sorted Array Sorting in some other Order I want to sort this in Ascending order by price.
It's unclear what your test data looks like - but the following snippet works as expected
NSArray *priceArray = [NSArray arrayWithObjects:#(74),#(100),#(100),#(130), nil];
NSArray *nameArray = [NSArray arrayWithObjects:#"Yva",#"Hallo", #"Adam", #"Xavier", nil];
NSMutableArray *resultArray = [NSMutableArray new];
for(int i=0;i<[priceArray count];i++)
{
NSMutableDictionary *cellDict=[[NSMutableDictionary alloc]init];
[cellDict setObject:nameArray[i] forKey:#"Name"];
[cellDict setObject:priceArray[i] forKey:#"Percentage"];
[cellDict setObject:priceArray[i] forKey:#"Price"];
[resultArray addObject:cellDict];
}
// Sort by Name
//NSSortDescriptor *sort =[[NSSortDescriptor alloc] initWithKey:#"Price" ascending:YES];
// Sort by Name
NSSortDescriptor *sort =[[NSSortDescriptor alloc] initWithKey:#"Name" ascending:YES selector:#selector(localizedCaseInsensitiveCompare:)];
NSArray *descriptors = [NSArray arrayWithObjects:sort, nil];
NSArray *sortedArray=[resultArray sortedArrayUsingDescriptors:descriptors];
NSLog(#"Result %# Sorted arr %#",resultArray, sortedArray);
Result:
2015-07-11 12:54:54.358 ret[10480:162783] Result (
{
Name = Yva;
Percentage = 74;
Price = 74;
},
{
Name = Hallo;
Percentage = 100;
Price = 100;
},
{
Name = Adam;
Percentage = 100;
Price = 100;
},
{
Name = Xavier;
Percentage = 130;
Price = 130;
}
) Sorted arr (
{
Name = Adam;
Percentage = 100;
Price = 100;
},
{
Name = Hallo;
Percentage = 100;
Price = 100;
},
{
Name = Xavier;
Percentage = 130;
Price = 130;
},
{
Name = Yva;
Percentage = 74;
Price = 74;
}
)

Issues parsing a dictionary

Below is a dictionary I have created by retrieving the my twitter statuses for an App Only twitter feed. I have parsed and returned the value of "text" successfully, and displayed it in a table view cell as follows:
static NSString *cellID = #"FeedCell3" ;
FeedCell3 *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[FeedCell3 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
NSInteger idx = indexPath.row;
NSDictionary *t = self.twitterDataSource[idx];
cell.updateLabel.text = t[#"text"];
return cell;
However, when I try to return other values for keys such as "profile_image_url" and "screen_name" using the method below, an (null) is returned, even though there is a value in the dictionary. What am I doing wrong?
cell.nameLabel.text = t[#"screen_name"];
NSURL *profileImageURL = t[#"profile_image_url"];
NSLog(#"%#", profileImageURL);
Dictionary Data:
contributors = "<null>";
coordinates = "<null>";
"created_at" = "Thu May 01 20:15:32 +0000 2014";
entities = {
hashtags = (
);
symbols = (
);
urls = (
);
"user_mentions" = (
);
};
"favorite_count" = 0;
favorited = 0;
geo = "<null>";
id = 461962182734217473;
"id_str" = 46398745473;
"in_reply_to_screen_name" = "<null>";
"in_reply_to_status_id" = "<null>";
"in_reply_to_status_id_str" = "<null>";
"in_reply_to_user_id" = "<null>";
"in_reply_to_user_id_str" = "<null>";
lang = fr;
place = "<null>";
"retweet_count" = 0;
retweeted = 0;
source = web;
text = "Test Tweet";
truncated = 0;
user = {
"contributors_enabled" = 0;
"created_at" = "Sun Mar 23 21:18:10 +0000 2014";
"default_profile" = 1;
"default_profile_image" = 1;
description = "";
entities = {
description = {
urls = (
);
};
};
"favourites_count" = 0;
"follow_request_sent" = "<null>";
"followers_count" = 1;
following = "<null>";
"friends_count" = 6;
"geo_enabled" = 0;
id = 24072342084;
"id_str" = 242344084;
"is_translation_enabled" = 0;
"is_translator" = 0;
lang = en;
"listed_count" = 0;
location = "";
name = "My Name";
notifications = "<null>";
"profile_background_color" = C0DEED;
"profile_background_image_url" = "http://abs.twimg.com/images/themes/theme1/bg.png";
"profile_background_image_url_https" = "https://abs.twimg.com/images/themes/theme1/bg.png";
"profile_background_tile" = 0;
"profile_image_url" = "http://abs.twimg.com/sticky/default_profile_images/default_profile_1_normal.png";
"profile_image_url_https" = "https://abs.twimg.com/sticky/default_profile_images/default_profile_1_normal.png";
"profile_link_color" = 0084B4;
"profile_sidebar_border_color" = C0DEED;
"profile_sidebar_fill_color" = DDEEF6;
"profile_text_color" = 333333;
"profile_use_background_image" = 1;
protected = 0;
"screen_name" = ScreenName;
"statuses_count" = 2;
"time_zone" = "<null>";
url = "<null>";
"utc_offset" = "<null>";
verified = 0;
};
Those latter two keys you're going for are in the user dictionary, so try
t[#"user"][#"screen_name"];
t[#"user"][#"profile_image_url"];
The second issue will appear when you solve the first. The url is a string. To get a proper NSURL you'll need to use an NSURL class method.
NSURL *aURL = [NSURL URLWithString:t[#"user"][#"profile_image_url"]];

how to find location of Id in NSMutableArray

I have NSMutableArray data as below.
(
{
Id = "-1";
NameEn = Country;
},
{
Id = 14;
NameEn = Iran;
},
{
Id = 11;
NameEn = Jordan;
},
{
Id = 5;
NameEn = "United Arab Emirates";
},
{
Id = 4;
NameEn = "Suadi Arabia";
},
{
Id = 3;
NameEn = Kuwait;
},
{
Id = 10;
NameEn = Yemen;
},
{
Id = 6;
NameEn = Oman;
},
{
Id = 12;
NameEn = Syria;
},
{
Id = 7;
NameEn = Qatar;
},
{
Id = 13;
NameEn = Lebanon;
},
{
Id = 1;
NameEn = Egypt;
},
{
Id = 8;
NameEn = "Bahrain Kingdom";
}
)
I want to find the location where Id=5.
Any idea how can I do?
I tried with below.
NSString *myCountry = [[NSUserDefaults standardUserDefaults] valueForKey:#"mCountryId"];
NSUInteger indexOfTheObject = [feedsCountry indexOfObject: myCountry];
NSLog(#"indexOfTheObject===%i==%#", indexOfTheObject, myCountry);
if (NSNotFound == indexOfTheObject) {
NSLog(#"not found...");
}
But I get output as not found... for mCountryId as 5.
Use an NSDictionary instead, and key your entries by Id; e.g.:
NSMutableDictionary* dictionary = [NSMutableDictionary new];
[dictionary setObject:#"Egypt" forKey:#"1"];
// (etc...)
EDIT: If you already have an array and can not change that, use a for loop like this:
for(NSDictionary* entry in givenArray){
NSString* key = [entry objectForKey:#"Id"];
NSString* value = [entry objectForKey:#"NameEn"];
[dictionary setObject:value forKey:key];
}
NSPredicate * filter = [NSPredicate predicateWithFormat:#"Id = 5"];
NSArray * filtered = [array filteredArrayUsingPredicate:filter];
The first object in filtered is the dictionary with Id = 5

Resources