nsarray

Finding maximum numeric value in NSArray

微笑、不失礼 提交于 2019-11-30 06:12:09
问题 I have an NSArray of NSNumbers and want to find the maximum value in the array. Is there any built in functionality for doing so? I am using iOS4 GM if that makes any difference. 回答1: The KVC approach looks like this: int max = [[numbers valueForKeyPath:@"@max.intValue"] intValue]; or NSNumber * max = [numbers valueForKeyPath:@"@max.intValue"]; with numbers as an NSArray 回答2: NSArray * test= @[@3, @67, @23, @67, @67]; int maximumValue = [[test valueForKeyPath: @"@max.self"] intValue]; NSLog(@

Use NSArray to specify otherButtonTitles?

不羁岁月 提交于 2019-11-30 05:34:27
UIAlertSheet's constructor takes an otherButtonTitles parameter as a varg list. I'd like to specify the other button titles from an NSArray instead. Is this possible? i.e. I have to do this: id alert = [[UIActionSheet alloc] initWithTitle: titleString delegate: self cancelButtonTitle: cancelString destructiveButtonTitle: nil otherButtonTitles: button1Title, button2Title, nil]; But since I'm generating the list of available buttons at runtime, I really want something like this: id alert = [[UIActionSheet alloc] initWithTitle: titleString delegate: self cancelButtonTitle: cancelString

NSPredicate subquery syntax

老子叫甜甜 提交于 2019-11-30 05:27:12
I have sort of an unfriendly array of dictionaries that in turn have arrays of data and I am trying to filter the outer array based on any of the inner array passing a predicate. I can't seem to create an NSPredicate to make this work. I started off with: NSPredicate *lookupPredicate = [NSPredicate predicateWithFormat: @"row_values.property_id == %@ AND row_values.property_value == %@", @"47cc67093475061e01000540", @"Male"]; [dataRows filterUsingPredicate:lookupPredicate]; This returns no values. I've tried various forms of ANY but I can't seem to find anything that it will parse. Again, the

NSDictionary, NSArray, NSSet and efficiency

我的梦境 提交于 2019-11-30 05:25:12
I've got a text file, with about 200,000 lines. Each line represents an object with multiple properties. I only search through one of the properties (the unique ID) of the objects. If the unique ID I'm looking for is the same as the current object's unique ID, I'm gonna read the rest of the object's values. Right now, each time I search for an object, I just read the whole text file line by line, create an object for each line and see if it's the object I'm looking for - which is basically the most inefficient way to do the search. I would like to read all those objects into memory, so I can

Fastest way to check if an array contains the same objects of another array

◇◆丶佛笑我妖孽 提交于 2019-11-30 04:38:43
The goal is to compare two arrays as and check if they contain the same objects (as fast as possible - there are lots of objects in the arrays). The arrays cannot be checked with isEqual: as they are differently sorted. I already tried the solution posted here ( https://stackoverflow.com/a/1138417 - see last code snippet of the post by Peter Hosey). But this doesn't work with differently sorted arrays. The code I'm using now is the following: + (BOOL)arraysContainSameObjects:(NSArray *)array1 andOtherArray:(NSArray *)array2 { // quit if array count is different if ([array1 count] != [array2

UITableView Section Headers by month of dates in array

穿精又带淫゛_ 提交于 2019-11-30 04:13:53
I have an NSArray with something similar to: 6/1/13 | Data 6/2/13 | Data 7/1/13 | Data 9/1/13 | Data What I need to somehow get the months to create section headers - but only if they are in the array and then break the dates up into the appropriate sections. Looking like: (Section Header)June 2013 6/1/13 | Data 6/2/13 | Data (Section Header)July 2013 7/1/13 | Data (skips august as no dates from august are in array) (Section Header)September 2013 9/1/13 | Data I am attempting to implement: - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return @

Is there an easy way to iterate over an NSArray backwards?

一个人想着一个人 提交于 2019-11-30 04:09:13
I've got an NSArray and have to iterate over it in a special case backwards, so that I first look at the last element. It's for performance reasons: If the last one just makes no sense, all previous ones can be ignored. So I'd like to break the loop. But that won't work if I iterate forward from 0 to n. I need to go from n to 0. Maybe there is a method or function I don't know about, so I wouldn't have to re-invent the wheel here. To add on the other answers, you can use -[NSArray reverseObjectEnumerator] in combination with the fast enumeration feature in Objective-C 2.0 (available in Leopard

Sorting Array in increasing order

萝らか妹 提交于 2019-11-29 23:27:02
问题 I have an array that contains values like 0,3,2,8 etc.I want to sort my array in increasing order.Please tell me how to do this. Thanks in advance!! 回答1: If your array is an NSArray containing NSNumbers: NSArray *numbers = [NSArray arrayWithObjects: [NSNumber numberWithInt:0], [NSNumber numberWithInt:3], [NSNumber numberWithInt:2], [NSNumber numberWithInt:8], nil]; NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:@selector(localizedCompare:

NSArray arrayWithObjects: if nil is meant to mark array end, can I do …nil, nil]?

我的梦境 提交于 2019-11-29 22:25:20
问题 If nil is meant to mark the end of parameters, then can I use: [NSArray arrayWithObjects:obj1, obj2, nil, nil, nil]; as the first nil marks array end and two nils after will be ignored? I got two opposite answers and will try it out. update: The reason why I need to do this, is I need to create an UIAlertView, which may have buttons: 'OK' only, or 'Call' and 'Cancel', so either 'OK' or 'Cancel', is the cancel button, whereas 'Call' is other buttons in one case, in other cases, I do not need

Filtering NSArray of NSDictionary objects using NSPredicate

人走茶凉 提交于 2019-11-29 21:56:54
I have an NSArray of NSDictionary objects. I want to filter the array based on keys of the dictionaries using NSPredicate. I have been doing something like this: NSString *predicateString = [NSString stringWithFormat:@"%@ == '%@'", key, value]; NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString]; NSArray *filteredResults = [allResultsArray filteredArrayUsingPredicate:predicate]; This works fine if they key passed in is one-word: Color, Name, Age. But it doesn't work if the key is multi-word, like: Person Age, Person Name. Basically, any key that contains a space, it