How to remove Optional from String Value Swift

后端 未结 3 712
不思量自难忘°
不思量自难忘° 2020-12-12 06:21

I\'d like to use a String value without the optional extension. I parse this data from firebase using the following code:

Database.database().reference(withP         


        
3条回答
  •  有刺的猬
    2020-12-12 07:19

    Optional is because you're printing an optional value ?.

    print("Value is", laststring!)

    Above code will not print Optional. But avoid forcecasting and use guard for safety.

    guard let value = lastString else {return}
    print("Value is", value)
    

    Or you can use Nil Coalescing operator.

    print("Value is", laststring ?? "yourDefaultString")
    

    So in case laststring is nil it will print yourDefaultString.

    Important message for you, Use Dictionary instead of NSDictionary, use Array instead of NSArray this is swift.

提交回复
热议问题