简体   繁体   中英

Call a func from another class in swift

I would like to call a function which is coded on another class.
So far I have made a struct on the file structs.swift for my data:

struct defValues {
    let defCityName: String
    let loadImages: Bool

    init(defCity: String, loadImgs: Bool){
        self.defCityName = defCity
        self.loadImages = loadImgs
    }

}

I have made the file Defaults.swift containing:

import Foundation

class DefaultsSet {

    let cityKey: String = "default_city"
    let loadKey: String = "load_imgs"

    func read() -> defValues {
        let defaults = NSUserDefaults.standardUserDefaults()
        if let name = defaults.stringForKey(cityKey){
            print(name)
            let valuesToReturn = defValues(defCity: name, loadImgs: true)
            return valuesToReturn
        }
        else {
            let valuesToReturn = defValues(defCity: "No default city set", loadImgs: true)
            return valuesToReturn
        }
    }

    func write(city: String, load: Bool){
        let def = NSUserDefaults.standardUserDefaults()
        def.setObject(city, forKey: cityKey)
        def.setBool(load, forKey: loadKey)
    }

}

in which I have the two functions read, write to read and write data with NSUsersDefault respectively.
On my main ViewController I can read data with:

let loadeddata: defValues = DefaultsSet().read()
if loadeddata.defCityName == "No default city set" {
    defaultCity = "London"
}
else {
    defaultCity = loadeddata.defCityName
    defaultLoad = loadeddata.loadImages

}

But when I try to write data it gives me error. I use this code:

@IBOutlet var settingsTable: UITableView!
@IBOutlet var defaultCityName: UITextField!
@IBOutlet var loadImgs: UISwitch!

var switchState: Bool = true

@IBAction func switchChanged(sender: UISwitch) {
    if sender.on{
        switchState = true
        print(switchState)
    }else   {
        switchState = false
        print(switchState)
    }
}

@IBAction func saveSettings(sender: UIButton) {
    DefaultsSet.write(defaultCityName.text, switchState)
}

You need an instance of the DefaultsSet class

In the view controller add this line on the class level

var setOfDefaults = DefaultsSet()

Then read

let loadeddata = setOfDefaults.read()

and write

setOfDefaults.write(defaultCityName.text, switchState)

The variable name setOfDefaults is on purpose to see the difference.

Or make the functions class functions and the variables static variables and call the functions on the class (without parentheses)

From the code you posted, it seems you either need to make the write method a class method (just prefix it with class ) or you need to call it on an instance of DefaultsSet : DefaultsSet().write(defaultCityName.text, switchState) .

Another issue I found is that you also need to unwrapp the value of the textField. Your write method takes as parameters a String and a Bool , but the value of defaultCityName.text is an optional, so String? . This results in a compiler error.

You can try something like this:

@IBAction func saveSettings(sender: UIButton) {
    guard let text = defaultCityName.text else {
         // the text is empty - nothing to save
         return
    }

    DefaultsSet.write(text, switchState)
}

This code should now compile and let you call your method.

Let me know if it helped you solve the problem

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