nsarray

Changing the sort order of -[NSArray sortedArrayUsingComparator:]

可紊 提交于 2019-11-29 16:47:25
问题 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; }];

convert std:vector to NSArray

你。 提交于 2019-11-29 16:38:36
问题 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? 回答1: 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. 回答2: Yes, you can

NSArray - check if objects are in an array?

这一生的挚爱 提交于 2019-11-29 16:07:31
I have 2 arrays. One is a large static group of 600 objects, the other is a small varying group of 10 objects. I want to take any common objects between the two groups and put them in a new array. So lets say the large group contains 600 objects named 1 to 600. The smaller group contains 9 objects: 1, 2, 3, 4, 5, 6, a, b, c. I want to be able to create a new array that contains the objects 1, 2, 3, 4, 5, 6. What is the best way to do this? Are you sure that you need NSArray s? For intersections it would be better to use NSSet s. For more information about the usage of NSArrays and NSSet please

Why can I send messages to a deallocated instance of NSArray?

懵懂的女人 提交于 2019-11-29 15:53:15
I just noticed a surprising behavior of NSArray , that's why I'm posting this question. I just added a method like: - (IBAction) crashOrNot { NSArray *array = [[NSArray alloc] init]; array = [[NSArray alloc] init]; [array release]; [array release]; } Theoretically this code will crash. But In my case it never crashed !!! I changed the NSArray with NSMutableArray but this time the app crashed. Why this happens, why NSArray not crashing and NSMutableArray crashes ? In general, when you deallocate an object the memory is not zeroed out, it’s just free to be reclaimed by whoever needs it.

Getting the string value from a NSArray

旧城冷巷雨未停 提交于 2019-11-29 15:08:24
问题 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(@"%@",

Sort NSArray using sortedArrayUsingFunction

蓝咒 提交于 2019-11-29 14:22:35
Any good example of sorting a NSArray using sortedArrayUsingFunction ? TY! NSArray *sorted_bookings = [myUnsortedArray sortedArrayUsingFunction:Sort_Bookingdate_Comparer context:self]; NSInteger Sort_Bookingdate_Comparer(id id1, id id2, void *context) { // Sort Function Booking* booking1 = (Booking*)id1; Booking* booking2 = (Booking*)id2; return ([booking1.BOOKING_DATE compare:booking2.BOOKING_DATE]); } This I used to sort bookings by bookingdate. Booking is a class with a synthesized instance variable called BOOKING_DATE. As stated in the documentation: http://developer.apple.com/library/mac/

Compare two NSArrays and return number of differences

有些话、适合烂在心里 提交于 2019-11-29 14:09:32
问题 How can I take two NSArrays, compare them, then return the number of differences, preferably the number of different objects, for example: Array 1: one two three Array 2: two four one I would like that to return "1" 回答1: You can do this by using an intermediate NSMutableArray: NSArray *array1 = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil]; NSArray *array2 = [NSArray arrayWithObjects:@"Two", @"Four", @"One", nil]; NSMutableArray *intermediate = [NSMutableArray arrayWithArray:array1

ios check if nsarray == null

こ雲淡風輕ζ 提交于 2019-11-29 13:49:43
问题 I'm receiving some response from JSON , and is working fine, but I need to check for some null values, I have found different answers but seems is not working still, NSArray *productIdList = [packItemDictionary objectForKey:@"ProductIdList"]; I have tried with if ( !productIdList.count ) //which breaks the app, if ( productIdList == [NSNull null] ) // warning: comparison of distinct pointer types (NSArray and NSNull) So what is happening? How to fix this and check for null in my array? Thanks

Sort NSArray of NSStrings like Addressbook on iphone sort

…衆ロ難τιáo~ 提交于 2019-11-29 13:41:48
问题 I have an array of strings (names) and i would like to sort them like how the address book on the iphone sorts them eg: éli -> under E eg: àli -> under A eg: 4li -> under # any suggestions? 回答1: You need to perform a diacritic insensitive comparison against the strings. NSString has a compare:options: method with an option of NSDiacriticInsensitiveSearch . NSArray *array = [NSArray arrayWithObjects:@"éli", @"bob", @"earl", @"allen", @"àli", @"aaron", nil]; NSArray *sorted = [array

Reading plist file

蓝咒 提交于 2019-11-29 13:33:20
问题 I created plist with big base of different animals in it (keys) and added this plist in my project. Type of every animal is NSDictionary. In every dictionary there are 5 more keys - characteristics of these animals (). I would like to go through every animal and if I match a certain characteristics of this animal, I put this animal in another array. So, I tried to make a code: NSArray *newArray = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"animals" ofType:@