9

I just got into swift coding and I am trying to follow a tutorial. but it seems as if the coder I'm following may have an older version, or I am doing something wrong. I am trying to make a sound object to make a soundboard. but when i try to add the sound file to the array using append, it says that the method append is not a member of the NSArray. can someone tell me what is the right way to do solve this?!]1

enter image description here

Andrea
  • 26,120
  • 10
  • 85
  • 131
i.zain595
  • 93
  • 1
  • 1
  • 4

4 Answers4

23

Declare sounds as

var sounds: [Sound] = []
CQH
  • 346
  • 1
  • 5
11

You should work with Swift native type Array

var array: [Any] = []

if you know it will have only one type you can do as follow:

var intArray: [Int] = []
var doubleArray: [Double] = []
var stringArray: [String] = []
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
3

Swift and Foundation arrays are different types of objects.
Swift Array declares a method called append, NSMutableArray(NSArray is immutable you can't modify it after you created) doesn't declare that kind of method.
The solution is cast your NSArray into an Array type (using the as operator, since they are bridged), or use the method -addObject: on a NSMutableArray, but first you need to make a -mutableCopy of the NSArray

Andrea
  • 26,120
  • 10
  • 85
  • 131
1

NSArray is an immutable object. You have to use NSMutableArray Here is the documentation

Jens
  • 67,715
  • 15
  • 98
  • 113