Why is Swift nil-coalescing returning an Optional?

J. Cocoe

First, I try mapping a [String?], to get a [String]:

$ xcrun swift
Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.30). Type :help for assistance.
  1> import Foundation
  2> let j: [String?] = ["a", nil]
j: [String?] = 2 values {
  [0] = "a"
  [1] = nil
}
  3> j.map {$0 ?? ""}
$R0: [String] = 2 values {
  [0] = "a"
  [1] = ""
}

This makes perfect sense to me. I nil-coalesce a String?, and I get a String. But with [AnyObject?], something strange occurs:

  4> let k: [AnyObject?] = ["a", nil]
k: [AnyObject?] = 2 values {
  [0] = "a"
  [1] = nil
}
  5> k.map {$0 ?? ""}
$R1: [AnyObject?] = 2 values {
  [0] = "a"
  [1] = (instance_type = 0x00007fff7bc2c140 @"")
}

I'm nil-coalescing optionals, but this time I get out an optional. Why?

The Swift Programming Language says a ?? b is shorthand for a != nil ? a! : b, but when I try that, I get out an array of non-optionals:

  6> k.map {$0 != nil ? $0! : ""}
$R2: [AnyObject] = 2 values {
  [0] = "a"
  [1] = ""
}

Am I misunderstanding how ?? is supposed to work? What is going on here?

J. Cocoe

It has come to my attention that Apple considered this a bug in Swift 2.

In Swift 3, the 1st example above still works, while the 2nd and 3rd examples are invalid syntax (with or without Foundation bridging).

Replacing the AnyObject declaration with Any works: a ?? b then behaves identically to a != nil ? a! : b, as the documentation says.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is the nil coalescing operator ?? returning nil?

Swift: compile-time error with optional chaining and nil-coalescing

swift nil coalescing with doubles

Nil Coalescing Operator Swift

Swift nil coalescing operator with array

Why is a Swift implicitly unwrapped optional `nil`?

Why assigning nil to an optional variable is true in Swift?

Why is my Swift string variable returning nil?

Why doesn't Swift nil-coalescing ternary operator return unwrapped type?

Using nil-coalescing operator with try? for function that throws and returns optional

What is the role of the nil-coalescing operator "??" in optional chaining?

Optional value returning nil with ?? operator

Swift why unwrapping optional by guarding against NIL is not working

Swift: Coding rule of optional. Which is swift's style returning nil or []

Why this function is returning nil?

Swift: navigationController returning nil

Swift: DateFormatter returning nil

Why does the nil coalescing operator wrap an implicitly unwrapped default value?

Returning a nil from an optional generic extension

Is Nil-Coalescing always needed when using firstIndex(of: Character) in Swift?

Coalescing Swift operator for an empty non-nil string

Swift 4.2, Xcode 10.2 nil coalescing operator warning

Swift optional inout parameters and nil

Firebase returning Optional() with Swift 3.0

why is my gestureRecognizer returning nil?

Left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used

NSKeyedArchiver returning nil Swift 4.2

Swift protocol delegate returning nil

Swift viewcontroller declaration returning nil