Search NSArray for value matching value

Why not just to use predicates to do that for you?: // For number kind of values: NSPredicate *predicate = [NSPredicate predicateWithFormat:@”SELF = %@”, value]; NSArray *results = [array_to_search filteredArrayUsingPredicate:predicate]; // For string kind of values: NSPredicate *predicate = [NSPredicate predicateWithFormat:@”SELF contains[cd] %@”, value]; NSArray *results = [array_to_search filteredArrayUsingPredicate:predicate]; // For any object kind of … Read more

What’s the best way to put a c-struct in an NSArray?

NSValue doesn’t only support CoreGraphics structures – you can use it for your own too. I would recommend doing so, as the class is probably lighter weight than NSData for simple data structures. Simply use an expression like the following: [NSValue valueWithBytes:&p objCType:@encode(Megapoint)]; And to get the value back out: Megapoint p; [value getValue:&p];

Rebuild an NSArray by grouping objects that have matching id numbers?

NSArray *array = @[@{@”groupId” : @”1″, @”name” : @”matt”}, @{@”groupId” : @”2″, @”name” : @”john”}, @{@”groupId” : @”3″, @”name” : @”steve”}, @{@”groupId” : @”4″, @”name” : @”alice”}, @{@”groupId” : @”1″, @”name” : @”bill”}, @{@”groupId” : @”2″, @”name” : @”bob”}, @{@”groupId” : @”3″, @”name” : @”jack”}, @{@”groupId” : @”4″, @”name” : @”dan”}, @{@”groupId” : @”1″, @”name” … Read more

Compiler error “expected method not found” when using subscript on NSArray

You’ve got to be compiling with the iOS 6 or OS X 10.8 SDKs — otherwise Foundation objects don’t have the necessary methods for the subscripting bit of the literal syntax.* Specifically in this case, the subscripting expects objectAtIndexedSubscript: to be implemented by NSArray, and that’s a new method that was created to interact with … Read more

Which has faster performance indexesOfObjectsPassingTest or filteredArrayUsingPredicate?

The following tests (compiled in Release mode, executed on a Mac Pro) indicate that filteredArrayUsingPredicate is slower than indexesOfObjectsPassingTest if you use a “textual” predicate, but faster if you use block-based predicate. The fasted method in my test was a simple (fast-enumeration) loop that adds all matching objects to a mutable array. Results for filtering … Read more

Store [String] in NSUserDefaults

The following code should help you resolve your problem: import UIKit class ViewController: UIViewController { var food: [String] { get { if let returnValue = NSUserDefaults.standardUserDefaults().objectForKey(“food”) as? [String] { return returnValue } else { return [“muesli”, “banana”] //Default value } } set { NSUserDefaults.standardUserDefaults().setObject(newValue, forKey: “food”) NSUserDefaults.standardUserDefaults().synchronize() } } override func viewDidLoad() { super.viewDidLoad() print(food) … Read more

How do copy and mutableCopy apply to NSArray and NSMutableArray?

copy and mutableCopy are defined in different protocols (NSCopying and NSMutableCopying, respectively), and NSArray conforms to both. mutableCopy is defined for NSArray (not just NSMutableArray) and allows you to make a mutable copy of an originally immutable array: // create an immutable array NSArray *arr = [NSArray arrayWithObjects: @”one”, @”two”, @”three”, nil ]; // create … Read more

How can I sort an NSArray containing NSDictionaries?

Here is an example. Imagines you have an array of dictionnaries. Ecah dictionnary have 2 keys “name” & “dateOfBirth”. You can sort your array by “name” like this : // // Sort array by name // NSSortDescriptor *Sorter = [[NSSortDescriptor alloc] initWithKey:@”name” ascending:YES]; [myArray sortUsingDescriptors:[NSArray arrayWithObject:Sorter]]; [Sorter release]; Note that myArray is a NSMutableArray.