I get the error in Swift and don't understand it when I do this:
if(currentUser["employer"] as! Bool == false) { print("employer is false: "+currentUser["employer"] as! Bool) }
But I can do (Its not actually printing anything though, maybe another problem):
if(currentUser["employer"] as! Bool == false) { print(currentUser["employer"]) }
Results in error:
Binary operator '+' cannot be applied to operands of type 'String' and 'AnyObject'
Similarly:
let currentUser = PFUser.currentUser()!
let isEmployer = currentUser["employer"]
print("isEmployer: \(isEmployer)")
print(currentUser["employer"])
But these two don't work:
print("employer: "+currentUser["employer"])
print("employer: \(currentUser["employer"])")
I also happen to be using Parse to get data, and not sure if that is the right way either.
The error message might be misleading in the first example
if currentUser["employer"] as! Bool == false { print("employer is false: "+currentUser["employer"] as! Bool) }
In this case, the error message is supposed to be
binary operator '+' cannot be applied to operands of type 'String' and 'Bool'
because currentUser["employer"] as! Bool
is a non-optional Bool
and cannot be implicitly cast to String
Those examples
print("employer: "+currentUser["employer"]) print("employer: \(currentUser["employer"])")
don't work because
currentUser["employer"]
without any typecast is an optional AnyObject
(aka unspecified) which doesn't know a +
operator. "employer"
within the String interpolated expression causes a syntax error (which is fixed in Xcode 7.1 beta 2). Edit:
This syntax is the usual way.
let isEmployer = currentUser["employer"]
print("isEmployer: \(isEmployer)")
Or alternatively, you can write
print("employer is " + String(currentUser["employer"] as! Bool))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With