nsarray

KVO Notifications for a Modification of an NSArray backed by a NSMutableArray

一个人想着一个人 提交于 2019-12-03 05:19:23
问题 I am trying to use KVO to listen to collection change events on an NSArray property. Publicly, the property is a readonly NSArray, but is backed by an NSMutableArray ivar so that I can modify the collection. I know I can set the property to a new value to get a “set” change, but I’m interested in add, remove, replace changes. How do I correctly notify these type of changes for an NSArray? @interface Model : NSObject @property (nonatomic, readonly) NSArray *items; @end @implementation Model {

How to count duplicates values in NSArray?

核能气质少年 提交于 2019-12-03 05:18:09
问题 Value of my NSArray includes the duplicates. I find the duplicates but now how can I find the no. they repeat? 回答1: You can use NSCountedSet for this. Add all your objects to a counted set, then use the countForObject: method to find out how often each object appears. 回答2: Example: NSArray *names = [NSArray arrayWithObjects:@"John", @"Jane", @"John", nil]; NSCountedSet *set = [[NSCountedSet alloc] initWithArray:names]; for (id item in set) { NSLog(@"Name=%@, Count=%lu", item, (unsigned long)

How to check if an NSString contains one of the NSStrings in an NSArray?

南笙酒味 提交于 2019-12-03 05:06:57
I'm making an iOS app and I need to figure out if an NSString contains any of the NSStrings in an NSArray . BOOL found=NO; for (NSString *s in arrayOfStrings) { if ([stringToSearchWithin rangeOfString:s].location != NSNotFound) { found = YES; break; } } Adam It may be a silly optimization for your use case, but depending on how large the array is that you are iterating, it may be helpful/more performant to use NSArray's indexOfObjectWithOptions:passingTest: method. With this method you pass some options and a block that contains your test. Passing the NSEnumerationConcurrent option will allow

Create index for UITableView from an NSArray

别说谁变了你拦得住时间么 提交于 2019-12-03 04:35:11
问题 I've read that the best way of creating an index (the a-z at the side of a uitableview) is to set up an array of nsdictionaries, where each dictionary corresponds to a section, and a rowValue key contains an array of the rows. NSDictionary headerTitle => ‘A’ rowValues => {”Aardvark”, “Ape”, “Aquaman”} NSDictionary headerTitle => ‘B’ rowValues => {”Bat”, “Boot”, “Bubbles”} etc But how can this be created from an array of all the row titles - {”Aardvark”, “Ape”, “Aquaman”, ”Bat”, “Boot”,

Concatenation of two arrays in Objective-C

好久不见. 提交于 2019-12-03 04:13:13
How to concatenate two arrays into a single array in Objective-C? NSArray* newArray = [firstArray arrayByAddingObjectsFromArray:secondArray]; Or using mutable array version just add to it all objects from another array: [myMutableArray addObjectsFromArray:secondArray]; bnabilos With immutable arrays: NSArray *arr1 = @[@(1), @(2), @(3)]; NSArray *arr2 = @[@(4), @(5), @(6)]; NSArray *arr3 = [arr1 arrayByAddingObjectsFromArray:arr2]; or adding onto a mutable array: NSArray *arr1 = @[@(1), @(2), @(3)]; NSArray *arr2 = @[@(4), @(5), @(6)]; NSMutableArray *arr3 = [NSMutableArray arrayWithArray:arr1]

Testing if NSMutableArray contains a string object

≡放荡痞女 提交于 2019-12-03 04:06:44
问题 I have a NSMutableArray which contains a few NSString objects. How can I test if the array contains a particular string literal? I tried [array containsObject:@"teststring"] but that doesn't work. 回答1: What you're doing should work fine. For example NSArray *a = [NSArray arrayWithObjects:@"Foo", @"Bar", @"Baz", nil]; NSLog(@"At index %i", [a indexOfObject:@"Bar"]); Correctly logs "At index 1" for me. Two possible foibles: indexOfObject sends isEqual messages to do the comparison - you've not

IOS: store an array with NSUserDefault

[亡魂溺海] 提交于 2019-12-03 03:54:01
I want to store an array with NSUserDefault , then, I put in applicationDidEnterBackground [[NSUserDefaults standardUserDefaults] setObject:myArray forKey:@"myArray"]; and in application didFinishLaunchingWithOption myArray= [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"myArray"]]; it's ok for multitasking device, but for not-multitasking device, how can I solve? Store the object in NSUserDefaults in -applicationWillTerminate: , if it hasn't already been saved by the invocation of -applicationDidEnterBackground: (i.e. check if multitasking is

Difference between NSArray and NSMutableArray

那年仲夏 提交于 2019-12-03 03:51:20
问题 What is the difference b/w NSArray and NSMutableArray ? 回答1: NSMutableArray (and all other classes with Mutable in the name) can be modified. So, if you create a plain NSArray , you cannot change its contents later (without recreating it). But if you create an NSMutableArray , you can change it — you'll notice it has methods like -addObject: and -insertObject:atIndex: . See the documentation for details. 回答2: The "mutable" types are classes which can be changed after they've been initialized,

Sorting an NSArray by an NSDictionary value

余生长醉 提交于 2019-12-03 03:46:52
I'm trying to sort an array that would look something like this: (please ignore the fact these people are well past any living age! I just needed large numbers) NSDictionary *person1 = [NSDictionary dictionaryWithObjectsAndKeys:@"sam",@"name",@"28.00",@"age",nil]; NSDictionary *person2 = [NSDictionary dictionaryWithObjectsAndKeys:@"cody",@"name",@"100.00",@"age",nil]; NSDictionary *person3 = [NSDictionary dictionaryWithObjectsAndKeys:@"marvin",@"name",@"299.00",@"age",nil]; NSDictionary *person4 = [NSDictionary dictionaryWithObjectsAndKeys:@"billy",@"name",@"0.0",@"age",nil]; NSDictionary

Converting NSArray to NSMutableArray [closed]

笑着哭i 提交于 2019-12-03 03:38:09
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . As above. Would be helpful to know. Thanks! 回答1: Here are two options: - (NSMutableArray *)createMutableArray1:(NSArray *)array { return [NSMutableArray arrayWithArray:array]; } - (NSMutableArray *)createMutableArray2:(NSArray *)array { return [[array mutableCopy] autorelease]; } 回答2: Use -mutableCopy and make