nsarray

Search NSArray for value matching value

独自空忆成欢 提交于 2019-11-27 04:30:33
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 efficiently as this will be required quite often. With your current data structures, you can only do it in O(n^2) time by looping over the first array once for each member of the second array: NSMutableArray * array = [NSMutableArray array]; for (NSString * name in names) { for (MyObject * object in

What is an easy way to break an NSArray with 4000+ objects in it into multiple arrays with 30 objects each?

匆匆过客 提交于 2019-11-27 04:25:29
What is an easy way to break an NSArray with 4000 objects in it into multiple arrays with 30 objects each? So right now I have an NSArray *stuff where [stuff count] = 4133. I want to make a new array that holds arrays of 30 objects. What is a good way to loop through, breaking *stuff into new 30-object arrays, and placing them inside of a larger array? Obviously the last array won't have 30 in it (it will have the remainder) but I need to handle that correctly. Make sense? Let me know if there is an efficient way to do this. samvermette Off the top of my head, something like (untested):

Is it possible to filter an NSArray by class?

风流意气都作罢 提交于 2019-11-27 04:25:09
问题 Is there a way construct a predicate to filter by class type? I currently loop through the array and check to the class of each object. Maybe there is a cleaner way? 回答1: You could add a category to NSObject that adds a "cf_className" method, like so: @interface NSObject (CFAdditions) - (NSString *) cf_className; @end @implementation NSObject (CFAdditions) - (NSString *) cf_className { return NSStringFromClass([self class]); } @end From there, you can use predicates like: NSPredicate * p =

Store NSArray In Core Data Sample Code?

眉间皱痕 提交于 2019-11-27 04:24:59
问题 I have been searching for some sample code on how to store an NSArray in Core Data for awhile now, but haven't had any luck. Would anyone mind pointing me to some tutorial or example, or better yet write a simple sample as an answer to this question? I have read this but it doesn't show an example of how to go about implementing a transformable attribute that is an NSArray . Thanks in advance! 回答1: If you really need to do it, then encode as data. I simply created a new filed called receive

Sort NSArray of custom objects by their NSDate properties

你说的曾经没有我的故事 提交于 2019-11-27 04:15:53
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 = [NSSortDescriptor sortDescriptorWithKey:@"startDateTime" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObject:dateDescriptor]; NSArray *sortedEventArray = [nodeEventArray sortedArrayUsingDescriptors:sortDescriptors]; Are you sure that the

Does NSArray copy objects?

a 夏天 提交于 2019-11-27 03:52:06
问题 When I make an NSArray using + [NSArray arrayWithObjects:] , does it copy those objects? If I release the objects after adding them to the array, will I run into problems? 回答1: No, it doesn't copy them. It retains them. Yes, you can safely release the objects after adding them to the array. The docs, as always, spell this out very clearly: Arrays maintain strong references to their contents—in a managed memory environment, each object receives a retain message before its id is added to the

Serialize and Deserialize Objective-C objects into JSON

馋奶兔 提交于 2019-11-27 03:39:46
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 I still have to do the work to convert NSDictionary into Objective-C objects. To make things more complex, my Object A can have reference to an NSArray/NSDictionary of Object Bs. My question is very similar to this question with addition of the collection

How to convert NSNumber to NSString

微笑、不失礼 提交于 2019-11-27 02:59:43
So I have an NSArray "myArray" with NSNumber s and NSString s. 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 NSNumber s out of it? (I actually need them as strings...) I would convert a NSString out of a NSNumber like this: NSString *blah = [NSNumber intValue] . But

Non-retaining array for delegates

五迷三道 提交于 2019-11-27 02:58:18
In a Cocoa Touch project, I need a specific class to have not only a single delegate object, but many of them. It looks like I should create an NSArray for these delegates; the problem is that NSArray would have all these delegates retained, which it shouldn't (by convention objects should not retain their delegates). Should I write my own array class to prevent retaining or are there simpler methods? Thank you! I found this bit of code awhile ago (can't remember who to attribute it to). It's quite ingenius, using a Category to allow the creation of a mutable array that does no retain/release

How to do sparse array in Cocoa

三世轮回 提交于 2019-11-27 02:52:26
问题 I have an undetermined size for a dataset based on unique integer keys. I would like to use an NSMutableArray for fast lookup since all my keys are integer based. I want to do this. NSMutableArray* data = [NSMutableArray array]; // just create with 0 size then later people will start throwing data at me with integer indexes (all unique) so I just want to do something like this... if ([data count] < index) [data resize:index]; // ? how do you resize and have the array resized so that i can