nsmutablearray

How to sort NSMutableArray using sortedArrayUsingDescriptors?

旧巷老猫 提交于 2019-11-26 04:21:16
I have a question about sorting NSMutableArray . I can use sortedArrayUsingDescriptors: method to sort an array with objects. For example I have an NSMutableArray of places where I have an attribute frequency (int value) and I want to sort descending on frequency but I don't know how to use it correctly. What do I put as a key in initWithKey ? My object place contains: NSString * name; NSString * address; NSString * frequency; NSString * type; NSMutableArray * places; ... populate array with objects ... NSSortDescriptor * sortByFrequency = [[[NSSortDescriptor alloc] initWithKey:@"????????"

NSMutableArray - force the array to hold specific object type only

我是研究僧i 提交于 2019-11-26 03:09:26
问题 Is there a way to force NSMutableArray to hold one specific object type only? I have classes definitions as follow: @interface Wheel:NSObject { int size; float diameter; } @end @interface Car:NSObject { NSString *model; NSString *make; NSMutableArray *wheels; } @end How can I force wheels array to hold Wheel objects only with code? (and absolutely not other objects) 回答1: Update in 2015 This answer was first written in early 2011 and began: What we really want is parametric polymorphism so you

How to sort NSMutableArray using sortedArrayUsingDescriptors?

两盒软妹~` 提交于 2019-11-26 01:53:09
问题 I have a question about sorting NSMutableArray . I can use sortedArrayUsingDescriptors: method to sort an array with objects. For example I have an NSMutableArray of places where I have an attribute frequency (int value) and I want to sort descending on frequency but I don\'t know how to use it correctly. What do I put as a key in initWithKey ? My object place contains: NSString * name; NSString * address; NSString * frequency; NSString * type; NSMutableArray * places; ... populate array with

The best way to remove duplicate values from NSMutableArray in Objective-C?

核能气质少年 提交于 2019-11-26 00:23:57
问题 The best way to remove duplicate values ( NSString ) from NSMutableArray in Objective-C? Is this the easiest and right way to do it? uniquearray = [[NSSet setWithArray:yourarray] allObjects]; 回答1: Your NSSet approach is the best if you're not worried about the order of the objects, but then again, if you're not worried about the order, then why aren't you storing them in an NSSet to begin with? I wrote the answer below in 2009; in 2011, Apple added NSOrderedSet to iOS 5 and Mac OS X 10.7.

Cannot add items to an NSMutableArray ivar

喜你入骨 提交于 2019-11-25 23:47:31
问题 My goal is to add a string to array, and I do that in a method which I call. In this method, I get a null value in the array, and don\'t know why. I have this at the start of my class: NSMutableArray *listOfEvents; and a method which I call on each event: -(void)EventList { [listOfEvents addObject:@\"ran\"]; NSLog(@\"%@\", listOfEvents); } I get (null) in the log. If I put the array definition NSMutableArray *listOfEvents; in the function body, I get the string value @\"ran\" , each time, so

NSMutableArray addObject not working

烈酒焚心 提交于 2019-11-25 22:25:30
问题 I have declared an NSMutableArray *categories in my view controller .h file, and declared a property for it. In the parser:foundCharacters: method of the NSXMLParser delegate in my .m file, I have this code: -(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string { if (elementFound) { element = string; [self.categories addObject:element]; } } But when I hover over the [self.categories addObject:element] line after stepping into it in debug mode, XCode tells me the size is 0x0,

How do I sort an NSMutableArray with custom objects in it?

倖福魔咒の 提交于 2019-11-25 22:14:33
问题 What I want to do seems pretty simple, but I can\'t find any answers on the web. I have an NSMutableArray of objects, and let\'s say they are \'Person\' objects. I want to sort the NSMutableArray by Person.birthDate which is an NSDate . I think it has something to do with this method: NSArray *sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(???)]; In Java I would make my object implement Comparable, or use Collections.sort with an inline custom comparator...how on earth do you