nsarray

Sorting NSArray based on other Array with custom classes

此生再无相见时 提交于 2019-12-20 03:47:49
问题 I desperately need to sort an array: heres the situation, I need to rearrange / sort and replace arrays based on other an object class from another array. ParentClass :NSObject{ NSString *name; NSNumber *type; } This is the parent class, which fills up the parentArray parentArray = { parentA, parentB, parentC } This is another object class, it has ParentClass *parent. @class ParentClass; EntryClass: NSObject{ NSString *name; NSNumber *number; ParentClass *parent; } Now I have an array

Sorting negative and positive numbers in objective c

泪湿孤枕 提交于 2019-12-20 02:43:27
问题 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],@

NSDictionary split into two arrays (objects and keys) and then sorted both by the objects array (or a similar solution)

懵懂的女人 提交于 2019-12-20 02:27:17
问题 I have an NSDictionary. It has keys and objects. For the purposes of simplicity the keys are Question numbers and the objects are calculated Answer scores. Now how I did it before was that I set the answer score as the keys and the question numbers as the objects. This way I could get an array of allKeys from the dictionary, sort it and then do something similar to: for(NSString *string in tempArray){ NSLog(@"%@",[dictionary objectForKey:string]); } The (stupid - on my part) problem that I

How to tell if object is in NSArray?

£可爱£侵袭症+ 提交于 2019-12-19 16:12:19
问题 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. 回答1: 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. 回答2: if([yourArray indexOfObject

how to convert NSString to NSArray [duplicate]

怎甘沉沦 提交于 2019-12-19 12:21:23
问题 This question already has answers here : Comma-separated string to NSArray in Objective-C (2 answers) Closed 5 years ago . I have a string like NSString* str = @"[90, 5, 6]"; I need to convert it to an array like NSArray * numbers = [90, 5 , 6]; I did a quite long way like this: + (NSArray*) stringToArray:(NSString*)str { NSString *sep = @"[,"; NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:sep]; NSArray *temp=[str componentsSeparatedByCharactersInSet:set];

How to add all decimal numbers in an NSMutableArray

霸气de小男生 提交于 2019-12-19 11:34:51
问题 I have a NSMutableArray which have some NSDecimalNumber in it, like (500,50.80,70,8000) Now I want to add all those decimal numbers together. I've tried to use for (NSDecimalNumber *number in self.numbersArray) { NSDecimal *sum += [number decimalValue] } But failed. 回答1: Use - (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber Take a look at NSDecimalNumber Class Reference NSDecimalNumber *lNumber = [NSDecimalNumber zero]; for (NSDecimalNumber *number in self

How to add all decimal numbers in an NSMutableArray

孤街醉人 提交于 2019-12-19 11:34:37
问题 I have a NSMutableArray which have some NSDecimalNumber in it, like (500,50.80,70,8000) Now I want to add all those decimal numbers together. I've tried to use for (NSDecimalNumber *number in self.numbersArray) { NSDecimal *sum += [number decimalValue] } But failed. 回答1: Use - (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber Take a look at NSDecimalNumber Class Reference NSDecimalNumber *lNumber = [NSDecimalNumber zero]; for (NSDecimalNumber *number in self

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

别等时光非礼了梦想. 提交于 2019-12-19 10:55:10
问题 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... 回答1: There are a few solutions, one of which is: NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"@count" ascending:YES]; NSArray *sds = [NSArray arrayWithObject:sd]; NSArray *sortedArray = [array

Objective-C code (array.indexOfObjectPassingTest) to Swift

▼魔方 西西 提交于 2019-12-19 10:23:11
问题 How can I use the Objective-C code below in Swift, I tried but something is wrong. Objective-C: NSUInteger index = [theArray indexOfObjectPassingTest: ^BOOL(NSDictionary *dict, NSUInteger idx, BOOL *stop) { return [[dict objectForKey:@"name"] isEqual:theValue]; } ]; Swift (Doesn't work): let index = theArray.indexOfObjectPassingTest { (var dict: NSDictionary, var ind: Int, var bool: Bool) -> Bool in return dict.objectForKey("name")?.isEqual("theValue") } 回答1: I played with it and got this to

Get data from a PList into UITableView?

不羁岁月 提交于 2019-12-19 10:13:14
问题 I want to maintain a list of records, for each one I maintain the same type of data. I want to use this data in 2 different places: UITableView that takes from each record the "Name" value UIViewController that takes all the data to use in different fields. I assume I should be using a plist to store the data; I also assume that the object that should be receiving the data for the UITableView is NSArray so I can use the cellForRowAtIndexPath method to create the table automatically. So i