Add an object to the beginning of an NSMutableArray?
Simply [array insertObject:obj atIndex:0]; Check the documentation
Simply [array insertObject:obj atIndex:0]; Check the documentation
Write a class method that returns the array. + (NSArray *)titles { static NSArray *_titles; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _titles = @[@”Your Move”, @”Their Move”, @”Won Games”, @”Lost Games”, @”Options”]; }); return _titles; } Then you can access it wherever needed like so: NSArray *titles = [[self class] titles];
No. It is not thread safe and if you need to modify your mutable array from another thread you should use NSLock to ensure everything goes as planned: NSLock *arrayLock = [[NSLock alloc] init]; […] [arrayLock lock]; // NSMutableArray isn’t thread-safe [myMutableArray addObject:@”something”]; [myMutableArray removeObjectAtIndex:5]; [arrayLock unlock];
You can use -[NSArray indexOfObject:inSortedRange:options:usingComparator:] to ask an NSArray for the index where an object should be inserted given an array range that’s currently sorted. For example, assuming the entire array is sorted:: NSMutableArray *array = …; id newObject = …; NSComparator comparator = …; NSUInteger newIndex = [array indexOfObject:newObject inSortedRange:(NSRange){0, [array count]} options:NSBinarySearchingInsertionIndex usingComparator:comparator]; … Read more
If you must add a nil object to a collection, use the NSNull class: The NSNull class defines a singleton object used to represent null values in collection objects (which don’t allow nil values). Assuming “array” is of type NSMutableArray: …. [array addObject:[NSNumber numberWithInt:2]; [array addObject:@”string”]; [array addObject:[NSNull null]];
exchangeObjectAtIndex:withObjectAtIndex: does exactly this.
There isn’t a built in way, but I just usually use mutableCopy like this: NSMutableArray *array = [@[ @”1″, @”2″, @”3″ ] mutableCopy];
The “0x0” part is a memory address. Specifically, “nil”, which means your mutable array doesn’t exist at the time this is being called. Try creating it in your -init method: categories = [[NSMutableArray alloc] init]; Don’t forget to release it in your -dealloc.
NSMutableArray (and all other classes with Mutable in the name) can be modified. So, if you create a plain NSArray, you cannot change its contents later (without recreating it). But if you create an NSMutableArray, you can change it — you’ll notice it has methods like -addObject: and -insertObject:atIndex:. See the documentation for details.
The method is called – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath The indexpath parameter contains a row and section property. indexPath.section indexPath.row Here’s documentation link for the NSIndexPath class.