Arrays (lldb)错误swift NSMUTABLEARRY

Arrays (lldb)错误swift NSMUTABLEARRY,arrays,swift,nsmutablearray,Arrays,Swift,Nsmutablearray,我正在尝试创建一个有趣的事实应用程序。它每次显示一个数组中的随机字符串,然后从数组中删除该事实。对于我来说,当应用程序首次启动时,它会获取新的事实数组,并在应用程序关闭时保存数据,并在每次首次启动后使用上一次启动时的数组。我的问题是,当我试图在最后第四行从数组中删除一个字符串时,我得到了“thread1:SIGABRT信号”。请告诉我需要做哪些更正。我对编程相当陌生。我感谢所有的帮助。谢谢你抽出时间 import Foundation let userDefaults = NSUserDefau

我正在尝试创建一个有趣的事实应用程序。它每次显示一个数组中的随机字符串,然后从数组中删除该事实。对于我来说,当应用程序首次启动时,它会获取新的事实数组,并在应用程序关闭时保存数据,并在每次首次启动后使用上一次启动时的数组。我的问题是,当我试图在最后第四行从数组中删除一个字符串时,我得到了“thread1: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.mutableCopy() as! NSMutableArray
}

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) // Thread 1: signal SIGABRT
userDefaults.setObject(oldFunFactsArray, forKey:"key")
userDefaults.synchronize()
println(oldFunFactsArray)
这是我在调试区域得到的:

App already launched
2015-09-02 10:17:06.110 TestCode1[36860:1553790] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff8245403c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff8743d76e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff82453eed +[NSException raise:format:] + 205
    3   CoreFoundation                      0x00007fff824477be -[__NSCFArray removeObjectAtIndex:] + 94
    4   TestCode1                           0x00000001001552a6 main + 806
    5   libdyld.dylib                       0x00007fff903bd5c9 start + 1
    6   ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

即使一切设置正确,有时候Xcode也会给我们一个NSArray而不是NSMutableArray。 要恢复NSMutableArray,以便删除对象,请执行以下操作:

let immutable = array.mutableCopy() as! NSMutableArray
以下是我所做的:

var modArray = oldFunFactsArray.mutableCopy() as! NSMutableArray
modArray.removeObjectAtIndex(randomFacts1().1)
oldFunFactsArray = modArray

我创建了一个新数组并将其分配给我的旧数组。然后,我从新数组中删除了字符串,并将旧数组分配给新数组。谢谢大家的帮助

为什么要使用
stringForKey
而不是
boolForKey
?还有,为什么要使用
NSMutableArray
?为什么不直接使用Swift
数组呢?
?另外,您是否看到了
”-[\uu NSCFArray removeObjectAtIndex::]:发送到不可变对象的mutating方法“
?这意味着您的代码需要一个
NSMutableArray
,但实际上它得到了一个常规的
NSArray
。我将let-factsArray更改为var-factsArray,但我仍然得到相同的错误,有时不是“Xcode”这样做。NSUserDefaults始终返回不可变的对象。如果您想修改从NSUserDefaults返回的内容,您必须将其复制到mutableCopy中,或者将其放入一个Swift(不是NS!)数组中,该数组是一个var。