I have a NsMutableArray. It has format as the below:
arrayTest:(
{
away = "Test 1";
"time_start" = "20:30";
},
{
away = "Test 2";
"time_start" = "21:00";
},
{
away = #"Test 3";
"time_start" = "21:30";
}
)
Can I edit or delete the value for 'away' in this array?
Assuming here arrayTest is a NSMutableArray.
You can approach the first dictionary by [arrayTest objectAtIndex:0].
Furthermore, to get the value for keyTest 1, use [[arrayTest objectAtIndex:0] objectForKey:#"Test 1"];
Related
I have two dictionary with multiple items. I want to compare values.
Dict1:{
"Displacement_trim" = "49.26 ";
"Dry_Weight" = "<null>";
}
Dict2:{
"Displacement_trim" = "171.20 ";
"Dry_Weight" = "<null>";
}
I want to know which "Displacement_trim" is greater.
Also checking null values.
Print the data in cell.
How can I achieve this?
To find out which value from the dict is bigger use the following code:
Swift 3
let dict1 = [ "Displacement_trim" : "49.26", "Dry_Weight" : "<null>" ]
let dict2 = [ "Displacement_trim" : "171.20", "Dry_Weight" : "<null>" ]
// I force unwrapped everything for brevity
if Float(dict1["Displacement_trim"]!)! > Float(dict2["Displacement_trim"]!)! {
// highlight label for dict1
} else {
// highlight label for dict2
}
Objective-C
NSDictionary *dict1 = #{#"Displacement_trim" : #"49.26", #"Dry_Weight" : #"<null>"};
NSDictionary *dict2 = #{#"Displacement_trim" : #"171.20", #"Dry_Weight" : #"<null>"};
NSString *dict1DisplacementTrim = dict1[#"Displacement_trim"];
NSString *dict2DisplacementTrim = dict2[#"Displacement_trim"];
if (dict1DisplacementTrim.floatValue > dict2DisplacementTrim.floatValue) {
// highlight label for dict1
} else {
// highlight label for dict2
}
NSMutableDictionary *dict1,*dict2;
dict1 = [[NSMutableDictionary alloc]init];
dict2 = [[NSMutableDictionary alloc]init];
[dict1 setObject:[NSNumber numberWithFloat:13.20] forKey:#"Displacement_trim"];
[dict2 setObject:[NSNumber numberWithFloat:10.20] forKey:#"Displacement_trim"];
if ( [[dict1 valueForKey:#"Displacement_trim"] compare:[dict2 valueForKey:#"Displacement_trim"]]==NSOrderedAscending) {
NSLog(#"dict 2 is greater");
}else{
NSLog(#"dict 1 is greater");
}
Let me know if any thing get wrong.
I want to sort my main array depending on the first_name in the array self.contacts
Code that is used to sort the array
-(void)Sort
{
NSSortDescriptor *sdName = [[NSSortDescriptor alloc] initWithKey:#"first_name" ascending:YES];
self.contacts = [NSMutableArray arrayWithArray:[self.contacts sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sdName, nil]]];
}
MyData
self.contacts = (
{
contact = {
"first_name" = "Test R";
};
"last_met" = (
{
"last_met_date" = "2014-10-09";
}
);
},
{
contact = {
"first_name" = "Test K";
};
"last_met" = (
{
"last_met_date" = "2014-10-09";
}
);
}
)
Try with this sort descriptor:
descriptor = [[NSSortDescriptor alloc] initWithKey:#"contact.first_name"
ascending:YES];
The parameter key is actually a key path, and NSDictionary is KV compliant. You can write stuff like: [contacts[0] valueForKeyPath:#"contact.first_name"].
Looking on your code it looks like that you want to sort your contacts array, which is an array of dictionaries, the solution I provide here is for that case.
You can do it like this:
NSArray *sortedContactsArray = [self.contacts sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString *first_name1 = [obj1 valueForKeyPath:#"contact.first_name"];
NSString *first_name2 = [obj2 valueForKeyPath:#"contact.first_name"];
return [first_name1 compare:first_name2];
}];
Note, that it could be not safe and optimal, but that's just one of the ways to do it.
I begin in Ios dev and I got some troubles to manipulate an array create by Json :
I call in my app a web Service which return me data :
{evenements =(
({
dateEvenement ={
1 = "01-01-2013";
2 = "02-01-2013";
3 = "03-01-2013";
4 = "04-01-2013";
};
idEvenement = 61;
nbrInvite = 1;
nomEvenement = "My event Name";
nomUtilisateur = "Lucas ";
}
),
);
}
I'm able to get all the values by the following code except for "dateEvenement" :
NSArray *msgList;
msgList = [ jsonResults objectForKey:#"evenements" ];
for (NSDictionary *evenements in msgList) {
for (NSDictionary *evenement in evenements ) {
NSString *idEvenement = [evenement objectForKey:#"idEvenement"];
NSString *nomUtilisateur = [evenement objectForKey:#"nomUtilisateur"];
NSString *nomEvenement = [evenement objectForKey:#"nomEvenement"];
NSString *nbrInvite = [evenement objectForKey:#"nbrInvite"];
NSArray *dates = [ evenement objectForKey:#"dateEvenement" ];
}
}
Can you help me for getting datas of "dateEvenement"
Well in your JSON the dateEvenement isn't an Array but a dictionary:
NSDictionary *dates = [ evenement objectForKey:#"dateEvenement"];
for(NSNumber *key in dates) {
NSString *dateString = [dates objectForKey:key];
NSLog(%# : %#, key, dateString);
}
As declared in your JSON example the key's for the dictionary are numbers, thus you should NSNumber object for the key type.
{
"id": "1",
"result": [
{
"Name": "John",
"Statu": "Online"
},
{
"Name": "Alex",
"Statu": "Online"
},
{
"Name": "Diaz",
"Statu": "Offline"
}
]
}
How do i extract each "car" JSON object and put it into a native object? I tried several way but i can't do that.
NSString *responseString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
NSString *responseDict = [responseString JSONValue];
NSArray *objects = [NSArray arrayWithObjects:[responseDict valueForKeyPath:#"result.Name"],[responseDict valueForKeyPath:#"result.Statu"],nil];
NSLog(#"objects Array: %#",objects);
**==> NSLOG gives:
(
(
"John",
"Alex",
"Diaz"
),
(
"Online",
"Online",
"Offline"
)
)
NSArray *resultsArray = [responseString JSONValue];
for (NSDictionary *personDict in resultsArray)
{
NSLog(#"ihaleAdi =: %#",[carDict valueForKey:#"result.ihaleAdi"]);
NSLog(#"ihaleDurum =: %#",[carDict valueForKey:#"result.Statu"]);
}
But ıt gıves an error tooç I want to just lıst them but ı cant do thatç can anybody help me please? thank you for reading
Use an Array to capture the responseString:
NSString *responseString = [request responseString];
NSArray *array = [responseString JSONValue];
Then when you need an individual item from that array use a Dictionary:
// 0 is the index of the array you need
NSDictionary *itemDictionary = (NSDictionary *)[array objectAtIndex:0];
Given a JSON responseString that looks like this:
[{"UniqueID":111111,"DeviceName":"DeviceName1","Location":"Device1Loc","Description":"Device1Desc"},{"UniqueID":22222,"DeviceName":"DeviceName2","Location":"Device2Loc","Description":"Device2Desc"}]
You will wind up with an Array that looks like this:
myArray = (
{
Description = "Device1Desc";
DeviceName = "DeviceName1";
Location = "Device1Loc";
UniqueID = 111111;
},
{
Description = "Device2Desc";
DeviceName = "DeviceName2";
Location = "Device2Loc";
UniqueID = 222222;
}
)
And a Dictionary of index 0 that looks like this:
myDictionary = {
Description = "Device1Desc";
DeviceName = "DeviceName1";
Location = "Device1Loc";
UniqueID = 111111;
}
Sorry for any confusion and improperly instantiated object earlier. I am still a relative newbie that learned something today.
I'm trying to sort an array that would look something like this:
(please ignore the fact these people are well past any living age! I just needed large numbers)
NSDictionary *person1 = [NSDictionary dictionaryWithObjectsAndKeys:#"sam",#"name",#"28.00",#"age",nil];
NSDictionary *person2 = [NSDictionary dictionaryWithObjectsAndKeys:#"cody",#"name",#"100.00",#"age",nil];
NSDictionary *person3 = [NSDictionary dictionaryWithObjectsAndKeys:#"marvin",#"name",#"299.00",#"age",nil];
NSDictionary *person4 = [NSDictionary dictionaryWithObjectsAndKeys:#"billy",#"name",#"0.0",#"age",nil];
NSDictionary *person5 = [NSDictionary dictionaryWithObjectsAndKeys:#"tammy",#"name",#"54.00",#"age",nil];
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:person1,person2,person3,person4,person5,nil];
// before sort
NSLog(#"%#",arr);
NSSortDescriptor *ageSorter = [[NSSortDescriptor alloc] initWithKey:#"age" ascending:YES];
[arr sortUsingDescriptors:[NSArray arrayWithObject:ageSorter]];
// after sort
NSLog(#"%#",arr);
Now before sort the output would be:
2010-07-21 10:46:31.898 Sorting[70673:207] (
{
age = "28.00";
name = sam;
},
{
age = "100.00";
name = cody;
},
{
age = "299.00";
name = marvin;
},
{
age = "0.0";
name = billy;
},
{
age = "54.00";
name = tammy;
}
)
and after the sort:
2010-07-21 10:46:31.900 Sorting[70673:207] (
{
age = "0.0";
name = billy;
},
{
age = "100.00";
name = cody;
},
{
age = "28.00";
name = sam;
},
{
age = "299.00";
name = marvin;
},
{
age = "54.00";
name = tammy;
}
)
As you can see it does sort it, but from my understanding it's sorting by string. I've tried but after a few days of failure of trying to write a method that would sort this for me im still at a loss. What would be the best approach and accomplishing this so it sorts by a numeric value?
Although I question the use of strings here, the simplest way to work with that data with be:
[array sortedArrayUsingComparator:^(NSDictionary *item1, NSDictionary *item2) {
NSString *age1 = [item1 objectForKey:#"age"];
NSString *age2 = [item2 objectForKey:#"age"];
return [age1 compare:age2 options:NSNumericSearch];
}];
Or, using Objective-C's more recent subscripting features:
[array sortedArrayUsingComparator:^(NSDictionary *item1, NSDictionary *item2) {
return [item1[#"age"] compare:item2[#"age"] options:NSNumericSearch];
}];
Easiest would be storing the age as a number instead of a string.