nsmutablearray

Shuffling an array in objective-c [duplicate]

旧巷老猫 提交于 2019-11-27 05:46:05
问题 Possible Duplicate: What’s the Best Way to Shuffle an NSMutableArray? I develop apps for iphone/iPad.I want to shuffle the objects stored in an NSArray.Is there any way to achieve it with objective-c? 回答1: Add a category to NSMutableArray, with code provided by Kristopher Johnson - // NSMutableArray_Shuffling.h #if TARGET_OS_IPHONE #import <UIKit/UIKit.h> #else #include <Cocoa/Cocoa.h> #endif // This category enhances NSMutableArray by providing // methods to randomly shuffle the elements.

iOS: store two NSMutableArray in a .plist file

十年热恋 提交于 2019-11-27 05:19:39
I want to store two NSMutableArray that I use as global array in AppDelegate. These two array are also store with NSUserDefaults. Now I want to know how I must create this file and how can I store these two array everytime I modify them. Can You help me? Create an NSArray containing your two NSMutableArrays. NSArray *array = [NSArray arrayWithObjects:<#(id), ...#>, nil]; Write the array to a file. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *libraryDirectory = [paths objectAtIndex:0]; NSString *location = [libraryDirectory

How will I be able to remove [NSNull Null] objects from NSMutableArray?

一个人想着一个人 提交于 2019-11-27 05:16:41
I need to remove Null object added by [mutArrSkills addObject:[NSNull null]]; Do I need to iterate? Is there any function to remove all null values from NSMutableArray? If need to Iterate, how will I do that? You can use NSMutableArray 's removeObjectIdenticalTo: method, as follows [mutArrSkills removeObjectIdenticalTo:[NSNull null]]; to remove the null values. No need to iterate. removeObjectIdenticalTo: Removes all occurrences of a given object in the array. Discussion This method uses the indexOfObjectIdenticalTo: method to locate matches and then removes them by using removeObjectAtIndex:.

UIActivityViewController - Email and Twitter sharing

这一生的挚爱 提交于 2019-11-27 05:09:57
问题 I recently started working with UIActivity to share my app to the world, but I have few problems. First, I didn't find how to set the subject of my email. Is there any way? Second, when I set the body text of my email, there is a extra "enter" (first line of the email is blank and my text starts at the second line). Here's the code: NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"Test", nil]; UIActivityViewController *activityViewController = [[UIActivityViewController

2D arrays using NSMutableArray

浪子不回头ぞ 提交于 2019-11-27 03:17:27
I need to create a mutable two-dimensional array in Objective-C. For example I have: NSMutableArray *sections; NSMutableArray *rows; Each item in sections consists of an array rows . rows is an array that contains objects. And I want to do something like this: [ sections[i] addObject: objectToAdd]; //I want to add a new row In order have something like this: section 0, rows: obj1, obj2, obj3 section 1, rows: obj4, obj5, obj6, obj 7... Is there a way to do that in Objective-C? mouviciel First, you must allocate and initialize your objects before use, something like: NSMutableArray * sections =

'Invalid update: invalid number of rows in section 0

人走茶凉 提交于 2019-11-27 03:06:20
I've read all of the related posts regarding this and I am still having an error: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' Here are the details: in .h I have an NSMutableArray : @property (strong,nonatomic) NSMutableArray *currentCart;

How to do sparse array in Cocoa

三世轮回 提交于 2019-11-27 02:52:26
问题 I have an undetermined size for a dataset based on unique integer keys. I would like to use an NSMutableArray for fast lookup since all my keys are integer based. I want to do this. NSMutableArray* data = [NSMutableArray array]; // just create with 0 size then later people will start throwing data at me with integer indexes (all unique) so I just want to do something like this... if ([data count] < index) [data resize:index]; // ? how do you resize and have the array resized so that i can

Removing duplicates from NSMutableArray

爷,独闯天下 提交于 2019-11-27 02:07:18
I have this problem with removing duplicate objects from an array. I tried these already: noDuplicates = _personalHistory.personalHistory; for (int i=[noDuplicates count]-1; i>0; i--) { if ([noDuplicates indexOfObject: [noDuplicates objectAtIndex: i]]<i) [noDuplicates removeObjectAtIndex: i]; } for (PersonalHistory_artikels *e in _personalHistory.personalHistory) { if (![noDuplicates containsObject:e]) { NSLog(@"Dubplicates"); [noDuplicates addObject:e]; } } for (i=0; i<_personalHistory.personalHistory.count; i++) { PersonalHistory_artikels *test = [_personalHistory.personalHistory

NSMutableArray initWithCapacity nuances

◇◆丶佛笑我妖孽 提交于 2019-11-27 00:52:38
Does anyone have advice on how to best initialize an NSMutableArray when it comes to dictating the capacity? The documentation mentions that "...even though you specify a size when you create an array, the specified size is regarded as a “hint”; the actual size of the array is still 0." So... 1) If I init with a greater capacity than I typically use, do I not have to worry about wasted memory? 2) If I init with a capacity typically lower than what I use, do I have to worry about heavier processing time allocating more memory to hold the extra elements? Just how impacting is this initialized

How can I sort an NSMutableArray alphabetically?

别来无恙 提交于 2019-11-27 00:45:05
问题 I want to sort an NSMutableArray alphabetically. 回答1: You can do this to sort NSMutableArray: [yourArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 回答2: The other answers provided here mention using @selector(localizedCaseInsensitiveCompare:) This works great for an array of NSString, however the OP commented that the array contains objects and that sorting should be done according to object.name property. In this case you should do this: NSSortDescriptor *sort =