Reputation: 10022
So I'm trying to call the function "submitBoilerPlate" in Swift:
var url:String = "https://eamorr.com/ajax/getLatestContent.php"
var params = [
"hello":"world"
]
submitBoilerPlate(url, _params:params, _button:nil)
And here is the corresponding function:
func submitBoilerPlate(_url:String, _params:[String: String!], _button:UIButton?){
var oldTitle = _button?.titleLabel?.text
_button?.setTitle("Please wait...", forState: UIControlState.Normal)
_button?.enabled = false
//etc.
...
}
Everything works as expected when I send a real UIButton that's declared on my xib.
However, I want to be able to send a "nil" button (this particular xib doesn't have a button).
Is this possible? Or do I have to have create a new function? I'm trying to keep code repetition to a minimum...
I get the following run-time error when the code gets to submitBoilerPlate(...):
fatal error: can't unsafeBitCast between types of different sizes
Upvotes: 1
Views: 1044
Reputation: 14030
Pasting your code into a playground is a pretty good way to help yourself debug. I took your code into my own playground and found the following to work:
import UIKit
func submitBoilerPlate(_url:String, _params:[String: String!], _button:UIButton?){
var oldTitle = _button?.titleLabel?.text
_button?.setTitle("Please wait...", forState: UIControlState.Normal)
_button?.enabled = false
}
var url:String = "https://eamorr.com/ajax/getLatestContent.php"
var params:[String: String!] = [
"hello":"world"
]
submitBoilerPlate(url, params, nil)
Notice the explicit type of params
and the removal of your _params
and _button
errors in the submitBoilerPlate
call.
Specifying params
without an explicit type was causing the compiler to not align your bits properly according to the required [String: String!]
parameter type.
Having _params
and _button
as part of your function call is actually an error. When there's no space, there's no name to the parameters. An example of the alternative is:
func submitBoilerPlate(_url:String, someParams _params:[String: String!], andButton _button:UIButton?) {
// .. same func body
}
submitBoilerPlate(url, someParams:params, andButton:nil)
Upvotes: 1
Reputation: 10296
Your problem is not with UIButton?
but with _params:[String: String!]
if you are using it somewhere below in submitBoilerPlate function . Try replacing it with _params:[String: String]
Upvotes: 1