简体   繁体   English

快速呼叫另一个班级的功能

[英]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: 到目前为止,我已经在文件structs.swift中为我的数据制作了一个结构:

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: 我已将文件Defaults.swift制作为包含:

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. 其中我有两个函数,分别使用NSUsersDefault读取,写入和读取数据。
On my main ViewController I can read data with: 在主ViewController上,我可以使用以下命令读取数据:

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 您需要一个DefaultsSet类的实例

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. 变量名称setOfDefaults专门用于查看差异。

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) . 从您发布的代码,看来你要么需要使write方法的类方法(只使用前缀class ),或者你需要调用它的一个实例DefaultsSetDefaultsSet().write(defaultCityName.text, switchState)

Another issue I found is that you also need to unwrapp the value of the textField. 我发现的另一个问题是,您还需要解包textField的值。 Your write method takes as parameters a String and a Bool , but the value of defaultCityName.text is an optional, so String? 您的write方法将StringBool作为参数,但是defaultCityName.text的值是可选的,因此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 让我知道这是否可以帮助您解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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