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.

Use NSArray to specify otherButtonTitles?

This is a year old but the solution is pretty simple … do as @Simon suggested but do not specify a cancel button title, so: UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString delegate: self cancelButtonTitle: nil destructiveButtonTitle: nil otherButtonTitles: nil]; But after adding your normal buttons, add the cancel button, like: for( NSString *title in … Read more

iPhone – getting unique values from NSArray object

Take a look at keypaths. They are super powerful and I use them instead of NSPredicate classes most of the time. Here is how you would use them in your example… NSArray *uniqueStates; uniqueStates = [customObjects valueForKeyPath:@”@distinctUnionOfObjects.state”]; Note the use of valueForKeyPath instead of valueForKey. Here is a more detailed/contrived example… NSDictionary *arnold = [NSDictionary … Read more

Difference between NSArray and NSMutableArray

NSMutableArray (and all other classes with Mutable in the name) can be modified. So, if you create a plain NSArray, you cannot change its contents later (without recreating it). But if you create an NSMutableArray, you can change it — you’ll notice it has methods like -addObject: and -insertObject:atIndex:. See the documentation for details.

Create NSIndexSet from integer array in Swift

Swift 3 IndexSet can be created directly from an array literal using init(arrayLiteral:), like so: let indices: IndexSet = [1, 2, 3] Original answer (Swift 2.2) Similar to pbasdf’s answer, but uses forEach(_:) let array = [1,2,3,4,5,7,8,10] let indexSet = NSMutableIndexSet() array.forEach(indexSet.add) //Swift 3 //Swift 2.2: array.forEach{indexSet.addIndex($0)} print(indexSet)

Creating an array from properties of objects in another array

This will return an array containing the value of licensePlate from each item in the myCars array: NSArray *licensePlates = [myCars valueForKeyPath:@”licensePlate”] If you want only unique items (for example), you can do something like this: NSArray *licensePlates = [myCars valueForKeyPath:@”@distinctUnionOfObjects.licensePlate”]; For more possibilities, see the Collection Operators documentation in the Key-Value Coding Programming Guide.

Sorting NSArray of dictionaries by value of a key in the dictionaries

I think this will do it: brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@”brand” ascending:YES]; sortDescriptors = [NSArray arrayWithObject:brandDescriptor]; sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors]; I pulled the code from Sort Descriptor Programming Topics. Also, Key-Value Coding comes into play, in that sortedArrayUsingDescriptors: will send a valueForKey: to each element in myArray, and then use standard comparators to sort the … Read more

Getting Index of an Object from NSArray?

The index returned by indexOfObject will be the first index for an occurence of your object. Equality is tested using isEqual method. The garbage value you get is probably equal to NSNotFound. Try testing anIndex against it. The number you are looking for isn’t probably in your array : NSNumber *num=[NSNumber numberWithInteger:56]; NSInteger anIndex=[myArray indexOfObject:num]; … Read more