nsmutablearray

Sorting an NSArray of NSString

為{幸葍}努か 提交于 2019-11-29 03:03:15
Can someone please show me the code for sorting an NSMutableArray? I have the following NSMutableArray: NSMutableArray *arr = [[NSMutableArray alloc] init]; with elements such as "2", "4", "5", "1", "9", etc which are all NSString. I'd like to sort the list in descending order so that the largest valued integer is highest in the list (index 0). I tried the following: [arr sortUsingSelector:@selector(compare:)]; but it did not seem to sort my values properly. Can someone show me code for properly doing what I am trying to accomplish? Thanks! It's pretty simple to write your own comparison

How do I reverse the order of objects in an NSMutableArray? [duplicate]

蹲街弑〆低调 提交于 2019-11-29 02:11:01
问题 This question already has an answer here: How can I reverse a NSArray in Objective-C? 18 answers I'm trying to achieve something like this NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@"A",@"B",@"C", nil]; NSLog(@"Array: %@", myArray); //logs A, B, C. //reverse code here NSLog(@"Array: %@", myArray); //logs C, B, A The code isn't exact, just a demo. But you get the idea. Any way to do this? 回答1: Simply use; NSArray* reversed = [[myArray reverseObjectEnumerator] allObjects]; The

Sort array of objects by their NSDate property [duplicate]

佐手、 提交于 2019-11-29 01:44:43
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How to sort an NSMutableArray with custom objects in it? How does one sort an array of objects by accessing their NSDate properties? I know one can use [mutableArray sortUsingSelector:@selector(compare:)]; on an array of dates, but how does one do it by accessing a NSDate property on each element in the array, sorting the objects by earliest date to latest date? 回答1: Thanks for all the answers, I came across

What is the KVC Search Pattern for mutableArrayValueForKey?

▼魔方 西西 提交于 2019-11-28 21:03:50
问题 I'm attempting to understand Cocoa's Key-Value Coding (KVC) mechanism a little better. I've read Apple's Key-Value Programming Guide but am still a little confused about how certain KVC methods search for keys. Particularly, mutableArrayValueForKey: . Below I'm going to explain how I understand valueForKey: KVC "getters" to work. Then I'll get to my question regarding mutableArrayValueForKey. There are seven different "getter" KVC methods: - (id)valueForKey:(NSString *)key; - (id

How can I sort strings in NSMutableArray into alphabetical order?

被刻印的时光 ゝ 提交于 2019-11-28 20:53:31
问题 I have a list of strings in an NSMutableArray , and I want to sort them into alphabetical order before displaying them in my table view. How can I do that? 回答1: There is the following Apple's working example, using sortedArrayUsingSelector: and localizedCaseInsensitiveCompare: in the Collections Documentation: sortedArray = [anArray sortedArrayUsingSelector: @selector(localizedCaseInsensitiveCompare:)]; Note that this will return a new sorted array. If you want to sort your NSMutableArray in

Storing CLLocationCoordinates2D in NSMutableArray

强颜欢笑 提交于 2019-11-28 18:52:11
After some searching, I got the following solution : reference . CLLocationCoordinate2D* new_coordinate = malloc(sizeof(CLLocationCoordinate2D)); new_coordinate->latitude = latitude; new_coordinate->longitude = longitude; [points addObject:[NSData dataWithBytes:(void *)new_coordinate length:sizeof(CLLocationCoordinate2D)]]; free(new_coordinate); And access it as: CLLocationCoordinate2D* c = (CLLocationCoordinate2D*) [[points objectAtIndex:0] bytes]; However, someone claims that there is a memory leak here? Can anyone suggest me where is the leak and how to fix it. Further, is there a better

how to add nil to nsmutablearray?

拥有回忆 提交于 2019-11-28 18:07:30
NSArray *array = [[NSArray alloc] initWithObjects:@"ΕΛΤΑ", @"ΕΛΤΑ COURIER", @"ACS", @"ACS ΕΞΩΤΕΡΙΚΟ", @"DHL", @"INTERATTICA", @"SPEEDEX", @"UPS", @"ΓΕΝΙΚΗ ΤΑΧΥΔΡΟΜΙΚΗ", @"ΜΕΤΑΦΟΡΙΚΕΣ ΕΞΩΤΕΡΙΚΟΥ", nil]; This is working because it has nil at the end. But I add objects like this: addObject:name etc... So at the end I have to add nil I do this addObhect:nil but when I run the app it still crashes at cellForRowAtIndexPath: how can I do this work? Ok, I dont have to add nil What is the reason that my app crashes then? mr-sk You can't add nil when you're calling addObject . Adrian Kosmaczewski If you

Static NSArray of strings - how/where to initialize in a View Controller

南楼画角 提交于 2019-11-28 16:39:21
In a Master-Detail app I'd like to display a TableView with 5 sections titled: Your Move Their Move Won Games Lost Games Options So I create a blank Master-Detail app in Xcode 5.0.2 and then in its MasterViewController.m (which is a UITableViewController) I'm trying to implement the method: - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return _titles[section]; } My question is however how to init the NSArray _titles? I'm trying in the MasterViewController.m: #import "MasterViewController.h" #import "DetailViewController.h" static NSArray *_titles

Join an Array in Objective-C

余生颓废 提交于 2019-11-28 16:26:32
I'm looking for a method of turning a NSMutableArray into a string. Is there anything on a par with this Ruby array method? >> array1 = [1, 2, 3] >> array1.join(',') => "1,2,3" Cheers! Jason Coco NSArray *array1 = [NSArray arrayWithObjects:@"1", @"2", @"3", nil]; NSString *joinedString = [array1 componentsJoinedByString:@","]; componentsJoinedByString: will join the components in the array by the specified string and return a string representation of the array. Rémy The method you are looking for is componentsJoinedByString . NSArray *a = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];/

Getting “mutating method sent to immutable object” error

大兔子大兔子 提交于 2019-11-28 14:20:57
I can't figure out what is causing this. Basically, a few different 'tasks' are colliding with each other in my app. When i press a button, it runs this code just fine: PalAppDelegate *dataCenter = (PalAppDelegate *)[[UIApplication sharedApplication] delegate]; [dataCenter.colourPalettesContainer addObject:[NSNumber numberWithInt:5]]; It can do this as many times as i like. But when i perform another task (and theres a few which cause this to happen), which involves this code for example: PalAppDelegate *dataCenter = (PalAppDelegate *)[[UIApplication sharedApplication] delegate]; [dataCenter