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
465
votes
8 answers

How do I iterate over an NSArray?

I'm looking for the standard idiom to iterate over an NSArray. My code needs to be suitable for OS X 10.4+.
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
393
votes
9 answers

How do I convert NSMutableArray to NSArray?

How do I convert NSMutableArray to NSArray in objective-c?
marcy
  • 4,213
  • 3
  • 23
  • 14
363
votes
18 answers

How can I reverse a NSArray in Objective-C?

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?
Andy Jacobs
  • 15,187
  • 13
  • 60
  • 91
189
votes
9 answers

Convert NSArray to NSString in Objective-C

I am wondering how to convert an NSArray [@"Apple", @"Pear ", 323, @"Orange"] to a string in Objective-C.
alexyorke
  • 4,224
  • 3
  • 34
  • 55
179
votes
2 answers

Best practice? - Array/Dictionary as a Core Data Entity Attribute

I am new to Core Data. I have noticed that collection types are not available as attribute types and would like to know what the most efficient way is of storing array/dictionary type data as an attribute (e.g. the elements that make up an address…
RunLoop
  • 20,288
  • 21
  • 96
  • 151
137
votes
7 answers

How to convert NSNumber to NSString

So I have an NSArray "myArray" with NSNumbers and NSStrings. I need them in another UIView so i go like this: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { DetailViewController *details =…
dav3
  • 1,447
  • 2
  • 10
  • 9
135
votes
16 answers

How to group by the elements of an array in Swift

Let's say that I have this code: class Stat { var statEvents : [StatEvents] = [] } struct StatEvents { var name: String var date: String var hours: Int } var currentStat = Stat() currentStat.statEvents = [ StatEvents(name:…
Ruben
  • 1,789
  • 2
  • 17
  • 26
130
votes
3 answers

Join an Array in Objective-C

I'm looking for a method of turning a NSMutableArray into a string. Is there anything on a par with this Ruby array method? >> array1 = [1, 2, 3] >> array1.join(',') => "1,2,3" Cheers!
Codebeef
  • 43,508
  • 23
  • 86
  • 119
128
votes
9 answers

filtering NSArray into a new NSArray in Objective-C

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…
lajos
  • 25,525
  • 19
  • 65
  • 75
122
votes
6 answers

Deep copying an NSArray

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…
ivanTheTerrible
  • 2,836
  • 4
  • 25
  • 25
106
votes
3 answers

How to return an NSMutableArray from an NSSet

I'm able to put the contents of an NSSet into an NSMutableArray like this: NSMutableArray *array = [set allObjects]; The compiler complains though because [set allObjects] returns an NSArray not an NSMutableArray. How should this be fixed?
node ninja
  • 31,796
  • 59
  • 166
  • 254
98
votes
2 answers

NSArray with NSPredicate using NOT IN

I have an NSArray that I want to filter out certain objects using an NSPredicate, I was hoping I could use NOT IN since I saw that I can easily do an IN. So I have my array: self.categoriesList Then I get the values I want to remove: NSArray…
Slee
  • 27,498
  • 52
  • 145
  • 243
97
votes
16 answers

How to count occurrences of an element in a Swift array?

I've seen a few examples of this but all of those seem to rely on knowing which element you want to count the occurrences of. My array is generated dynamically so I have no way of knowing which element I want to count the occurrences of (I want to…
Alex Chesters
  • 3,380
  • 5
  • 19
  • 27
95
votes
6 answers

Using NSPredicate to filter an NSArray based on NSDictionary keys

I have an array of dictionaries. I want to filter the array based on a key. I tried this: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(SPORT == %@)", @"Football"]; NSArray *filteredArray = [data…
Corey Floyd
  • 25,929
  • 31
  • 126
  • 154
90
votes
3 answers

Get all keys of an NSDictionary as an NSArray

Is it possible to get all the keys from a specific NSDictionary as a seperate NSArray?
Jay
  • 913
  • 1
  • 6
  • 4
1
2 3
99 100