swift array.removeAtIndex error

时间秒杀一切 提交于 2019-12-25 06:56:39

问题


How come I'm getting the error "NSArray does not have a member named 'removeAtIndex'. How can I fix this? The error is on the fourth last line. Sorry if my question is stupid, I'm fairly new to programming. I appreciate all the help I get.

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)

回答1:


We have some problems here:

1 How to invoke a method

removeAtIndex is a method that accepts an Int as parameters. It cannot be invoked this way

removeAtIndex[randomFacts1().1]

instead you should write

removeAtIndex(randomFacts1().1)

2. The type of oldFunFactsArray is NSArray and it's wrong.

Intact when you write this:

var oldFunFactsArray = []

Swift does infer this:

var oldFunFactsArray : NSArray = []

But at this point you have an immutable NSArray so it does not have the removeAtIndex method.

Since you are using Swift I suggest you to declare the var oldFunFactsArray as follow:

var oldFunFactsArray : [String]

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

Please note that here I am declaring a Swift array of String(s). Since I use the var keyword this array will be mutable and we will be able to invoke removeAtIndex later on.

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.

Hope this helps.




回答2:


You need to use NSMutableArray to use this method.

NSArray is not Mutable (can not change its content after it is intialized).



来源:https://stackoverflow.com/questions/32334706/swift-array-removeatindex-error

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