Questions tagged [nsarray]

An immutable, integer-indexed array of objects from the Apple Foundation framework.

An NSArray object manages an immutable array; once you have created the array, you cannot add, remove, or replace the objects in the array. You can, however, modify the individual elements themselves (if they support modification). The mutability of the collection does not affect the mutability of the objects inside the collection. You should use an immutable array if your data rarely changes, or changes all at once. Consider using a mutable array () if your data changes frequently.

Resources:

5346 questions
90
votes
12 answers

What's the best way to put a c-struct in an NSArray?

What's the usual way to store c-structures in an NSArray? Advantages, disadvantages, memory handling? Notably, what's the difference between valueWithBytes and valueWithPointer -- raised by justin and catfish below. Here's a link to Apple's…
Fattie
  • 27,874
  • 70
  • 431
  • 719
90
votes
1 answer

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

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…
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
87
votes
9 answers

Sort NSArray of date strings or objects

I have an NSArray that contains date strings (i.e. NSString) like this: "Thu, 21 May 09 19:10:09 -0700" I need to sort the NSArray by date. I thought about converting the date string to an NSDate object first, but got stuck there on how to sort by…
postalservice14
  • 2,515
  • 4
  • 25
  • 33
85
votes
6 answers

How to create an "array of selectors"

I'm using the iPhone SDK (3.0) and I'm trying to create an array of selectors to invoke a variety of methods within one class. Obviously, I'm doing something wrong (I think @selector isn't considered a class and so stuffing them into an NSArray…
user141146
  • 3,285
  • 7
  • 38
  • 54
83
votes
3 answers

Check if NSString instance is contained in an NSArray

I have an array with a bunch of strings and I want to check if a certain string is contained in the array. If I use the containsObject: message on the array, I'm getting correct results. Do all NSString objects with the same string point to the same…
the Reverend
  • 12,305
  • 10
  • 66
  • 121
78
votes
7 answers

NSMutablearray move object from index to index

I have a UItableview with reordable rows and the data is in an NSarray. So how do I move an object in the NSMutablearray when the appropriate tableview delegate is called? Another way to ask this is how to reorder an NSMutableArray?
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
73
votes
7 answers

I want to sort an array using NSSortDescriptor

I am having a problem regarding sorting an array w.r.t database: NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"w" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObject: sorter]; [mGlossaryArray…
Prash.......
  • 851
  • 1
  • 8
  • 14
72
votes
5 answers

Is there an easy way to iterate over an NSArray backwards?

I've got an NSArray and have to iterate over it in a special case backwards, so that I first look at the last element. It's for performance reasons: If the last one just makes no sense, all previous ones can be ignored. So I'd like to break the…
Thanks
  • 40,109
  • 71
  • 208
  • 322
71
votes
9 answers

How can I check if an object in an NSArray is NSNull?

I am getting an array with null value. Please check the structure of my array below: ( "< null>" ) When I'm trying to access index 0 its crashing because of -[NSNull isEqualToString:]: unrecognized selector sent to instance…
Navi
  • 1,696
  • 2
  • 17
  • 36
69
votes
6 answers

Best way to sort an NSArray of NSDictionary objects?

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?
iOSDevil
  • 1,786
  • 3
  • 16
  • 29
67
votes
1 answer

Creating an array from properties of objects in another array

Is there any convenient way to take an array/set of objects and create a new array/set containing some property of each item in the first array? For example, an array contains Car objects. I need an array of licensePlates, where each car has an…
Ben Packard
  • 26,102
  • 25
  • 102
  • 183
67
votes
11 answers

Sorting NSArray of dictionaries by value of a key in the dictionaries

I have an array populated by dictionaries, and I need to sort the array alphabetically by the values of one of the keys of the dictionaries. This is my array: tu dictus: ( { brand = Ryul; productTitle = Any; quantity = 1; …
manuelBetancurt
  • 15,428
  • 33
  • 118
  • 216
67
votes
7 answers

Getting Index of an Object from NSArray?

i am trying to get index of an array through indexOfObject method as follows but when i try to log the value to test the index i get a garbage value.. for testing purposes i am having an array with values {57,56,58..} to get an index of lets say…
hemant
  • 1,771
  • 4
  • 31
  • 43
66
votes
5 answers

Create NSIndexSet from integer array in Swift

I converted an NSIndexSet to an [Int] array using the answer at https://stackoverflow.com/a/28964059/6481734 I need to do essentially the opposite, turning the same kind of array back into an NSIndexSet.
Jacolack
  • 1,365
  • 2
  • 11
  • 25
66
votes
8 answers

How do copy and mutableCopy apply to NSArray and NSMutableArray?

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,…
fuzzygoat
  • 26,573
  • 48
  • 165
  • 294