ios - 从 NSMutableArray 错误中删除值

标签 ios arrays swift nsmutablearray

我正在尝试创建一个有趣的事实应用程序。它每次都会显示数组中的随机字符串,然后从数组中删除该事实。对于我的代码,当应用程序首次启动时,它会获取新的事实数组,并在应用程序关闭时保存数据,并在每次初始启动后使用上次启动时的数组。我的问题是,当我尝试从数组的最后第四行删除字符串时,出现错误“Thread 1: signal SIGABRT”。请告诉我需要进行哪些更正。我对编程相当陌生。我感谢我得到的所有帮助。感谢您的宝贵时间

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 : NSMutableArray = [
        "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 : NSMutableArray = []

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

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




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.removeObjectAtIndex(randomFacts1().1) //this gives me the error
//oldFunFactsArray.removeValueAtIndex(randomFacts1().1, fromPropertyWithKey: "key") //this gives me the same error
//oldFunFactsArray.removeAtIndex(randomFacts1().1) //This gives me the error "NSMutableArray does not have a member named 'removeAtIndex'
userDefaults.setObject(oldFunFactsArray, forKey:"key")
userDefaults.synchronize()
println(oldFunFactsArray)

最佳答案

您必须创建事实数组的可变副本才能从中删除对象。

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

关于ios - 从 NSMutableArray 错误中删除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32336416/

相关文章:

ios - regionDidChange 中的计时器不会触发 swift 4

iphone - 段控制背景色

ios - Instagram 的移动应用重定向 URI

javascript - 数组映射函数不改变元素

python - 查找具有零值的行索引

swift - 在将节点之一从屏幕上删除之前,如何要求其发生两次碰撞?

ios - 如何验证 iOS 应用购买者

ios - 为什么我不能调用另一个类的方法

ios - iOS-CGImageRef潜在泄漏

arrays - 将 JSON 解析为 swift 预定义数组