arrays - 快速 array.removeAtIndex 错误

标签 arrays swift nsarray

为什么我收到错误“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)

最佳答案

我们这里遇到一些问题:

1 如何调用方法

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

removeAtIndex[randomFacts1().1]

相反,你应该写

removeAtIndex(randomFacts1().1)

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

当你写下这个时完好无损:

var oldFunFactsArray = []

Swift 确实推断出这一点:

var oldFunFactsArray : NSArray = []

但是此时您有一个不可变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.

希望这有帮助。

关于arrays - 快速 array.removeAtIndex 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32334706/

相关文章:

c++ - 指向更大 unsigned char 数组的每个第 n 个元素的指针数组

swift - 如何将 SwiftUI View 设置为 CollectionView 的单元格

ios - 重新排序 NSDictionary 数组

ios - Apple 的 AVCamera 照片捕捉

ios - 使表格单元格点击事件更符合 "MVVM"

image - ios Objective C 请帮忙处理数组!

ios - NSPredicates 可以用来用字典中的值替换数组中的对象吗?

javascript - JavaScript 数组的长度是什么类型的整数?有环绕的危险吗?

php - 根据数组中的值做出决策

arrays - VBA 中的单个数据字典