nsarray

sorting an NSArray of NSDates

◇◆丶佛笑我妖孽 提交于 2019-11-30 12:14:50
I have an NSArray of NSDate objects and I want to sort them so that today is at 0, yesterday at 1 etc. Is it ascending or descending, and do i use a function, selector or what? There are different sort methods for NSArray because there may be different ways you want to sort things. NSSortDescriptors are a general way that give you a lot of options as far as what keys to use in sorting, what selectors you want to use on those keys, and overall order to use, etc. Or you can use functions or comparator blocks instead if your case requires that or if that's more convenient for your particular case

Print array in objective-c?

不打扰是莪最后的温柔 提交于 2019-11-30 11:42:56
I need to check my values from NSArray which are stored dynamically. How should I print the array values in Objective-C? NSLog(@"%@",yourArray); This actually calls the description method of your NSArray and prints it to the log. http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/description 来源: https://stackoverflow.com/questions/3396336/print-array-in-objective-c

How retrieve an index of an NSArray using a NSPredicate?

回眸只為那壹抹淺笑 提交于 2019-11-30 11:31:30
I would know how retrieve an index of an NSArray using a NSPredicate ? NSArray *array = [NSArray arrayWithObjects: @"New-York City", @"Washington DC", @"Los Angeles", @"Detroit", nil]; Wich kind of method should I use in order to get the index of "Los Angles" by giving only a NSString NB: @"Los An" or @"geles" should return the same index.. Using NSPredicate you can get array of strings that contain your search string (it seems there's no built-in method to get just element indexes): NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", searchString]; NSArray

convert std:vector to NSArray

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 11:25:05
Is there a good way to convert a vector<int32_t> to an NSArray of NSNumber or is looping and adding to an NSMutableArray pretty much the only way? If you have a vector of objects, you can do the following: NSArray *myArray = [NSArray arrayWithObjects:&vector[0] count:vector.size()]; However, if your vector contains primitive types, such as NSInteger , int , float , etc., you would have to manually loop the values in the vector and convert them to a NSNumber first. Yes, you can create an NSArray of NSInteger s from a std::vector<NSInteger> by using the following approach: // thrown together as

Changing the sort order of -[NSArray sortedArrayUsingComparator:]

风流意气都作罢 提交于 2019-11-30 11:09:28
I need to generate a sorted array of NSNumber s from another NSArray . I want to use the sortedArrayUsingComparator: method. I found this example in the Apple documentation: NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) { if ([obj1 integerValue] > [obj2 integerValue]) { return (NSComparisonResult)NSOrderedDescending; } if ([obj1 integerValue] < [obj2 integerValue]) { return (NSComparisonResult)NSOrderedAscending; } return (NSComparisonResult)NSOrderedSame; }]; Here I am just providing the comparator in a block, which is fine. The sort itself -- the bit that's

How to turn an NSArray of strings into an array of unique strings, in the same order?

自古美人都是妖i 提交于 2019-11-30 10:39:36
问题 If you have an NSArray of strings { @"ONE", @"ONE", @"ONE", "TWO", @"THREE", @"THREE" } How would I turn that into { @"ONE", @"TWO", @"THREE" } ..where the array follows the same order as the original. I think that you can turn an array into an NSSet to get unique items, but if you turn it back into an array you are not guaranteed to get the same order.. 回答1: My initial thought was that you could do: NSArray * a = [NSArray arrayWithObjects:@"ONE", @"ONE", @"ONE", @"TWO", @"THREE", @"THREE",

Get all keys of an NSDictionary as an NSArray

て烟熏妆下的殇ゞ 提交于 2019-11-30 10:36:44
问题 Is it possible to get all the keys from a specific NSDictionary as a seperate NSArray ? 回答1: Just use NSArray*keys=[dict allKeys]; In general, if you wonder if a specific class has a specific method, look up Apple's own documentation . In this case, see NSDictionary class reference. Go through all the methods. You'll discover many useful methods that way. 回答2: Yes it's possible. Use allKeys method: NSDictionary *yourDictionary; NSArray * yourKeys yourKeys = [yourDictionary allKeys]; 回答3: And

Sorting NSMutableArray using SortDescriptor AND Predicate possible?

こ雲淡風輕ζ 提交于 2019-11-30 10:01:57
I have an array of type "Restaurant" which has an NSSet of "Rating." Rating has an ID and a value. I want to sort the array of Restaurant's by rating with an ID of 01, from high to low. Something like the following, but how do I use the predicate and sort descriptor together on originalArray? NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Rating.rating_id=%d", ratingID]; NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"currentValue" ascending:YES]; NSArray *sorted = [[originalArray allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]

Getting the string value from a NSArray

倾然丶 夕夏残阳落幕 提交于 2019-11-30 09:55:26
I have a NSArrayController and I when I get the selectedObjects and create a NSString with the value of valueForKey:@"Name" it returns ( "This is still a work in progress " ) and all I want to have is the text in the "" how would I get that? also, this my code: NSArray *arrayWithSelectedObjects = [[NSArray alloc] initWithArray:[arrayController selectedObjects]]; NSString *nameFromArray = [NSString stringWithFormat:@"%@", [arrayWithSelectedObjects valueForKey:@"Name"]]; NSLog(@"%@", nameFromArray); Edit : I also have other strings in the array When you call valueForKey: on an array, it calls

iOS sort an NSArray of Time NSString's

我只是一个虾纸丫 提交于 2019-11-30 09:53:07
I need to sort an NSArray containing time NSString 's such as, NSMutableArray *times = [[NSMutableArray alloc]initWithObjects:@"09:00 AM",@"07:30 AM",@"06:45 PM",@"05:00 PM",@"12:45 AM",@"12:45 PM",@"01:00 AM",@"01:15 PM", nil]; What I need is to sort the array in ascending order of time. Is there any way to do such a thing? NSMutableArray *times = [[NSMutableArray alloc]initWithObjects:@"09:00 AM",@"07:30 AM",@"06:45 PM",@"05:00 PM",@"12:45 AM",@"12:45 PM",@"01:00 AM",@"01:15 PM", nil]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"hh:mm a"];