Is there a simple way to split a NSString into an array of characters?

You could do something like this (if you want to use enumerators) NSString *fooString = @”Hello”; NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[fooString length]]; [fooString enumerateSubstringsInRange:NSMakeRange(0, fooString.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { [characters addObject:substring]; }]; And if you really wanted it in an NSArray finally NSArray *fooChars = [NSArray arrayWithArray:characters];

Sorting NSArray of dictionaries by value of a key in the dictionaries

I think this will do it: brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@”brand” ascending:YES]; sortDescriptors = [NSArray arrayWithObject:brandDescriptor]; sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors]; I pulled the code from Sort Descriptor Programming Topics. Also, Key-Value Coding comes into play, in that sortedArrayUsingDescriptors: will send a valueForKey: to each element in myArray, and then use standard comparators to sort the … Read more

filtering NSArray into a new NSArray in Objective-C

NSArray and NSMutableArray provide methods to filter array contents. NSArray provides filteredArrayUsingPredicate: which returns a new array containing objects in the receiver that match the specified predicate. NSMutableArray adds filterUsingPredicate: which evaluates the receiver’s content against the specified predicate and leaves only objects that match. These methods are illustrated in the following example. NSMutableArray *array … Read more

Finding smallest and biggest value in NSArray of NSNumbers

If execution speed (not programming speed) is important, then an explicit loop is the fastest. I made the following tests with an array of 1000000 random numbers: Version 1: sort the array: NSArray *sorted1 = [numbers sortedArrayUsingSelector:@selector(compare:)]; // 1.585 seconds Version 2: Key-value coding, using “doubleValue”: NSNumber *max=[numbers valueForKeyPath:@”@max.doubleValue”]; NSNumber *min=[numbers valueForKeyPath:@”@min.doubleValue”]; // 0.778 seconds … Read more

Best way to sort an NSArray of NSDictionary objects?

Use NSSortDescriptor like this.. NSSortDescriptor * descriptor = [[NSSortDescriptor alloc] initWithKey:@”interest” ascending:YES]; stories = [stories sortedArrayUsingDescriptors:@[descriptor]]; recent = [stories copy]; stories is the array you want to sort. recent is another mutable array which has sorted dictionary values. Change the @”interest” with the key value on which you have to sort. All the best

Using NSPredicate to filter an NSArray based on NSDictionary keys

It should work – as long as the data variable is actually an array containing a dictionary with the key SPORT NSArray *data = [NSArray arrayWithObject:[NSMutableDictionary dictionaryWithObject:@”foo” forKey:@”BAR”]]; NSArray *filtered = [data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@”(BAR == %@)”, @”foo”]]; Filtered in this case contains the dictionary. (the %@ does not have to be quoted, this is done … Read more

Best practice? – Array/Dictionary as a Core Data Entity Attribute [closed]

There is no “native” array or dictionary type in Core Data. You can store an NSArray or an NSDictionary as a transformable attribute. This will use the NSCoding to serialize the array or dictionary to an NSData attribute (and appropriately deserialize it upon access). The advantage of this approach is that it’s easy. The downside … Read more