Finding Intersection of NSMutableArrays

Use NSMutableSet: NSMutableSet *intersection = [NSMutableSet setWithArray:array1]; [intersection intersectSet:[NSSet setWithArray:array2]]; [intersection intersectSet:[NSSet setWithArray:array3]]; NSArray *array4 = [intersection allObjects]; The only issue with this is that you lose ordering of elements, but I think (in this case) that that’s OK. As has been pointed out in the comments (thanks, Q80!), iOS 5 and OS X 10.7 … Read more

How to wrap a Struct into NSObject

Hm, try to look at the NSValue at https://developer.apple.com/documentation/foundation/nsvalue You can use it like struct aStruct { int a; int b; }; typedef struct aStruct aStruct; Then sort of “wrap” it to an NSValue object like: aStruct struct; struct.a = 0; struct.b = 0; NSValue *anObj = [NSValue value:&struct withObjCType:@encode(aStruct)]; NSArray *array = @[anObj]; To … Read more

How can I sort strings in NSMutableArray into alphabetical order?

There is the following Apple’s working example, using sortedArrayUsingSelector: and localizedCaseInsensitiveCompare: in the Collections Documentation: sortedArray = [anArray sortedArrayUsingSelector: @selector(localizedCaseInsensitiveCompare:)]; Note that this will return a new sorted array. If you want to sort your NSMutableArray in place, then use sortUsingSelector: instead, like so: [mutableArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

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

Sorting NSMutableArray By Object’s Property

NSSortDescriptorss make this really simple. With NSMutableArray you can sort the existing array using sortUsingDescriptors: and with immutable arrays you create a new array using sortedArrayUsingDescriptors: //This will sort by stringProperty ascending, then dateProperty ascending [mutable_array sortUsingDescriptors: @[ [NSSortDescriptor sortDescriptorWithKey:@”stringProperty” ascending:YES], [NSSortDescriptor sortDescriptorWithKey:@”dateProperty” ascending:YES] ]];

2D arrays using NSMutableArray

First, you must allocate and initialize your objects before use, something like: NSMutableArray * sections = [[NSMutableArray alloc] initWithCapacity:10]; For the rows, you need one object for each, not a single NSMutableArray * rows; Second, depending on whether you’re using Xcode 4.4+ (which introduced subscripting, a.k.a section[i] & section[i] = …) you may have to … Read more

Avoiding “NSArray was mutated while being enumerated”

You can always iterate without an enumerator. Which means a regular for loop, and when you remove an object:- decrement the index variable and continue;. if you are caching the array’s count before entering the for-loop, then make sure you decrement that too when removing an object. Anyway, I do not see why an array … Read more

File not found.