nsmutablearray

How to split an NSArray into two equal pieces?

冷暖自知 提交于 2019-11-27 23:36:58
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) { if (i < [testableArray count]/2) { [leftArray addObject:[testableArray objectAtIndex:i]]; } else {

Should I subclass the NSMutableArray class

陌路散爱 提交于 2019-11-27 22:00:32
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 NSMutableArray, or should I do something else? PeyloW NSMutableArray is not a concrete class, it is just the

How do you store “int” values in an NSMutableArray* or NSMutableDictionary*? Chronic problems with JSON data that come in as integers.

纵饮孤独 提交于 2019-11-27 21:50:49
问题 How do you store "int" values in an NSMutableArray or NSMutableDictionary ? Chronic problems with JSON data that come in as integers. Should I try to store these integers as NSNumber objects or just as strings that contain the integer? How dangerous is it to do a "raw" (int) conversion on the pointers if I know the values will always be Natural Numbers (numbers>=0 including zero for null.) The integers I'm always working with are Foreign Key Id's from a database. 回答1: Use NSNumber like this:

Swift - Cast Int64 to AnyObject for NSMutableArray

核能气质少年 提交于 2019-11-27 19:25:22
问题 Hi I have a NSMutableArray and I try this: var ma = NSMutableArray() let number:Int64 = 8345834344 ma.addObject(number)// Error "Type Int64 does not conform to protocol AnyObject" How to add Int64 variable to NSMutableArray() ? 回答1: You are using a Foundation array (NSMutableArray), so you should use a Foundation number object: ma.addObject(NSNumber(longLong:number)) You could also use a native swift array: var ma = [Int64]() ma.append(number) 回答2: Like so much of Swift, this is implemented

JSON Parsing in iOS 7

岁酱吖の 提交于 2019-11-27 17:28:00
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]; and do this : NSArray *Array = [result valueForKey:@"array"]; Instead of an Array I got NSMutableString

Is Objective-C's NSMutableArray thread-safe?

别说谁变了你拦得住时间么 提交于 2019-11-27 17:20:07
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 synchronize it, but it still crashed: @synchronized(self) { [self.searchResult removeAllObjects]; } Here

How do I convert NSMutableArray to NSArray?

[亡魂溺海] 提交于 2019-11-27 16:36:09
How do I convert NSMutableArray to NSArray in objective-c ? 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. hallski 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 can't be modified you can create a NSArray either of these ways depending on whether you want it

what does -arrayWithArray actually DO?

丶灬走出姿态 提交于 2019-11-27 16:36:01
问题 I want to see EXACTLY how it creates an array. How can I view the .m files that show how it's done? 回答1: As @Ken mentioned, you can't see the source (although you can disassemble the method via gdb). The method itself creates an immutable (can't be changed), autoreleased copy of the given array. The following are identical in behavior: // Both resulting arrays are immutable and won't be retained NSArray* immutableArray = [[[NSArray alloc] initWithArray:mutableArray] autorelease]; NSArray*

Removing object from NSMutableArray

蓝咒 提交于 2019-11-27 15:28:31
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? Vladimir You cannot remove objects from array while fast-enumerating it: numeration is “safe”—the enumerator has a mutation guard so that if you attempt to modify the collection during

insert object in an NSMutableArray saved with NSUserDefaults

只谈情不闲聊 提交于 2019-11-27 15:28:15
I have an NSMutableArray saved with NSUserDefaults. This array is my "favourite" items that the user can saves, so when i want to add one item, i need to read the array (from NSuserDefault) and save in the first free position. I'm using this method to add a value in the NSMutableArray -(IBAction)save{ NSMutableArray *abc = [[NSUserDefaults standardUserDefaults] objectForKey:@"12345"]; int n = [abc count]; [abc insertObject:@"aaa" atIndex:n]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [[NSUserDefaults standardUserDefaults] setObject:abc forKey:@"12345"]; [defaults