I have an array of dictionaries with multiple keys and objects of different types. I want to fetch only one of the object and compare with the other object. I have been trying with for each loop but not bee able to do so.
Iterate the array:
for (NSDictionary *dict in array) {
then iterate the keys in the dictionary
NSArray *keys = [dict allKeys];
for (NSString *key in keys) {
then extract the value for the key:
id obj = dict[key];
Then test the object type and compare with the other object:
if ([obj isKindOfClass:[NSString class]]) {
NSString *stringObj = (NSString *)obj;
if ([stringObj isEqualToString:otherObj]) {
NSLog(#"Object equals dictionary entry %#", key);
}
} else if ([obj isKindOfClass:[NSNumber class]]) {
NSNumber *numberObj = (NSNumber *)obj;
if ([numberObj isEqual:otherObj]) {
NSLog(#"Object equals dictionary entry %#", key);
}
} else // etc.
first get the dictionary from array
NSDictionary *dict=[yourarray objectAtIndex:index];
then get the object from dictionary
NSString *yourValue=[dict objectForKey:#"Your KeyValue"];
In case of for each loop
for (NSDictionary *dict in yourArray) {
NSString *yourValue=[dict objectForKey:#"Your KeyValue"];
NSLog(#"%#",yourValue);
}
Related
after a decent search and could not find solution (probably i missed something...):
i have an array with objects, the object is AddressCard and one if the properties is name.
so i send to my function string and with for statement looking all the matches inside my object collection array who contain AddressCArd object (bookArray) and if there is match i want to add this object to an array asnd return this array:
-(NSMutableArray *) lookup:(NSString *) name
{
NSMutableArray arr = [NSMutableArray array];
for(AddressCard *card in bookArray}
{
if([card.name rangeOfString: name].location == NSNotfound)
{
[arr addObject: card];
}
}
return arr;
}
You can do as this:
-(NSMutableArray *) lookup:(NSString *) name {
NSMutableArray *arr = [NSMutableArray array];
for(AddressCard *card in bookArray) {
//if([card.name isEqualToString:name]) {
if([[card.name capitalizedString] rangeOfString:[name capitalizedString]].location != NSNotFound)
[arr addObject:card];
}
}
return arr;
}
I'm trying to get an array of keys where the value for the corresponding key in an NSDictionary is #YES.
For EG,:
Starting Dictionary:
NSDictionary* dict = #{#"key1" : #YES, #"key2": #YES, #"key3": #NO, #"key4": #NO};
Desired Result:
NSArray* array = #[#"key1", #"key2"];
I tried doing this with NSPredicates and subpredicates but couldn't find one that did what i wanted. My "working" but ugly solution is:
NSMutableArray* array = [[NSMutableArray alloc] initWithCapacity:0];
NSDictionary* dict = #{#"key1" : #YES, #"key2": #YES, #"key3": #NO, #"key4": #NO};
for (NSString* key in [dict allKeys]) {
if ([[dict objectForKey:key] isEqualToNumber:#YES])
[array addObject:key];
}
What is a better way of doing this, possibly with NSPredicates?.
This kind of abstracts a lot of the messiness away
NSArray *results = [dict keysOfEntriesPassingTest:^(id key, id obj, BOOL *stop) {
return [obj isEqualToNumber:#YES];
}].allObjects;
NSLog(#"%#", results);
I got a dictionary with 10 arrays and each array got 20 elements. How can i access the 18th element of an each array? pointers please. Thanks. Attaching my log file
for (NSString *key in [dictionary allKeys])
{
NSArray *menuList = [dictionary objectForKey:key];
NSString *imageName = [menuList objectAtIndex:17];
NSLog(#"Image Name:%#", imageName);
}
It seems what you actually wanted was
NSString *imageName = [dictionary objectForKey:#"episodeImagePath"];
Use the following code
//iterate all values
for (NSArray *arr in yourDictionary.allValues) {
//To loop the array
for (NSString *str in arr) {
NSLog(#"Element %#", str);
}
}
i think its better to take one object class, so that we can get all 18th elements in one array
i have structured an NSMutableArray and here is an example
(
{
Account = A;
Type = Electricity;
},
{
Account = B;
Type = Water;
},
{
Account = C;
Type = Mobile;
} )
when i try to delete Account B using
[data removeObject:#"B"];
Nothing Happens
[[NSUserDefaults standardUserDefaults] synchronize];
NSArray *archivedArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self dataFilePath]];
if (archivedArray == nil) {
data = [[NSMutableArray alloc] init];
} else {
data = [[NSMutableArray alloc] initWithArray:archivedArray];
}
If you're actually using an array and not a dictionary, you need to search for the item before you can remove it:
NSUInteger index = [data indexOfObjectPassingTest:^BOOL (id obj, NSUInteger idx, BOOL *stop) {
return [[(NSDictionary *)obj objectForKey:#"Account"] isEqualToString:#"B"];
}];
if (index != NSNotFound) {
[data removeObjectAtIndex:index];
}
Alternative: try a NSMutableDictionary:
NSArray *accounts = [NSArray arrayWithObjects:#"A", #"B", #"C", nil];
NSArray *types = [NSArray arrayWithObjects:#"Electricity", #"Water", #"Mobile", nil];
NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjects:types forKeys:accounts];
[data removeObjectForKey:#"B"];
An NSArray is like a list of pointers, each pointer points to an object.
If you call:
[someArray removeObject:#"B"];
You create a new NSString object that contains the string "B". The address to this object is different from the NSString object in the array. Therefore NSArray cannot find it.
You will need to loop through the array and determine where the object is located, then you simply remove it by using removeObjectAtIndex:
i want to ask question that how can we search in a plist which is of array type and has elements of array type as well. i am searching from a plist which is of string element type and its working fine, but i am not able to search when it has array elements in the plist.
Regards!
- (BOOL)searchArray:(NSArray *)array forObject:(id)object {
if ([array containsObject:object]) {
return TRUE;
}
for (id elem in array) {
if ([elem isKindOfClass:[NSArray class]]) {
if ([self searchArray:elem forObject:object]) {
return TRUE;
}
}
}
return FALSE;
}
Will handle a two-dimensional array as well as any other depth.
You question is not very clear but if you're looking for a way to locate an object in a NSArray containing NSArrays containing objects (NSStrings), here's an example:
NSArray * l20 = [NSArray arrayWithObjects:#"One", #"Two", nil];
NSArray * l21 = [NSArray arrayWithObjects:#"Three", #"Four", nil];
NSArray * ll = [NSArray arrayWithObjects:l20, l21, nil];
for(id l1obj in ll)
for(id l2obj in l1obj)
if([l2obj isEqualToString:#"Three"])
NSLog(#"Found object three");