如何在swift中创建一个字符串和函数为键值对的字典

时间:2022-04-01 16:46:23

I am looking to create a dictionary that will have

我想创造一本字典

let urlDict:[String:Func] = ["LOGIN":getLoginURL(), "RESET":getResetPasswordURL()]

func getLoginURL() -> String{
        if (sandbox == true){
            return sb_login_url
        }else{
            return live_login_url
        }
    }


    func getResetPasswordURL() -> String{
        if (sandbox == true){
            return sb_reset_url
        }else{
            return live_reset_url
        }

    }

The purpose of this dict is to get/map functions based on the KEY and as per KEY corresponding function must be called which in turn will return return urls.

此命令的目的是基于键获取/映射函数,并且必须按键调用相应的函数,该函数将返回url。

I have tried naming the dictionary but I am unable to do it

我试过给那本字典命名,但我做不到

let urlDict:[String:Func] = ["LOGIN":getLoginURL(), "RESET":getResetPasswordURL()]
let urlDict:[String:Function] = ["LOGIN":getLoginURL(), "RESET":getResetPasswordURL()]
let urlDict:[String:Functions] = ["LOGIN":getLoginURL(), "RESET":getResetPasswordURL()]

EDIT 1

编辑1

 class Constants{
       private let sb_login_url = "http://IP_ADDRESS_COM/login_with_credentials"
        private let live_login_url = "http://google.com"


        private let sb_reset_url = "http://IP_ADDRESS_COM/forgot_password"
        private let live_reset_url = "http://google.com"


        func getLoginURL() -> String{
            if (sandbox == true){
                return sb_login_url
            }else{
                return live_login_url
            }
        }


        func getResetPasswordURL() -> String{
            if (sandbox == true){
                return sb_reset_url
            }else{
                return live_reset_url
            }

        }
  `    let urlDict: [String: () -> String] = ["LOGIN": Constants.getLog‌​inURL(), "RESET":Constants.getResetPasswordURL()]

    if let getFunc = urlDict[url_key] {
                let url = (getFunc()) // foo}


}

2 个解决方案

#1


1  

You must specify the type of the functions (which are common for both getLoginURL and getResetPasswordURL) for the value slot in the dictionary, namely () -> String, a zero-arguments function returning a String instance.

您必须为字典中的值槽(即()->字符串,一个返回字符串实例的零参数函数)指定函数的类型(对于getLoginURL和getResetPasswordURL都是通用的)。

func getLoginURL() -> String {
    return "foo"
}

func getResetPasswordURL() -> String {
    return "bar"
}

let urlDict: [String: () -> String] = 
    ["LOGIN": getLoginURL, "RESET":getResetPasswordURL]
/*            ^^^^^^^^^^^- note that you do not _call_ the functions,
                           as this would result in a String instance */ 

// get a function reference from the dictionary and invoke it
if let getFunc = urlDict["LOGIN"] {
    print(getFunc()) // foo
}

After your comments below, as well as your edit, it seems you want the get... functions to be class members if your class Constants (i.e., marked static).

在你的评论,以及你的编辑,似乎你想要得到…如果您的类常量(也就是),则函数为类成员。,标志着静态)。

class Constants {
    static var sandbox = true
    // I've just added this to test your example

    private static let sb_login_url = "http://IP_ADDRESS_COM/login_with_credentials"
    private static let live_login_url = "http://google.com"

    private static let sb_reset_url = "http://IP_ADDRESS_COM/forgot_password"
    private static let live_reset_url = "http://google.com"

    static func getLoginURL() -> String {
        if (Constants.sandbox == true){
            return Constants.sb_login_url
        } 
        else {
            return Constants.live_login_url
        } 
    }

    static func getResetPasswordURL() -> String{
        if (Constants.sandbox == true){
            return Constants.sb_reset_url
        } 
        else {
            return Constants.live_reset_url
        }
    }
}

let urlDict: [String: () -> String] = 
    ["LOGIN": Constants.getLoginURL, "RESET": Constants.getResetPasswordURL]

// get a function reference from the dictionary and invoke it
if let getFunc = urlDict["LOGIN"] {
    print(getFunc()) // http://IP_ADDRESS_COM/forgot_password
    Constants.sandbox = false
    print(getFunc()) // http://google.com
}

#2


0  

You could simply create a global typealias to Function prototype and can use it anywhere in your project and you don't need to use () -> String everytime when you create Dictionary.

您可以简单地为函数原型创建一个全局类型别名,并可以在项目中的任何地方使用它,并且在每次创建Dictionary时都不需要使用()->字符串。

public typealias VoidToStringFunctio‌n = () -> String

func getLoginURL() -> String {
return "url"

}

}

func getResetPasswordURL() -> String {
return "password"

}

}

and use it like this

像这样使用

let urlDict : [String: VoidToStringFunctio‌n] = ["LOGIN": getLoginURL, "Password": getResetPasswordURL]

#1


1  

You must specify the type of the functions (which are common for both getLoginURL and getResetPasswordURL) for the value slot in the dictionary, namely () -> String, a zero-arguments function returning a String instance.

您必须为字典中的值槽(即()->字符串,一个返回字符串实例的零参数函数)指定函数的类型(对于getLoginURL和getResetPasswordURL都是通用的)。

func getLoginURL() -> String {
    return "foo"
}

func getResetPasswordURL() -> String {
    return "bar"
}

let urlDict: [String: () -> String] = 
    ["LOGIN": getLoginURL, "RESET":getResetPasswordURL]
/*            ^^^^^^^^^^^- note that you do not _call_ the functions,
                           as this would result in a String instance */ 

// get a function reference from the dictionary and invoke it
if let getFunc = urlDict["LOGIN"] {
    print(getFunc()) // foo
}

After your comments below, as well as your edit, it seems you want the get... functions to be class members if your class Constants (i.e., marked static).

在你的评论,以及你的编辑,似乎你想要得到…如果您的类常量(也就是),则函数为类成员。,标志着静态)。

class Constants {
    static var sandbox = true
    // I've just added this to test your example

    private static let sb_login_url = "http://IP_ADDRESS_COM/login_with_credentials"
    private static let live_login_url = "http://google.com"

    private static let sb_reset_url = "http://IP_ADDRESS_COM/forgot_password"
    private static let live_reset_url = "http://google.com"

    static func getLoginURL() -> String {
        if (Constants.sandbox == true){
            return Constants.sb_login_url
        } 
        else {
            return Constants.live_login_url
        } 
    }

    static func getResetPasswordURL() -> String{
        if (Constants.sandbox == true){
            return Constants.sb_reset_url
        } 
        else {
            return Constants.live_reset_url
        }
    }
}

let urlDict: [String: () -> String] = 
    ["LOGIN": Constants.getLoginURL, "RESET": Constants.getResetPasswordURL]

// get a function reference from the dictionary and invoke it
if let getFunc = urlDict["LOGIN"] {
    print(getFunc()) // http://IP_ADDRESS_COM/forgot_password
    Constants.sandbox = false
    print(getFunc()) // http://google.com
}

#2


0  

You could simply create a global typealias to Function prototype and can use it anywhere in your project and you don't need to use () -> String everytime when you create Dictionary.

您可以简单地为函数原型创建一个全局类型别名,并可以在项目中的任何地方使用它,并且在每次创建Dictionary时都不需要使用()->字符串。

public typealias VoidToStringFunctio‌n = () -> String

func getLoginURL() -> String {
return "url"

}

}

func getResetPasswordURL() -> String {
return "password"

}

}

and use it like this

像这样使用

let urlDict : [String: VoidToStringFunctio‌n] = ["LOGIN": getLoginURL, "Password": getResetPasswordURL]