[英]Swift Struct Error
我正在嘗試創建一個有趣的事實應用程序。 每次都會從數組中顯示一個隨機字符串,然后將該事實從數組中刪除。 對於我來說,代碼在首次啟動應用程序時會獲取新的事實數組,並在應用程序關閉時保存數據,並在首次啟動后每次都使用前一次啟動時的數組。 我的問題是我的代碼的第四行出現錯誤“調用中參數#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()
您需要初始化returnRandomFacts
。 嘗試以下returnRandomFacts().randomFacts().1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.