簡體   English   中英

(lldb)錯誤swift NSMutableArray

[英](lldb) error swift NSMutableArray

我正在嘗試創建一個有趣的事實應用程序。 每次都會從數組中顯示一個隨機字符串,然后將該事實從數組中刪除。 對於我來說,代碼在首次啟動應用程序時會獲取新的事實數組,並在應用程序關閉時保存數據,並在首次啟動后每次都使用前一次啟動時的數組。 我的問題是,當我嘗試從最后一行的第四行中刪除數組中的字符串時,出現“線程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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM