nsarray

sorting mutable array in alphabetical order

荒凉一梦 提交于 2019-12-09 23:12:55
问题 I am having a nsmutable array and in that nearly 50 -60 object having different names ,and can i sort this array in alphabetical order (Is it possible, How?) 回答1: For a simple sort like this, I like to use sort descriptors. Suppose you have an mutable array of objects whose class has a name NSString property: NSSortDescriptor *sort=[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:NO]; [myArray sortUsingDescriptors:[NSArray arrayWithObject:sort]]; 回答2: Absolutely, you can use

How to get first letter of all strings in an array in iOS Swift?

我的未来我决定 提交于 2019-12-09 15:57:20
问题 I have a group of strings stored in an array. stringArray = [Aruna, Bala, Chitra, Divya, Fatima] I want to get the first letters of all the strings and store it in an array. Like: letterArray = [A, B, C, D, F] Note: They are not within quotes "--" 回答1: Not sure what you mean by 'they are not within quotes', but if they are actually Strings then something like this: var letterArray = [Character]() for string in stringArray { letterArray.append(string.characters.first!) } EDIT To have it as

NSPredicate 'OR' filtering based on an NSArray of keys

谁都会走 提交于 2019-12-09 10:26:57
问题 Consider the following NSArray: NSArray *dataSet = [[NSArray alloc] initWithObjects: [NSDictionary dictionaryWithObjectsAndKeys:@"abc", @"key1", @"def", @"key2", @"hij", @"key3", nil], [NSDictionary dictionaryWithObjectsAndKeys:@"klm", @"key1", @"nop", @"key2", nil], [NSDictionary dictionaryWithObjectsAndKeys:@"qrs", @"key2", @"tuv", @"key4", nil], [NSDictionary dictionaryWithObjectsAndKeys:@"wxy", @"key3", nil], nil]; I am able to filter this array to find dictionary objects that contain the

Differences between [NSArray arrayWithArray:] and [NSArray copy]

我的梦境 提交于 2019-12-09 08:21:40
问题 lately I work much with arrays and I'm wonder.. what's diffrences between those two lines. NSArray *array = [NSArray arrayWithArray:someArray]; and NSArray *array = [someArray copy]; Which of it is faster? What in case we have NSMutableArray and mutableCopy ? 回答1: Which of it is faster? Don't worry about it. Premature optimization. The main difference: the first approach results in an autoreleased "copy" that you don't own and don't have to release, while you do own the object created on the

Memory leak with a lot of big images in iPad

空扰寡人 提交于 2019-12-09 08:10:29
I'm trying to store UIImage datas in NSArray , actually 60 images, each with size of 300kb. Then, I'm trying to animate that images in UIImageView . My code: NSMutableArray *arr = [[NSMutableArray alloc] init]; for (int i=0; i<61; ++i) { [arr addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%i.png", i]]]; } img.animationImages = arr; img.animationDuration = 1.5; img.contentMode = UIViewContentModeBottomLeft; [img startAnimating]; When I test it in iPad Simulator 4.3, it works fine. When I want to test it on my device (iPad 1), application crashes . Note: App does not crashes if I

CALayerArray was mutated while being enumerated

纵然是瞬间 提交于 2019-12-09 07:55:18
问题 I have to delete an object of an array every now and then, when I do it I get this error. Collection < CALayerArray: 0xc4f3b20> was mutated while being enumerated The error appears on this method, which is the accesor of the Array: - (NSArray *)occupantsArray { if (dispatch_get_current_queue() == moduleQueue) { return occupantsArray; } else { __block NSArray *result; dispatch_sync(moduleQueue, ^{ //ON THIS LINE result = [occupantsArray copy]; }); return [result autorelease]; } } As you can

How get the total sum of NSNumber's from a NSArray?

杀马特。学长 韩版系。学妹 提交于 2019-12-09 07:41:34
问题 I have a large NSArray containing NSNumbers like 3, 4, 20, 10, 1, 100, etc... How do I get the total sum of all these NSNumbers (3 + 4 + 20 + 10 + 1 + 100 + etc...) as one total NSInteger ? Thank you! 回答1: NSInteger sum = 0; for (NSNumber *num in myArray) { sum += [num intValue]; } 回答2: You can use this: NSArray* numbers = //array of numbers NSNumber* sum = [numbers valueForKeyPath: @"@sum.self"]; 回答3: long long sum = ((NSNumber*)[array valueForKeyPath: @"@sum.longLongValue"]).longLongValue;

NSArray @property backed by a NSMutableArray

亡梦爱人 提交于 2019-12-09 07:35:05
问题 I've defined a class where I'd like a public property to appear as though it is backed by an NSArray . That is simple enough, but in my case the actual backing ivar is an NSMutableArray : @interface Foo { NSMutableArray* array; } @property (nonatomic, retain) NSArray* array; @end In my implementation file ( *.m ) I @synthesize the property but I immediately run into warnings because using self.words is the same as trying to modifying an NSArray . What is the correct way to do this? Thanks!

Is there any way to monkey-patch or swizzle an NSArray (or other Class Cluster)?

佐手、 提交于 2019-12-09 07:12:59
问题 Today I was working on a project in which I wanted to "alias" an alternative method for all instances of NSArray , and didn't think it would be too difficult with some good old-fashioned method swizzling. I broke out JRSwizzle and… [NSArray jr_swizzleMethod:@selector(objectAtIndex:) withMethod:@selector(objectAtIndex_accordingToMe:) error:nil]; To be clear, I paired this with the appropriate category on NSArray , an instance method called objectAtIndex_accordingToMe: . However, I was just

Convert JSON to NSArray

ⅰ亾dé卋堺 提交于 2019-12-09 06:07:40
问题 I have a JSON array being output on a page. I would like to convert this JSON array into an NSArray. This is the JSON output on the web page: [{"city":"Current Location"},{"city":"Anaheim, CA"},{"city":"Los Angeles, CA"},{"city":"San Diego, CA"},{"city":"San Francisco, CA"},{"city":"Indianapolis, IN"}] This is my NSURL call and NSJSONSerialization: NSString *cityArrayURL = [NSString stringWithFormat:@"http://site/page.php"; NSData *cityData = [NSData dataWithContentsOfURL:[NSURL URLWithString