nsarray

NSPredicate on array of arrays

一个人想着一个人 提交于 2019-11-28 01:46:37
问题 I have an array, that when printed out looks like this: ( ( databaseVersion, 13 ), ( lockedSetId, 100 ) ) Would it be possible to filter this using an NSPredicate (potentially by the index in the array). So something like: give me all rows where element 0 is 'databaseVersion'? I know that if I had an array of dictionaries I could do this with a predicate similar the one found here, but I found that when using dictionaries and storing a large amount of data, my memory consumption went up (from

Sort UITableView into sections by date from Array of events

我的未来我决定 提交于 2019-11-28 01:43:43
问题 I've retrieved data from 2 different shared Google Calendars, and stored the data in an array. I need to sort the data into a sectioned UITableView, sorted by date. Heres my code: CalendarModel.h #import "JSONModel.h" #import "Time.h" @interface CalendarModel : JSONModel @property (strong, nonatomic) NSString* title; @property (strong, nonatomic) NSArray<Time>* time; @end CalendarModel.m #import "CalendarModel.h" @implementation CalendarModel +(JSONKeyMapper*)keyMapper { return [

How to sort a NSArray which contains NSDictionary?

拥有回忆 提交于 2019-11-28 01:08:47
问题 I have a plist like this: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>highscores</key> <array> <dict> <key>highscoreInSeconds</key> <string>9</string> <key>levelName</key> <string>1</string> <key>name</key> <string>Black</string> </dict> <dict> <key>highscoreInSeconds</key> <string>12</string> <key>levelName</key> <string>1</string> <key>name</key> <string>Black<

how to sort an NSArray using compare:options

淺唱寂寞╮ 提交于 2019-11-28 00:53:50
问题 I have an NSArray containing numbers as NSString objects. ie. [array addObject:[NSString stringWithFormat:@"%d", 100]]; How do I sort the array numerically? Can I use compare:options and specify NSNumericSearch as NSStringCompareOptions ? Please give me an example/sample code. 回答1: You can use the example code given for the sortedArrayUsingFunction:context: method which should work fine for NSStrings too as they also have the intValue method. // Place this functions somewhere above

Help sorting an NSArray across two properties (with NSSortDescriptor?)

爷,独闯天下 提交于 2019-11-28 00:23:21
问题 I'm a bit of a NSSortDescriptor n00b. I think, though, it is the right tool for what I need to do: I have an NSArray consisting of objects with keys, say, "name" and "time". Instead of verbalizing it, here's an example: input: name: time B: 4 C: 8 B: 5 C: 4 A: 3 C: 2 A: 1 A: 7 B: 6 desired output: name: time A: 1 <--- A: 3 A: 7 C: 2 <--- C: 4 C: 8 B: 4 <--- B: 5 B: 6 So the values are sorted by "time" and grouped by "name". A comes first because he had the smallest time value, and all values

How to split an NSArray into two equal pieces?

冷暖自知 提交于 2019-11-27 23:36:58
I have an NSArray, and I want to split it into two equal pieces (if odd "count" then add to the latter new array) - I want to split it "down the middle" so to speak. The following code does exactly what I want, but is there a better way?: // NOTE: `NSArray testableArray` is an NSArray of objects from a class defined elsewhere; NSMutableArray *leftArray = [[NSMutableArray alloc] init]; NSMutableArray *rightArray = [[NSMutableArray alloc] init]; for (int i=0; i < [testableArray count]; i=i+1) { if (i < [testableArray count]/2) { [leftArray addObject:[testableArray objectAtIndex:i]]; } else {

Filter array by first letter of string property

烈酒焚心 提交于 2019-11-27 22:53:01
I have an NSArray with objects that have a name property. I would like filter the array by name NSString *alphabet = [agencyIndex objectAtIndex:indexPath.section]; //---get all states beginning with the letter--- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet]; NSMutableArray *listSimpl = [[NSMutableArray alloc] init]; for (int i=0; i<[[Database sharedDatabase].agents count]; i++) { Town *_town = [[Database sharedDatabase].agents objectAtIndex:i]; [listSimpl addObject:_town]; } NSArray *states = [listSimpl filteredArrayUsingPredicate:predicate];

Observing count in NSMutableArray

两盒软妹~` 提交于 2019-11-27 22:27:59
I'd like to be notified, when the count, ie. number of items in an NSArray changes.. Of course I wouldn't need this, if I was in control of addition and removal of objects into the array. But I am not, it happens unpredictably with regards to Business Process Model and depends on external factors. Is there some simple elegant solution? EDIT: I am correcting this to NSMutableArray of course.. CodaFi You’ll need to use KVC . But how to go about doing it? After all, NSMutableArray is not Key-Value-Coding compliant for its mutation methods or contents changes. The answer is proxying –as

Difference between NSArray.array/.new /@[]/alloc-init

女生的网名这么多〃 提交于 2019-11-27 21:28:40
问题 There seem to be different methods of instantiating NSArrays (same for NSDictionary and some others). I know: [NSArray array] [NSArray new] @[] [[NSArray alloc] init] For readability reasons I usually stick with [NSArray array] , but what is the difference between all those, do they all really do the same? 回答1: The result is the same for all of them, you get a new empty immutable array. The different methods have different memory management implications though. Using ARC this makes no

NSArray + remove item from array

ε祈祈猫儿з 提交于 2019-11-27 21:05:38
问题 How to remove an item from NSArray. 回答1: NSArray is not mutable, that is, you cannot modify it. You should take a look at NSMutableArray. Check out the "Removing Objects" section, you'll find there many functions that allow you to remove items: [anArray removeObjectAtIndex: index]; [anArray removeObject: item]; [anArray removeLastObject]; 回答2: NSMutableArray *arrayThatYouCanRemoveObjects = [NSMutableArray arrayWithArray:your_array]; [arrayThatYouCanRemoveObjects removeObjectAtIndex:your