我试图创建一个有趣的事实应用程序。它每次从数组中显示一个随机字符串,然后从数组中删除该事实。对于我的代码,当应用程序首次启动时,它获得新的事实数组,并在应用程序关闭时保存数据,并在初始启动后每次使用上次启动时的数组。我的问题是我得到"线程1:信号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)
我是这样做的:
var modArray = oldFunFactsArray.mutableCopy() as! NSMutableArray
modArray.removeObjectAtIndex(randomFacts1().1)
oldFunFactsArray = modArray
我创建了一个新的数组并将它赋值给我的旧数组。然后我从我的新数组中删除字符串,并将我的旧数组分配给我的新数组。谢谢大家的帮助!
即使一切设置正确,有时Xcode给我们一个NSArray而不是NSMutableArray。要取回一个NSMutableArray,这样你就可以移除对象了,这样做:
let immutable = array.mutableCopy() as! NSMutableArray