swift array.removeAtIndex 错误

swift array.removeAtIndex error

我怎么会收到错误消息“NSArray 没有名为 'removeAtIndex' 的成员。我该如何解决这个问题?错误出现在倒数第四行。抱歉,如果我的问题很愚蠢,我我对编程还很陌生。我很感激我得到的所有帮助。

import Foundation
let userDefaults = NSUserDefaults.standardUserDefaults()

func isAppAlreadyLaunchedOnce()->Bool{
    let defaults = NSUserDefaults.standardUserDefaults()

    if let isAppAlreadyLaunchedOnce = defaults.stringForKey("isAppAlreadyLaunchedOnce"){
        println("App already launched")
        return true
    }
    else{
        defaults.setBool(true, forKey: "isAppAlreadyLaunchedOnce")
        println("App launched first time")
        return false
    }
}

struct newFactBook {


    let factsArray = [
        "Ants stretch when they wake up in the morning.",
        "Ostriches can run faster than horses.",
        "Olympic gold medals are actually made mostly of silver.",
        "You are born with 300 bones; by the time you are an adult you will have 206.",
        "It takes about 8 minutes for light from the Sun to reach Earth.",
        "Some bamboo plants can grow almost a meter in just one day.",
        "The state of Florida is bigger than England.",
        "Some penguins can leap 2-3 meters out of the water.",
        "On average, it takes 66 days to form a new habit.",
        "Mammoths still walked the earth when the Great Pyramid was being built."]

}

    var checkLaunch = isAppAlreadyLaunchedOnce()
    var oldFunFactsArray = []

    if(checkLaunch == false){
    oldFunFactsArray = newFactBook().factsArray
    }

    else if (checkLaunch == true){
    oldFunFactsArray = userDefaults.objectForKey("key") as! NSArray
    }




    func randomFacts1() -> (String, Int){
        var unsignedArrayCount = UInt32(oldFunFactsArray.count)
        var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
        var randomNumber = Int(unsignedRandomNumber)
        return (oldFunFactsArray[randomNumber] as! String, randomNumber)

    }


oldFunFactsArray.removeAtIndex[randomFacts1().1] //error here

userDefaults.setObject(oldFunFactsArray, forKey:"key")
userDefaults.synchronize()
println(oldFunFactsArray)

您需要使用 NSMutableArray 才能使用此方法。

NSArray 不是可变的(初始化后不能改变它的内容)。

我们这里有一些问题:

1 如何调用方法

removeAtIndex 是一种接受 Int 作为参数的方法。 不能这样调用

removeAtIndex[randomFacts1().1]

你应该这样写

removeAtIndex(randomFacts1().1)

2。 oldFunFactsArray的类型是NSArray,是错误的。

当你写这个时完好无损:

var oldFunFactsArray = []

Swift 确实推断出这一点:

var oldFunFactsArray : NSArray = []

但是此时你有一个 immutable NSArray 所以它没有 removeAtIndex 方法。

由于您使用的是 Swift 我建议您声明 var oldFunFactsArray 如下:

var oldFunFactsArray : [String]

if checkLaunch == false {
    oldFunFactsArray = newFactBook().factsArray
} else {
    oldFunFactsArray = userDefaults.objectForKey("key") as! [String]
}

请注意,我在这里声明了一个 Swift 字符串数组 。因为我使用了 var 关键字,这个数组将是 可变的 并且我们稍后可以调用 removeAtIndex

Also note that in the else branch I am force-casting the result of objectForKey to [String]. It will be fine since I see, below, you are writing the oldFunFactsArray in that slot.

希望对您有所帮助。