是否有兴趣使用双布尔否定?

时间:2017-07-28 09:17:07

标签: javascript boolean

作为Javascript的初学者,我有一个看似有点奇怪的问题。 我正在使用我在网上找到的外部库,我在其中找到了以下代码:

query.keepSynched(true) //keeps data in sync with database, if you have data persistince on in your appDelegate
query.observeSingleEvent(of: .value, with: { (snapshot) in //notice the changed here
            print(snapshot)
            //Since you want to loop again because there could be multiple
            //messages in that chatroom which all have a unique ID, do this loop:
            let enumerator = snapshot.children
            while let rest = enumerator.nextObject() as? FIRDataSnapshot {
               //this is 1 single message here
               let values = rest.value as? NSDictionary
               for (key, value) in values{
               print("Key: \(key), value: \(value)") 
               }
               //lets say you want to check if there is a value with a key named "text":
            let textUser = values?["text"] as? String ?? "No text found" 
               //providing a default value if there is no text. You could leave it empty, than it is nil (not recommend)
               //Not only is this a lot more readable, I do not use force unwrapping so your app 
               //cannot cause an exception.

               //Get more values from the dictionary as I did with textUser
               //Initialize message than here. You will see you do not need to force unwrap anything :D
               }

            }

        })

是否有兴趣使用双重感叹号或只是一些笨拙的代码?

找不到任何答案,所以我想我可以删除它们,但我想100%肯定。

1 个答案:

答案 0 :(得分:3)

使用双isOpen = true,您强制对象返回“ boolean-able ”的内容,即使LinearLayout.setVisibility(VISIBLE)或其他内容,也可以将其评估为bool !null