Sort NSArray of custom objects based on sorting of another NSArray of strings

Hereby, I compare directly the index of obj1.assetID in stringOrder with the index of obj2.assetID in stringOrder (using Objective-C literals for @() to transform NSString => NSNumber) [items sortUsingComparator:^NSComparisonResult(Attribute *obj1, Attribute *obj2) { return [@([stringOrder indexOfObject:obj1.assetID]) compare:@([stringOrder indexOfObject:obj2.assetID])] }]; Or without ObjC literals : [items sortUsingComparator:^NSComparisonResult(Attribute *obj1, Attribute *obj2) { return [[NSNumber numberWithInt:[stringOrder indexOfObject:obj1.assetID]] compare:[NSNumber … Read more

Compare two arrays with the same value but with a different order

You can use NSCountedSet for that purpose: – (BOOL)isSameValues:(NSArray*)array1 and:(NSArray*)array2 { NSCountedSet *set1 = [NSCountedSet setWithArray:array1]; NSCountedSet *set2 = [NSCountedSet setWithArray:array2]; return [set1 isEqualToSet:set2]; } NSCountedSet is a collection of different objects, where each object has an associated counter with it. Therefore the result for NSArray *array1 = @[@0,@1,@2,@3]; NSArray *array2 = @[@2,@3,@1,@0]; is YES, … Read more

Using NSRegularExpression to extract URLs on the iPhone

The method matchesInString:options:range: returns an array of NSTextCheckingResult objects. You can use fast enumeration to iterate through the array, pull out the substring of each match from your original string, and add the substring to a new array. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@”http?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)?” options:NSRegularExpressionCaseInsensitive error:&error]; NSArray *arrayOfAllMatches = [regex matchesInString:httpLine options:0 range:NSMakeRange(0, [httpLine length])]; NSMutableArray … Read more

What is an easy way to break an NSArray with 4000+ objects in it into multiple arrays with 30 objects each?

Off the top of my head, something like (untested): NSMutableArray *arrayOfArrays = [NSMutableArray array]; int itemsRemaining = [stuff count]; int j = 0; while(itemsRemaining) { NSRange range = NSMakeRange(j, MIN(30, itemsRemaining)); NSArray *subarray = [stuff subarrayWithRange:range]; [arrayOfArrays addObject:subarray]; itemsRemaining-=range.length; j+=range.length; } The MIN(30, i) takes care of that last array that won’t necessarily have 30 … Read more

Serialize and Deserialize Objective-C objects into JSON

Finally we can solve this problem easily using JSONModel. This is the best method so far. JSONModel is a library that generically serialize/deserialize your object based on Class. You can even use non-nsobject based for property like int, short and float. It can also cater nested-complex JSON. Considering this JSON example: { “accounting” : [{ … Read more

Convert an iOS objective c object to a JSON string

EDIT: I have made a dummy app that should be a good example for you. I create a Message class from your code snippet; //Message.h @interface Message : NSObject { NSString *from_; NSString *date_; NSString *msg_; } @property (nonatomic, retain) NSString *from; @property (nonatomic, retain) NSString *date; @property (nonatomic, retain) NSString *msg; -(NSDictionary *)dictionary; @end … Read more

Array of tuples in Swift

It looks to me like resultArray.append() is treating the tuple a little bit like a variadic parameter, and trying to expand the tuple to match its own arguments. It’s complaining about your second parameter because it’s only expecting one. I haven’t seen this behavior for Array.append() documented anywhere, so I would say it’s a bug … Read more