我正在尝试创建一个有趣的事实应用程序。它每次都会从数组中显示一个随机字符串,然后从数组中删除该事实。对于我的代码,当应用程序首次启动时,它会获得新的事实数组,并在应用程序关闭时保存数据,并在每次初始启动后使用上一次启动时的数组。我的问题是我的代码的第四行最后一行收到错误“在参数#1中缺少参数”。请帮助我,我对编程很新。
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 = [
"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 = []
if(checkLaunch == false){
oldFunFactsArray = newFactBook().factsArray
}
else if (checkLaunch == true){
oldFunFactsArray = userDefaults.objectForKey("key") as! NSArray
}
struct returnRandomFacts {
func randomFacts() -> (String, Int){
var unsignedArrayCount = UInt32(oldFunFactsArray.count)
var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
var randomNumber = Int(unsignedRandomNumber)
return (oldFunFactsArray[randomNumber] as! String, randomNumber)
}
}
returnRandomFacts.randomFacts().1 // error right here
println(oldFunFactsArray)
userDefaults.setObject(oldFunFactsArray, forKey:"key")
userDefaults.synchronize()
答案 0 :(得分:0)
您需要初始化returnRandomFacts
。请尝试以下returnRandomFacts().randomFacts().1