nsarray

ios check if nsarray == null

左心房为你撑大大i 提交于 2019-11-30 09:31:41
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! Eliminate the warning using a cast: if (productIdList == (id)[NSNull null]) If productIdList is in

Compare two NSArrays and return number of differences

南楼画角 提交于 2019-11-30 09:18:40
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" 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]; [intermediate removeObjectsInArray:array2]; NSUInteger difference = [intermediate count]; With that way,

iOS how to debug crashes without a stack trace like : [__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array?

半腔热情 提交于 2019-11-30 09:12:04
I'm trying to dismiss a modal view controller and am getting the following error: * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' At one point I spent 4 hours trying to debug such error. Can anyone tell me if there's a way to look at stack traces for such errors to understand which object caused the incorrect access? Thank you! In Xcode 4 you can set an exception breakpoint in the breakpoint editor. Every time an exception is thrown (or caught depending on how you set it up), your application will

Reading plist file

谁说我不能喝 提交于 2019-11-30 09:08:56
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:@"plist"]]; for (int i = 0;[newArray objectAtIndex:i];i++) { if ([[newArray objectAtIndex:i]objectForKey

Sort NSArray of NSStrings like Addressbook on iphone sort

给你一囗甜甜゛ 提交于 2019-11-30 09:07:44
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? 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 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { return [(NSString*)obj1 compare:obj2 options

Evaluating an NSPredicate on a NSArray (without filtering)

爷,独闯天下 提交于 2019-11-30 07:16:43
Is it possible to evaluate a NSPredicate on a NSArray, without having the NSPredicate starting to filter out objects in the array? For instance, say I have the following predicate that just checks the number of objects in an array: NSPredicate *pred = [NSPredicate predicateWithFormat:@"count == 3"]; NSArray *list = [NSArray arrayWithObjects:@"uno", @"dos", @"volver", nil]; BOOL match = [pred evaluateWithObject:list]; This will crash, as pred will try to retrieve the "count" key from the first object in the array instead of the array itself. EmptyStack Use the SIZE operator of NSPredicate which

CLGeocoder Only Returning One Placemark

こ雲淡風輕ζ 提交于 2019-11-30 07:08:07
I have a problem with CLGeocoder where when I call geocodeAddressString:withCompletionHandler I only ever get one result back, despite knowing that the inputted string should return more than one value. The class reference even states: In the case of forward-geocoding requests, multiple placemark objects may be returned if the provided information yielded multiple possible locations. However, my placemarks array only ever has one item in it: [geocoderDestination geocodeAddressString:destination completionHandler:^(NSArray *placemarks, NSError *error){ NSLog(@"array count:%i", [placemarks count

Sorting NSArray which many contain number

喜夏-厌秋 提交于 2019-11-30 06:38:20
问题 I have an NSArray which is populated with objects from an NSMutableArray. Most of these object have integer values like "1", "2", "3", "4", "5", sometimes there is a name like "home", "far left", or "far right". I am trying to sort this array in Objective C. using sortedArrayUsingSelector:@selector(compare:) works fine when I have less then 10 items in the array. but when it there are more I start getting "1", "10", "11", "12", "2", "3" type of stuff. Any help would be most appreciated. The

removing duplicates in nsarray

冷暖自知 提交于 2019-11-30 06:38:16
问题 how can i remove duplicates in nsarray. for instance my array contains following data. I want to compare with adjacent dates to avoid duplicates but it throughs error. Can anyone guide me what i am going wrong calendar first- ( 2010-09-25 17:00:00 GMT, "AAA", 2010-09-25 17:00:00 GMT, "AAA", 2010-09-26 17:00:00 GMT, "BBB", 2010-09-26 17:00:00 GMT, "BBB", 2010-09-27 17:00:00 GMT, "CCCC", 2010-09-27 17:00:00 GMT, "CCC", 2010-09-28 17:00:00 GMT, "AAA", 2010-09-28 17:00:00 GMT, "AAA", 2010-09-29

Join an Array in Objective-C

风格不统一 提交于 2019-11-30 06:16:13
问题 I'm looking for a method of turning a NSMutableArray into a string. Is there anything on a par with this Ruby array method? >> array1 = [1, 2, 3] >> array1.join(',') => "1,2,3" Cheers! 回答1: NSArray *array1 = [NSArray arrayWithObjects:@"1", @"2", @"3", nil]; NSString *joinedString = [array1 componentsJoinedByString:@","]; componentsJoinedByString: will join the components in the array by the specified string and return a string representation of the array. 回答2: The method you are looking for