问题
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

回答1:
Declare sounds as
var sounds: [Sound] = []
回答2:
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] = []
回答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
回答4:
NSArray
is an immutable object.
You have to use NSMutableArray
Here is the documentation
来源:https://stackoverflow.com/questions/30474680/nsarray-does-not-have-a-member-named-append