swift - Why does the compiler tell me I need to unwrap a Bool (non-optional?) -


according the swift header string

the property isempty bool (not optional right?)

public var isempty: bool { } 

but in code, when try write:

!sender.titleofselecteditem?.isempty 

value of optional type bool? not unwrapped, did mean use "?" or "!"

why compiler think isempty optional?

is because object contains property optional?

titleofselecteditem string?

or missing entirely, here?

sender.titleofselecteditem?.isempty optional chain , has type bool? because can return nil if titleofselecteditem nil.

you should decide how want handle nil case. can combine optional chain nil coalescing operator ?? safely unwrap result:

// treat nil empty let empty = sender.titleofselecteditem?.isempty ?? true 

or can compare value false:

if sender.titleofselecteditem?.isempty == false {     // value isn't nil , isn't empty } 

or can compare value true:

if sender.titleofselecteditem?.isempty == true {     // value isn't nil , empty string } 

Comments

Popular posts from this blog

Ansible warning on jinja2 braces on when -

Parsing a protocol message from Go by Java -

html - How to custom Bootstrap grid height? -