nsarray

Sorting negative and positive numbers in objective c

人走茶凉 提交于 2019-12-01 21:15:28
I am listing out a quantity percentage like stuff for an item via a webservice.The response that I get is an array of dictionaries similar to the below code.I need it in a sorted format NSArray *numberArray = [NSArray arrayWithObjects: [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:-12.0], @"price", nil], [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:-86.0],@"price", nil], [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:-8.0],@"price", nil], [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:12.0],@"price", nil

How to fill NSArray in compile time?

蓝咒 提交于 2019-12-01 18:13:36
In Objective-C, how to do something like is int array[] = {1, 2, 3, 4}; in pure C? I need to fill NSArray with NSStrings with the smallest overhead (code and/or runtime) as possible. It's not possible to create an array like you're doing at compile time. That's because it's not a "compile time constant." Instead, you can do something like: static NSArray *tArray = nil; -(void)viewDidLoad { [super viewDidLoad]; tArray = [NSArray arrayWithObjects:@"A", @"B", @"C", nil]; } If it's truly important that you have this precompiled, then I guess you could create a test project, create the array (or

How to tell if object is in NSArray?

泄露秘密 提交于 2019-12-01 16:55:24
Is there a way to tell if a certain object is in an NSArray? The way I am adding objects to my array makes it possible for the same object to be added multiple times and I wanted to see if there was a way to see if it was already there (anywhere) in that array. The NSArray containsObject: method is precisely for this purpose, its full signature being: - (BOOL)containsObject:(id)anObject See the full NSArray Class Reference docs for more information. if([yourArray indexOfObject:yourObject] == NSNotFound) { // your object is not in here } Edit : middaparkas approach is way better (if you don't

What is the difference in the usage of the methods firstObject vs objectAtIndex:0?

好久不见. 提交于 2019-12-01 15:15:43
Today I have tested using firstObject and objectAtIndex:0. If the array has the size of 0, using the former does not cause a crash while the latter causes a crash. So I'm thinking that it's better to use firstObject than objectAtIndex:0. But are there pitfalls in using firstObject over objectAtIndex:0? I have also been reading through the NSArray documentation and I am surprised and wondering why they did not mention this fact on the documentation. There is one key difference. Using firstObject returns nil if there is none. Using objectAtIndex:0 will crash your app(throws an exception) if

copy NSArray inside an empty NSArray

三世轮回 提交于 2019-12-01 14:42:34
I have a first NSArray firstArray and I do [firstArray removeAllObjects]; after I want fill it with the content of an other array secondArray is it right write in this way? firstArray = secondArray; No, firstArray = secondArray will reassign the pointers, this is not the behavior you want. You want [firstArray addObjectsFromArray:secondArray] Since the firstArray is an NSArray, you will want to do this instead: firstArray = [NSArray arrayWithArray:secondArray]; 来源: https://stackoverflow.com/questions/6344563/copy-nsarray-inside-an-empty-nsarray

objective-C, sort an array of strings containing numbers [duplicate]

故事扮演 提交于 2019-12-01 14:31:21
问题 This question already has answers here : How to do a natural sort on an NSArray? (5 answers) Closed 5 years ago . Lets suppose I have an array like this NSArray* arr = @[@"1",@"4",@"2",@"8",@"11",@"10",@"14",@"9"]; //note: strings containing numbers and I want to sort them like this: [1,2,4,8,9,10,11,14] but if I use [arr sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; I get [1,10,11,14,2,4,8,9]... and if I use: NSSortDescriptor *sortDescriptor = [[NSSortDescriptor

What is the difference in the usage of the methods firstObject vs objectAtIndex:0?

早过忘川 提交于 2019-12-01 14:16:47
问题 Today I have tested using firstObject and objectAtIndex:0. If the array has the size of 0, using the former does not cause a crash while the latter causes a crash. So I'm thinking that it's better to use firstObject than objectAtIndex:0. But are there pitfalls in using firstObject over objectAtIndex:0? I have also been reading through the NSArray documentation and I am surprised and wondering why they did not mention this fact on the documentation. 回答1: There is one key difference. Using

How is the code for separation of single array into two arrays?

江枫思渺然 提交于 2019-12-01 12:59:42
问题 I am getting below response into the nsarray when I used the JSON parsing from my required URL but here I don't like to get 2,1,4,4,6,5,8,7,10,9,12 and 11 in single array I have to get total response into two arrays I mean one array set will consists 2,4,6,8,10 and other array set must be 3,5,7,9 and 11. So how is the code for separation of single array response into two arrays in iPhone? "(\n 2,\n 1\n)", "(\n 4,\n 3\n)", "(\n 6,\n 5\n)", "(\n 8,\n 7\n)", "(\n 10,\n 9\n)", "(\n 12,\n 11\n)"

how to sort an NSArray of nested NSArrays by array count?

泪湿孤枕 提交于 2019-12-01 12:47:42
I have an NSArray that contains nested NSArrays. Im looking for a way to sort the parent array according to the object count of the nested arrays in ascending order. so if [array1 count] is 4, [array2 count] is 2 and [array3 count] is 9, i would get : array2, array1, array3... There are a few solutions, one of which is: NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"@count" ascending:YES]; NSArray *sds = [NSArray arrayWithObject:sd]; NSArray *sortedArray = [array sortedArrayUsingDescriptors:sds]; static NSInteger MONSortObjectsAscendingByCount(id lhs, id rhs, void* ignored) {

Check duplicate property values of objects in NSArray

寵の児 提交于 2019-12-01 12:33:47
I have an NSArray containing objects with a size property. How can I check if the NSArray has two objects with the same value for size ? Can I do something like: int i = 0; for (id item1 in myArray) { NSDecimalNumber *size1 = [item1 size]; for (id item2 in myArray) { NSDecimalNumber *size2 = [item2 size]; if ([size1 isEqual:size2]) { i ++; } } } if (i > [myArray count]) { NSLog(@"Duplicate Sizes Exist"); } Or is there an easier way? Amar Try this code: NSSet *myset = [NSSet setWithArray:[myarray valueForKey:@"size"]]; int duplicatesCount = [myarray count] - [myset count]; size here is the