"NSArray' does not have a member named 'append'

不打扰是莪最后的温柔 提交于 2019-12-23 07:36:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!