简体   繁体   中英

Swift Struct Error

I'm trying to created an fun facts app. It displays a random string from an array every time, then that fact is deleted from the array. For me code, when the app is first launched it gets new array of facts and it saves the data when the app is closed and uses the array from the previous launch every time after the initial launch. My problem is I get an error "Missing argument for parameter #1 in call" on the fourth last line of my code. Please help me, I am fairly new to programming.

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()

you need to initialize returnRandomFacts . Try the following returnRandomFacts().randomFacts().1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM