How to search an NSSet or NSArray for an object which has an specific value for an specific property?

NSPredicate *predicate = [NSPredicate predicateWithFormat:@”type == %@”, @”standard”]; NSArray *filteredArray = [myArray filteredArrayUsingPredicate:predicate]; id firstFoundObject = nil; firstFoundObject = filteredArray.count > 0 ? filteredArray.firstObject : nil; NB: The notion of the first found object in an NSSet makes no sense since the order of the objects in a set is undefined.

Non-retaining array for delegates

I found this bit of code awhile ago (can’t remember who to attribute it to). It’s quite ingenius, using a Category to allow the creation of a mutable array that does no retain/release by backing it with a CFArray with proper callbacks. @implementation NSMutableArray (WeakReferences) + (id)mutableArrayUsingWeakReferences { return [self mutableArrayUsingWeakReferencesWithCapacity:0]; } + (id)mutableArrayUsingWeakReferencesWithCapacity:(NSUInteger)capacity { … Read more

Using @[array, of, items] vs [NSArray arrayWithObjects:]

They are almost identical, but not completely. The Clang documentation on Objective-C Literals states: Array literal expressions expand to calls to +[NSArray arrayWithObjects:count:], which validates that all objects are non-nil. The variadic form, +[NSArray arrayWithObjects:] uses nil as an argument list terminator, which can lead to malformed array objects. So NSArray *myArray = @[objectOne, objectTwo, … Read more

Searching NSArray of NSDictionary objects

NSArray *contacts = …; //your array of NSDictionary objects NSPredicate *filter = [NSPredicate predicateWithFormat:@”contact_type = 42″]; NSArray *filteredContacts = [contacts filteredArrayUsingPredicate:filter]; After this, filteredContacts will contain only the contacts whose contact_type is 42. If you need to search for more than one kind of contact_type, then simply use an OR in the predicate: filter = … Read more

Flatten an NSArray

It can be done in a single line if you like key-value coding (KVC). The @unionOfArrays collection operator does exactly what you are looking for. You may have encountered KVC before in predicates, bindings and similar places, but it can also be called in normal Objective-C code like this: NSArray *flatArray = [array valueForKeyPath: @”@unionOfArrays.self”]; … Read more

How to sort NSArray of objects based on one attribute

Use NSSortDescriptor. Just search the documentation on it and there some very simple examples you can copy right over. Here is a simplified example: NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:@”MyStringVariableName” ascending:YES]; NSArray *descriptors = [NSArray arrayWithObject:valueDescriptor]; NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:descriptors]; And just like that you have a sorted array.

Convert NSArray to NSDictionary

Try this magic: NSDictionary *dict = [NSDictionary dictionaryWithObjects:records forKeys:[records valueForKey:@”intField”]]; FYI this is possible because of this built-in feature: @interface NSArray(NSKeyValueCoding) /* Return an array containing the results of invoking -valueForKey: on each of the receiver’s elements. The returned array will contain NSNull elements for each instance of -valueForKey: returning nil. */ – (id)valueForKey:(NSString *)key;