简体   繁体   中英

(lldb) error swift NSMutableArray

I'm trying to created an fun facts app. It displays a random string from an array every time, then that fact is deleted from the array. For me code, when the app is first launched it gets new array of facts and it saves the data when the app is closed and uses the array from the previous launch every time after the initial launch. My problem is I get "Thread 1: signal SIGABRT" when I try to remove a string from my array on my 4th last line. Please tell me what corrections I need to make. I am fairly new to programming. I appreciate all the help I get. Thanks for your time.

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)

This is what I got in the debug area:

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) 

Here is what I did:

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

I made a new array and assigned it to my old array. I then removed the string from my new array and assigned my old array to my new array. Thanks for the help everyone!

Even setting everything up correctly, sometimes Xcode gives us a NSArray instead of a NSMutableArray. To get a NSMutableArray back, so you can remove objects, do this:

let immutable = array.mutableCopy() as! NSMutableArray

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM