nsarray

What is the BOOL *stop argument for enumerateObjectsUsingBlock: used for?

前提是你 提交于 2019-11-26 08:01:56
问题 I\'ve been using enumerateObjectsUsingBlock: a lot lately for my fast-enumeration needs, and I\'m having a hard time understanding the usage of BOOL *stop in the enumeration block. The NSArray class reference states stop : A reference to a Boolean value. The block can set the value to YES to stop further processing of the array. The stop argument is an out-only argument. You should only ever set this Boolean to YES within the Block. So then of course I can add the following in my block to

Store [String] in NSUserDefaults

巧了我就是萌 提交于 2019-11-26 07:36:52
问题 I want to save a Swift Style String Array into NSUserDefaults, but acutally the \"if\" statement in the code says that returnValue is always nil. Later in the code (iOS 8) I want to use \"food += [\"spaghetti\"] to add new entries. var food : [String] { get { var returnValue : [String]? = NSUserDefaults.standardUserDefaults().objectForKey(\"food\") as? [String] if returnValue == nil //Check for first run of app { returnValue = [\"muesli\", \"banana\"]; //Default value } return returnValue! }

How do copy and mutableCopy apply to NSArray and NSMutableArray?

蓝咒 提交于 2019-11-26 06:58:59
问题 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 **

How can I sort an NSArray containing NSDictionaries?

非 Y 不嫁゛ 提交于 2019-11-26 06:48:04
问题 I have an NSArray which contains NSDictionary objects. Each of these NSDictionary objects contains an ORDER key. How can I sort this NSArray based on this key within each of these NSDictionaries ? 回答1: Here is an example. Imagines you have an array of dictionnaries. Ecah dictionnary have 2 keys "name" & "dateOfBirth". You can sort your array by "name" like this : // // Sort array by name // NSSortDescriptor *Sorter = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; [myArray

Deep copying an NSArray

六月ゝ 毕业季﹏ 提交于 2019-11-26 06:43:20
Is there any built-in function that allows me to deep copy an NSMutableArray ? I looked around, some people say [aMutableArray copyWithZone:nil] works as deep copy. But I tried and it seems to be a shallow copy. Right now I am manually doing the copy with a for loop: //deep copy a 9*9 mutable array to a passed-in reference array -deepMuCopy : (NSMutableArray*) array toNewArray : (NSMutableArray*) arrayNew { [arrayNew removeAllObjects];//ensure it's clean for (int y = 0; y<9; y++) { [arrayNew addObject:[NSMutableArray new]]; for (int x = 0; x<9; x++) { [[arrayNew objectAtIndex:y] addObject:

Is there a simple way to split a NSString into an array of characters?

纵然是瞬间 提交于 2019-11-26 06:09:08
问题 Is there a simple way to split a NSString into an array of characters? It would actually be best if the resulting type were a collection of NSString\'s themselves, just one character each. Yes, I know I can do this in a loop, but I\'m wondering if there is a faster way to do this with any existing methods or functions the way you can with LINQ in C#. e.g. // I have this... NSString * fooString = @\"Hello\"; // And want this... NSArray * fooChars; // <-- Contains the NSStrings, @\"H\", @\"e\",

How can I reverse a NSArray in Objective-C?

一世执手 提交于 2019-11-26 05:42:41
问题 I need to reverse my NSArray . As an example: [1,2,3,4,5] must become: [5,4,3,2,1] What is the best way to achieve this? 回答1: For obtaining a reversed copy of an array, look at danielpunkass' solution using reverseObjectEnumerator . For reversing a mutable array, you can add the following category to your code: @implementation NSMutableArray (Reverse) - (void)reverse { if ([self count] <= 1) return; NSUInteger i = 0; NSUInteger j = [self count] - 1; while (i < j) { [self exchangeObjectAtIndex

Convert JSON feed to NSDictionary

流过昼夜 提交于 2019-11-26 05:29:53
问题 Where JSON_CATEGORY_DATA_URL_STRING is my feed URL, which returns fine as: [ { \"group\":\"For Sale\", \"code\":\"SSSS\" }, { \"group\":\"For Sale\", \"category\":\"Wanted\", \"code\":\"SWNT\" } ] I cannot seem to get a nice NSDictionary (or NSArray ) out of the following code: + (NSDictionary *)downloadJSON { NSDictionary *json_string; NSString *dataURL = [NSString stringWithFormat:@\"%@\", JSON_CATEGORY_DATA_URL_STRING]; NSLog(@\"%@\",dataURL); NSURLRequest *request = [NSURLRequest

filtering NSArray into a new NSArray in Objective-C

ぐ巨炮叔叔 提交于 2019-11-26 03:52:15
问题 I have an NSArray and I\'d like to create a new NSArray with objects from the original array that meet certain criteria. The criteria is decided by a function that returns a BOOL . I can create an NSMutableArray , iterate through the source array and copy over the objects that the filter function accepts and then create an immutable version of it. Is there a better way? 回答1: NSArray and NSMutableArray provide methods to filter array contents. NSArray provides filteredArrayUsingPredicate:

Best way to sort an NSArray of NSDictionary objects?

梦想的初衷 提交于 2019-11-26 03:50:15
问题 I\'m struggling with trying to sort an array of dictionaries. My dictionaries have a couple of values of interest, price, popularity etc. Any suggestions? 回答1: Use NSSortDescriptor like this.. NSSortDescriptor * descriptor = [[NSSortDescriptor alloc] initWithKey:@"interest" ascending:YES]; stories = [stories sortedArrayUsingDescriptors:@[descriptor]]; recent = [stories copy]; stories is the array you want to sort. recent is another mutable array which has sorted dictionary values. Change the