If I declared a String
like this: var date = String()
and I want to check if it is a nil
String
or not, so that I try something like:
if date != nil{
println("It's not nil")
}
But I got an error like : Can not invoke '!=' with an argument list of type '(@lvalue String, NilLiteralConvertible)'
after that I try this:
if let date1 = date {
println("It's not nil")
}
But still getting an error like:
Bound value in a conditional binding must be of Optional type
So my question is how can I check that the String
is not nil
if I declare it this way?
The string can't be nil. That's the point of this sort of typing in Swift.
If you want it to be possibly nil, declare it as an optional:
var date : String?
If you want to check a string is empty (don't do this, it's the sort of thing optionals were made to work around) then:
if date.isEmpty
But you really should be using optionals.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments