Tag: nsarray

Plist Array to NSDictionary

I have a plist: <plist version=”1.0″> <array> <dict> <key>name</key> <string>Alabama</string> <key>abreviation</key> <string>AL</string> <key>date</key> <string>1819</string> <key>population</key> <string>4,627,851</string> <key>capital</key> <string>Montgomery</string> <key>largestCity</key> <string>Birmingham</string> </dict> <dict> <key>name</key> <string>Alaska</string> <key>abreviation</key> <string>AK</string> <key>date</key> <string>1959</string> <key>population</key> <string>683,478</string> <key>capital</key> <string>Juneau</string> <key>largestCity</key> <string>Anchorage</string> </dict> … </array> </plist> I am trying to load it into an NSDictionary like this: NSString *path = [[NSBundle mainBundle] pathForResource:@”stateInfo” […]

Convert NSArray to NSDictionary

How can I convert an NSArray to an NSDictionary, using an int field of the array’s objects as key for the NSDictionary?

Serialize and Deserialize Objective-C objects into JSON

I need to serialize and deserialize objective-c objects into JSON to store in CouchDB. Do people have any example code for best practice for a general solution? I looked at a few JSON framework and they are stopped at the NSDictionary/NSArray level. i.e. A lot of framework will serialize and deserialize NSDictionary/NSArray into JSON. But […]

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

I have 2 nsarray, with the same values but in different order. NSArray * array1 = {0,1,2,3} NSArray * array2 = {2,3,1,0} I need a method to determinate if two arrays have the same values in a different order. Kind of -(BOOL) isSameValues:(NSArray*)array1 and:(NSArray*)array2;

Using NSRegularExpression to extract URLs on the iPhone

I’m using the following code on my iPhone app, taken from http://tinyurl.com/remarkablepixels to extract all URLs from striped .html code. I’m only being able to extract the first URL, but I need an array containing all URLs. My NSArray isn’t returning NSStrings for each URL, but the objects descriptions only. How do I make my […]

Search NSArray for value matching value

I have an NSArray of objects, which has a particular property called name (type NSString). I have a second NSArray of NSStrings which are names. I’d like to get an NSArray of all the objects whose .name property matches one of the names in the second NSArray. How do I go about this, fast and […]

Sort NSArray of custom objects by their NSDate properties

I am attempting to sort an NSArray that is populated with custom objects. Each object has a property startDateTime that is of type NSDate. The following code results in an array, sortedEventArray, populated but not sorted. Am I going about this the completely wrong way or am I just missing something small? NSSortDescriptor *dateDescriptor = […]

Compiler error “expected method not found” when using subscript on NSArray

I wrote this simple code to try out the new Objective-C literal syntax for NSArrays: NSArray *array = @[@”foo”]; NSLog(@”%@”, array[0]); The first line works fine, but the subscripting results in an error: Expected method to read array element not found on object of type ‘NSArray *’ Just wondering if I have done something wrong, […]

How to convert NSNumber to NSString

So I have an NSArray “myArray” with NSNumbers and NSStrings. I need them in another UIView so i go like this: – (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { DetailViewController *details = [[DetailViewController alloc] initWithNibName:@”DetailView” bundle:nil]; details.subjectText = [[myArray objectAtIndex:indexPath.row] objectForKey:@”subject”]; The subjectText works. But how can I get the NSNumbers out of it? (I actually need […]

What's the best way to put a c-struct in an NSArray?

What’s the usual way to store c-structures in an NSArray? Advantages, disadvantages, memory handling? Notably, what’s the difference between valueWithBytes and valueWithPointer — raised by justin and catfish below. Here’s a link to Apple’s discussion of valueWithBytes:objCType: for future readers… For some lateral thinking and looking more at performance, Evgen has raised the issue of […]