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

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

Sort NSArray of custom objects by their NSDate properties

Are you sure that the startDateTime instance variables of the node events are non-nil? If you don’t have one already, you might add a (custom) -description method to your node event objects that does something like this: – (NSString *)description { return [NSString stringWithFormat:@”%@ – %@”, [super description], startDateTime]]; } Then in your sorting code … Read more

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

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

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