.
if let name = optionalName {
.

Essentially the line is saying, “if you can let the new variable name equal the non-optional version of optionalName, do the following with it”. As Martin pointed out, this is called Optional Binding.

The sole purpose of it is to test if an optional variable contains an actual value and bind the non-optional form to a temporary variable. This is the safe way to “unwrap” an optional or in other words, access the value contained in the optional. It is in no way testing for equality of any kind. It is only testing for the existence of a value within an optional.

http://stackoverflow.com/questions/25828301/how-is-swift-if-let-evaluated

So instead of doing this

let previewUrl = item["preview_url"] as! String

Do this

let optionalPreviewUrl = item["preview_url"]

if var unwrappedPreviewUrl = optionalPreviewUrl {
   if !(unwrappedPreviewUrl is NSNull) {
      previewUrl = unwrappedPreviewUrl as! String
   }
}

Note: unwrappedPreviewUrl can still be NSNULL object. So you may need one more check before you can use.

Advertisements