在Swift 3.0中分享WhatsApp错误:上下文成员'urlHostAllowed'没有关联值

Ram*_*oun 2 swift swift3

我刚刚将我的swift项目升级为swift 3.

我一直在使用以下功能在Whatsapp上共享应用程序,但我无法理解升级后发生的错误

这是功能代码:

func shareOnWhatsapp() {
    let urlString = "Greetings,\n\nThis is the XYZ App link, I hope you find it useful!\n\nhttp://itunes.apple.com/app/idxxxxxxxx"
    let urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed())
    let url  = URL(string: "whatsapp://send?text=\(urlStringEncoded!)")

    if UIApplication.shared.canOpenURL(url!) {
        UIApplication.shared.openURL(url!)
    }

}
Run Code Online (Sandbox Code Playgroud)

错误说:

Contextual member 'urlHostAllowed' has no associated value
Run Code Online (Sandbox Code Playgroud)

知道如何解决这个问题吗?

rea*_* 19 5

你需要摆脱第三行中的"()":

let urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed())
Run Code Online (Sandbox Code Playgroud)

对此:

let urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
Run Code Online (Sandbox Code Playgroud)