nsarray

How can I sort an NSArray containing NSDictionaries?

最后都变了- 提交于 2019-11-26 19:07:38
I have an NSArray which contains NSDictionary objects. Each of these NSDictionary objects contains an ORDER key. How can I sort this NSArray based on this key within each of these NSDictionaries ? Here is an example. Imagines you have an array of dictionnaries. Ecah dictionnary have 2 keys "name" & "dateOfBirth". You can sort your array by "name" like this : // // Sort array by name // NSSortDescriptor *Sorter = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; [myArray sortUsingDescriptors:[NSArray arrayWithObject:Sorter]]; [Sorter release]; Note that myArray is a NSMutableArray .

JSON Parsing in iOS 7

荒凉一梦 提交于 2019-11-26 18:59:43
问题 I am creating an app for as existing website. They currently has the JSON in the following format : [ { "id": "value", "array": "[{\"id\" : \"value\"} , {\"id\" : \"value\"}]" }, { "id": "value", "array": "[{\"id\" : \"value\"},{\"id\" : \"value\"}]" } ] which they parse after escaping the \ character using Javascript. My problem is when i parse it in iOS using the following command : NSArray *result = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&localError];

How do I convert NSMutableArray to NSArray?

拟墨画扇 提交于 2019-11-26 18:45:25
问题 How do I convert NSMutableArray to NSArray in objective-c? 回答1: NSArray *array = [mutableArray copy]; Copy makes immutable copies. This is quite useful because Apple can make various optimizations. For example sending copy to a immutable array only retains the object and returns self . If you don't use garbage collection or ARC remember that -copy retains the object. 回答2: An NSMutableArray is a subclass of NSArray so you won't always need to convert but if you want to make sure that the array

Getting an array of a property from an object array

南笙酒味 提交于 2019-11-26 18:36:42
问题 I need to extract an array of a single property from a custom object array. eg. @interface MyClass : NSObject { int sampleNumber; NSString *sampleName; } I have an array of MyClass instances called myArray . I want to then get an array of the sampleName strings. Is there a way to do it without stepping through the whole array like this: NSMutableArray *stringArray; for (MyClass *thisInstance in myArray) { [stringArray addObject:thisInstance.sampleName]; } I attempted to search for a similar

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

孤街浪徒 提交于 2019-11-26 18:01:05
Is there a simple way to split a NSString into an array of characters? It would actually be best if the resulting type were a collection of NSString's themselves, just one character each. Yes, I know I can do this in a loop, but I'm wondering if there is a faster way to do this with any existing methods or functions the way you can with LINQ in C#. e.g. // I have this... NSString * fooString = @"Hello"; // And want this... NSArray * fooChars; // <-- Contains the NSStrings, @"H", @"e", @"l", @"l" and @"o" You could do something like this (if you want to use enumerators) NSString *fooString = @

Convert JSON feed to NSDictionary

风格不统一 提交于 2019-11-26 17:37:31
Where JSON_CATEGORY_DATA_URL_STRING is my feed URL, which returns fine as: [ { "group":"For Sale", "code":"SSSS" }, { "group":"For Sale", "category":"Wanted", "code":"SWNT" } ] I cannot seem to get a nice NSDictionary (or NSArray ) out of the following code: + (NSDictionary *)downloadJSON { NSDictionary *json_string; NSString *dataURL = [NSString stringWithFormat:@"%@", JSON_CATEGORY_DATA_URL_STRING]; NSLog(@"%@",dataURL); NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:dataURL]]; NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil

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

这一生的挚爱 提交于 2019-11-26 17:19:22
I have an array populated by dictionaries, and I need to sort the array alphabetically by the values of one of the keys of the dictionaries. This is my array: tu dictus: ( { brand = Ryul; productTitle = Any; quantity = 1; subBrand = "Ryul INJ"; type = Product; }, { brand = Trol; productTitle = Different; quantity = 2; subBrand = ""; type = Brand; }, { brand = Dtor; productTitle = Any; quantity = 1; subBrand = ""; type = Product; }, { brand = Ryul; productTitle = Different; quantity = 2; subBrand = "Ryul CHES"; type = SubBrand; }, { brand = Anan; productTitle = Any; quantity = 1; subBrand = "";

Using @[array, of, items] vs [NSArray arrayWithObjects:]

拟墨画扇 提交于 2019-11-26 17:14:54
问题 Is there a difference between NSArray *myArray = @[objectOne, objectTwo, objectThree]; and NSArray *myArray = [NSArray arrayWithObjects:objectOne, objectTwo, objectThree, nil]; Is one preferred over the other? 回答1: They are almost identical, but not completely. The Clang documentation on Objective-C Literals states: Array literal expressions expand to calls to +[NSArray arrayWithObjects:count:] , which validates that all objects are non-nil. The variadic form, +[NSArray arrayWithObjects:]

UIBezierPath Multiple Line Colors

℡╲_俬逩灬. 提交于 2019-11-26 16:56:02
问题 My attempt to draw UIBezierPath lines with different colors is failing me. All the lines change to the currently selected color. All my path and information are all stored in an NSMutableArray called pathInfo. In path info I drop in array that contains the Path, Color, Width, and Type of line. This works fine except all the lines turn to whatever color the user has selected. I would appreciate any help greatly! - (void)drawRect:(CGRect)rect { UIBezierPath *drawPath = [UIBezierPath bezierPath]

Flatten an NSArray

穿精又带淫゛_ 提交于 2019-11-26 16:42:10
问题 I have an array like this: array: ( ( "http://aaa/product/8_1371121323.png", "http://aaa/product/14_1371123271.png" ), ( "http://aaa/product/9_1371121377.png" ) ) and I have to create another array from that one like this array: ( "http://aaa/product/8_1371121323.png", "http://aaa/product/14_1371123271.png", "http://aaa/product/9_1371121377.png" ) How can I do that? Is it possible to combine all the objects and separate them using some string? 回答1: Sample Code : NSMutableArray *mainArray = [