简体   繁体   English

Swift可选参数功能

[英]Swift optional parameter functions

So I have the following function: 所以我有以下功能:

func remoteCall(_url:String, _params:[String: String]?=nil){
   ...


   ...
   Alamofire.request(.POST, _url, parameters: _params)   //requires _params to be of type [String String!]

   ...
}

As you can see, I can do: 如你所见,我可以这样做:

let url = "https://eamorr.com/remote.php"
remoteCall(url)    //works great!

Or, I can do: 或者,我可以这样做:

let url = "https://eamorr.com/remote.php"
let params = [
    "email": "eamorr@eamorr.com",
    "pword": "secret"
]
remoteCall(url, _params:params)   //works great!

However, what I can't do is: 但是,我不能做的是:

let url = "https://eamorr.com/remote.php"
let params = [
    "email": email.text,   //where "email" is a UITextField
    "pword": pword.text    //where "pword" is a UITextField
]
remoteCall(url, _params:params)   //this doesn't work

I get this error: 我收到此错误:

'String!' is not identical to 'String'

I need to be able to accomodate all three situations (pass nothing, pass raw strings, and pass UITextField values) 我需要能够适应所有三种情况(不传递任何内容,传递原始字符串,并传递UITextField值)

Unfortunately, if I try to change the function signature (note the '!' after the keyword "String") to: 不幸的是,如果我尝试更改函数签名(请注意关键字“String”之后的'!'):

func remoteCall(_url:String, _params:[String: String!]?=nil){
   ...

   ...
}

The UITextField values work, passing nil works, but the raw strings situation fails at run-time. UITextField值工作,传递nil工作,但原始字符串情况在运行时失败。

fatal error: can't unsafeBitCast between types of different sizes

EXC_BAD_INSTRUCTION

I'll be using this function a lot, so I would like not to have to wrap my "params" variable up in messy code. 我将使用这个函数很多,所以我不想在凌乱的代码中包装我的“params”变量。

I'm new to Swift, an Objective-C head, and trying to re-learn everything again... 我是Swift的新手,一个Objective-C头,并试图重新学习一切......

I guess if I can somehow convert any incorrect [String: String] to [String: String!] neatly inside the function without disturbing any nil 's or [String: String!] 's that already work? 我想如果我能以某种方式将任何不正确的[String: String]整齐地转换为函数内部的[String: String!] ,而不会打扰任何已经有效的nil[String: String!]

I think this should work: 我认为这应该有效:

let url = "https://eamorr.com/remote.php"
let params: [String: String] = [ // Note explicitly declared type
    "email": email.text,   //where "email" is a UITextField
    "pword": pword.text    //where "pword" is a UITextField
]
remoteCall(url, _params:params)

Problem in your case was that without explicit type declaration Swift will infer one for you. 在你的情况下,问题是如果没有明确的类型声明,Swift会为你推断一个。 Both values in the dictionary you create are of String! 您创建的字典中的两个值都是String! type, which is different from String . type,与String 不同 Hence the error. 因此错误。 If you explicitly tell Swift that params is of [String: String] type that should work fine because it is ok to assign String! 如果你明确地告诉Swift params[String: String]类型应该工作正常,因为它可以分配String! value to a String variable. value变量为String变量。

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

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