nsarray

Which has faster performance indexesOfObjectsPassingTest or filteredArrayUsingPredicate?

*爱你&永不变心* 提交于 2019-11-26 21:52:40
When needing to filter an NSArray to get a subset of the items in the array returned, which method is quicker more frequently and in edge cases? The following tests (compiled in Release mode, executed on a Mac Pro) indicate that filteredArrayUsingPredicate is slower than indexesOfObjectsPassingTest if you use a "textual" predicate, but faster if you use block-based predicate. The fasted method in my test was a simple (fast-enumeration) loop that adds all matching objects to a mutable array. Results for filtering an array of 10,000,000 dictionaries, where about 50% match the predicate: 8.514334

What is the BOOL *stop argument for enumerateObjectsUsingBlock: used for?

谁说胖子不能爱 提交于 2019-11-26 21:34:30
I've been using enumerateObjectsUsingBlock: a lot lately for my fast-enumeration needs, and I'm having a hard time understanding the usage of BOOL *stop in the enumeration block. The NSArray class reference states stop : A reference to a Boolean value. The block can set the value to YES to stop further processing of the array. The stop argument is an out-only argument. You should only ever set this Boolean to YES within the Block. So then of course I can add the following in my block to stop the enumeration: if (idx == [myArray indexOfObject:[myArray lastObject]]) { *stop = YES; } From what I

How to split an NSArray into two equal pieces?

一笑奈何 提交于 2019-11-26 21:32:07
问题 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) {

Convert NSArray to NSString in Objective-C

前提是你 提交于 2019-11-26 21:11:48
I am wondering how to convert an NSArray [@"Apple", @"Pear ", 323, @"Orange"] to a string in Objective-C . Dave DeLong NSString * result = [[array valueForKey:@"description"] componentsJoinedByString:@""]; One approach would be to iterate over the array, calling the description message on each item: NSMutableString * result = [[NSMutableString alloc] init]; for (NSObject * obj in array) { [result appendString:[obj description]]; } NSLog(@"The concatenated string is %@", result); Another approach would be to do something based on each item's class: NSMutableString * result = [[NSMutableString

Observing count in NSMutableArray

时光总嘲笑我的痴心妄想 提交于 2019-11-26 21:06:52
问题 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.. 回答1: You’ll need to use KVC. But how to go about doing it? After all, NSMutableArray is not Key-Value

Store [String] in NSUserDefaults

淺唱寂寞╮ 提交于 2019-11-26 20:23:02
I want to save a Swift Style String Array into NSUserDefaults, but acutally the "if" statement in the code says that returnValue is always nil. Later in the code (iOS 8) I want to use "food += ["spaghetti"] to add new entries. var food : [String] { get { var returnValue : [String]? = NSUserDefaults.standardUserDefaults().objectForKey("food") as? [String] if returnValue == nil //Check for first run of app { returnValue = ["muesli", "banana"]; //Default value } return returnValue! } set (newValue) { NSUserDefaults.standardUserDefaults().setObject(newValue, forKey: "food") NSUserDefaults

NSMutablearray move object from index to index

别等时光非礼了梦想. 提交于 2019-11-26 19:47:55
问题 I have a UItableview with reordable rows and the data is in an NSarray. So how do I move an object in the NSMutablearray when the appropriate tableview delegate is called? Another way to ask this is how to reorder an NSMutableArray? 回答1: id object = [[[self.array objectAtIndex:index] retain] autorelease]; [self.array removeObjectAtIndex:index]; [self.array insertObject:object atIndex:newIndex]; That's all. Taking care of the retain count is important, since the array might be the only one

Key Value Observing with an NSArray

不想你离开。 提交于 2019-11-26 19:31:20
问题 I've looked on SO for examples of using Key Value Observing with an NSArray (or NSMutableArray ) and apparently you need to use an NSArrayController (which unlike KVO I'm not familiar with), but I haven't found concrete examples of how to do this. Can anyone explain with some sample code? For instance, if I have a GameModel which represents its player names with an NSArray (playerNameArray) of NSStrings . I want to observe those strings (the view controller observes the model's data) to

How do copy and mutableCopy apply to NSArray and NSMutableArray?

只谈情不闲聊 提交于 2019-11-26 19:30:30
What is the difference between copy and mutableCopy when used on either an NSArray or an NSMutableArray ? This is my understanding; is it correct? // ** NSArray ** NSArray *myArray_imu = [NSArray arrayWithObjects:@"abc", @"def", nil]; // No copy, increments retain count, result is immutable NSArray *myArray_imuCopy = [myArray_imu copy]; // Copys object, result is mutable NSArray *myArray_imuMuta = [myArray_imu mutableCopy]; // Both must be released later // ** NSMutableArray ** NSMutableArray *myArray_mut = [NSMutableArray arrayWithObjects:@"A", @"B", nil]; // Copys object, result is immutable

I want to sort an array using NSSortDescriptor

给你一囗甜甜゛ 提交于 2019-11-26 19:14:12
问题 I am having a problem regarding sorting an array w.r.t database: NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"w" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObject: sorter]; [mGlossaryArray sortUsingDescriptors:sortDescriptors]; [sorter release]; Here in database there are some first capital letters and because of that capital letter it does not show me proper sorted output. Here i am sorting an array with r.t "w" which is my table column in database.