2015-09-01 4 views
0

Я пытаюсь создать приложение для забавных фактов. Он отображает случайную строку из массива каждый раз, затем этот факт удаляется из массива. Для меня код, когда приложение впервые запускается, получает новый массив фактов и сохраняет данные при закрытии приложения и использует массив из предыдущего запуска каждый раз после первого запуска. Моя проблема в том, что я получаю сообщение об ошибке «Thread 1: signal SIGABRT», когда я пытаюсь удалить строку из моего массива на моей четвертой последней строке. Скажите, пожалуйста, какие исправления мне нужно сделать. Я довольно новичок в программировании. Я ценю всю помощь, которую я получаю. Спасибо за ваше времяУдаление значения из ошибки NSMutableArray

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 
} 

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) //this gives me the error 
//oldFunFactsArray.removeValueAtIndex(randomFacts1().1, fromPropertyWithKey: "key") //this gives me the same error 
//oldFunFactsArray.removeAtIndex(randomFacts1().1) //This gives me the error "NSMutableArray does not have a member named 'removeAtIndex' 
userDefaults.setObject(oldFunFactsArray, forKey:"key") 
userDefaults.synchronize() 
println(oldFunFactsArray) 
+0

функция removeObjectAtIndex не removeAtIndex –

+0

'oldFunFactsArray.removeObjectAtIndex (randomFacts1(). 1)' работает для меня в тестовой Playground ([скриншот] (https://www.evernote.com/shard/s89 /sh/7a13543e-4fd4-4856-ab32-47caa1d41b8c/8061657c2f97992b/res/879bdf5c-b26f-4e99-b770-0a42085cc295/skitch.png)). – Moritz

ответ

0

Вы должны создать mutableCopy массива фактов к removeObjects от него.

if(checkLaunch == false){ 
    oldFunFactsArray = newFactBook().factsArray.mutableCopy 
}