IDK正在发生什么。我只是在我的数组中添加一个值。
以下是代码:
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let requestedURL = NSString(string: url_to_req)
print(requestedURL.substringFromIndex(36))
if requestedURL.substringFromIndex(36) == "forum"{
returnValue = (parseJSON["_id"] as? String)!
print("Here is id of posted question: '\(returnValue)'")
if (NSUserDefaults.standardUserDefaults().objectForKey("PostedQuesIds") != nil){
self.postedQuestionIds = NSUserDefaults.standardUserDefaults().objectForKey("PostedQuesIds") as? NSMutableArray
}
print("Total count of array '\(self.postedQuestionIds)")
self.postedQuestionIds?.addObject(returnValue)// overhere my app gets a crash
// self.postedQuestionIds = NSMutableArray(object: returnValue)
NSUserDefaults.standardUserDefaults().setObject(self.postedQuestionIds, forKey: "PostedQuesIds")
NSUserDefaults.standardUserDefaults().synchronize()
}
})
对于疑惑,我也试过下面的代码,就好像我已经忘记了关于nsmutablearray的事情
var name = "abc"
let firstname = "dd"
let lastname = "zz"
let arr:NSMutableArray? = NSMutableArray()
arr?.addObject(name)
name = "ABC"
arr?.removeObjectAtIndex(0)
arr?.addObject(firstname)
arr?.addObject(lastname)
arr?.addObject(name)
print("Here is complete array '\(arr)'")
这很好用。请帮助我看起来我错过了什么,无法找到它们。您的努力将受到高度赞赏。
答案 0 :(得分:2)
如果从NSUserDefaults中检索数组,它似乎变为不可变的数组。试试这个。
let array = NSUserDefaults.standardUserDefaults().objectForKey("PostedQuesIds") as! NSArray
self.postedQuestionIds = NSMutableArray(array: array)