What is the NSObject isEqual: and hash default function?

As you’ve correctly guessed, NSObject‘s default isEqual: behaviour is comparing the memory address of the object. Strangely, this is not presently documented in the NSObject Class Reference, but it is documented in the Introspection documentation, which states: The default NSObject implementation of isEqual: simply checks for pointer equality. Of course, as you are doubtless aware, … Read more

NSArray from NSSet – Do I have to sort it myself?

You can specify a sort when you retrieve data with an NSFetchRequest by setting the sortDescriptors property to an array of NSSortDescriptors. But if you already have it in an NSSet and don’t want to make another fetch request, you can use: [[theSet allObjects] sortedArrayUsingDescriptors:sortDescriptors]; It’ll create an interim NSArray when you call allObjects and … Read more

How to create a NSString from a format string like @”xxx=%@, yyy=%@” and a NSArray of objects?

It is actually not hard to create a va_list from an NSArray. See Matt Gallagher’s excellent article on the subject. Here is an NSString category to do what you want: @interface NSString (NSArrayFormatExtension) + (id)stringWithFormat:(NSString *)format array:(NSArray*) arguments; @end @implementation NSString (NSArrayFormatExtension) + (id)stringWithFormat:(NSString *)format array:(NSArray*) arguments { char *argList = (char *)malloc(sizeof(NSString *) * … 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

Disadvantage of using NSMutableArray vs NSArray?

If you’re loading objects from a database and you know exactly how many objects you have, you would likely get the best performance from NSMutableArrays arrayWithCapacity: method, and adding objects to it until full, so it allocates all the memory at once if it can. Behind the scenes, they’re secretly the same thing – NSArray … Read more

tech