nsmutablearray

Removing object from NSMutableArray

[亡魂溺海] 提交于 2019-11-26 22:25:19
问题 I stumbled across the following shortcut in setting up a for loop (shortcut compared to the textbook examples I have been using): for (Item *i in items){ ... } As opposed to the longer format: for (NSInteger i = 0; i < [items count]; i++){ ... } //think that's right If I'm using the shorter version, is there a way to remove the item currently being iterated over (ie 'i')? Or do I need to use the longer format? 回答1: You cannot remove objects from array while fast-enumerating it: numeration is

Finding Intersection of NSMutableArrays

余生长醉 提交于 2019-11-26 22:20:50
I have three NSMutableArray containing names that are added to the lists according to different criterieas. Here are my arrays pseudocode: NSMutableArray *array1 = [@"Jack", @"John", @"Daniel", @"Lisa"]; NSMutableArray *array2 = [@"Jack", @"Bryan", @"Barney", @"Lisa",@"Penelope",@"Angelica"]; NSMutableArray *array3 = [@"Jack", @"Jerome", @"Dan", @"Lindsay", @"Lisa"]; I want to find a fourth array which includes the intersection of those three arrays. In this case for example it will be: NSMutableArray *array4 = [@"Jack",@"Lisa"]; Because all the three array have jack and lisa as an element. Is

How to add properties to NSMutableArray via category extension?

拜拜、爱过 提交于 2019-11-26 21:53:34
问题 I know that I can "subclass" an NSMutableArray using "category extensions," i.e. @interface NSMutableArray (MyExtension) , to add new functions to the class. However, is there a way using category extensions to also add new properties to the extension? 回答1: Note that @interface NSMutableArray (MyStuff) is a category and not a class extension . They are similar in this context, but actually do have quite a few different details. You can't add storage to an existing class through either

How to split an NSArray into two equal pieces?

一笑奈何 提交于 2019-11-26 21:32:07
问题 I have an NSArray, and I want to split it into two equal pieces (if odd "count" then add to the latter new array) - I want to split it "down the middle" so to speak. The following code does exactly what I want, but is there a better way?: // NOTE: `NSArray testableArray` is an NSArray of objects from a class defined elsewhere; NSMutableArray *leftArray = [[NSMutableArray alloc] init]; NSMutableArray *rightArray = [[NSMutableArray alloc] init]; for (int i=0; i < [testableArray count]; i=i+1) {

Should I subclass the NSMutableArray class

北慕城南 提交于 2019-11-26 20:52:44
问题 I have an NSMutableArray object that I want to add custom methods to. I tried subclassing NSMutableArray but then I get an error saying "method only defined for abstract class" when trying to get the number of objects with the count method. Why is the count method not inherited? I read somewhere else that I will have to import some NSMutableArray methods into my custom class if I want to use them. I just want to add a custom method to the NSMutableArray class. So should I subclass

Dictionary in Swift with Mutable Array as value is performing very slow? How to optimize or construct properly?

人走茶凉 提交于 2019-11-26 20:27:12
I am trying to build a data structure in Swift that maps an Integer to an array of objects (a dictionary with int as key and array as value). The objects are extremely small, and they simply wrap a UIColor and an Int. I have two implementations one that uses a Swift array as the Dictionary's value type, while the other uses a NSMutableArray as the value type. My objective-C code performs extremely fast, but my Swift code is running egregiously slow. Ideally, I would not like to use an NSMutableArray, and would like to keep it as a Swift array. Reason for this is I am writing an algorithm and

How do copy and mutableCopy apply to NSArray and NSMutableArray?

只谈情不闲聊 提交于 2019-11-26 19:30:30
What is the difference between copy and mutableCopy when used on either an NSArray or an NSMutableArray ? This is my understanding; is it correct? // ** NSArray ** NSArray *myArray_imu = [NSArray arrayWithObjects:@"abc", @"def", nil]; // No copy, increments retain count, result is immutable NSArray *myArray_imuCopy = [myArray_imu copy]; // Copys object, result is mutable NSArray *myArray_imuMuta = [myArray_imu mutableCopy]; // Both must be released later // ** NSMutableArray ** NSMutableArray *myArray_mut = [NSMutableArray arrayWithObjects:@"A", @"B", nil]; // Copys object, result is immutable

JSON Parsing in iOS 7

荒凉一梦 提交于 2019-11-26 18:59:43
问题 I am creating an app for as existing website. They currently has the JSON in the following format : [ { "id": "value", "array": "[{\"id\" : \"value\"} , {\"id\" : \"value\"}]" }, { "id": "value", "array": "[{\"id\" : \"value\"},{\"id\" : \"value\"}]" } ] which they parse after escaping the \ character using Javascript. My problem is when i parse it in iOS using the following command : NSArray *result = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&localError];

Is Objective-C's NSMutableArray thread-safe?

蹲街弑〆低调 提交于 2019-11-26 18:56:40
问题 I've been trying to fix this crash for almost a week. The application crashes without any exception or stack-trace. The application does not crash in any way while running through instruments in zombie mode. I have a method that gets called on a different thread. The solution that fixed the crash was replacing [self.mutableArray removeAllObjects]; with dispatch_async(dispatch_get_main_queue(), ^{ [self.searchResult removeAllObjects]; }); I thought it might be a timing issue, so I tried to

How do I convert NSMutableArray to NSArray?

拟墨画扇 提交于 2019-11-26 18:45:25
问题 How do I convert NSMutableArray to NSArray in objective-c? 回答1: NSArray *array = [mutableArray copy]; Copy makes immutable copies. This is quite useful because Apple can make various optimizations. For example sending copy to a immutable array only retains the object and returns self . If you don't use garbage collection or ARC remember that -copy retains the object. 回答2: An NSMutableArray is a subclass of NSArray so you won't always need to convert but if you want to make sure that the array