Xcode 12.5 beta 3, Swift 5.3 fails to check Any for nil value

kas-kad

I have the following test that succeeds in all Xcode versions released for last few years except for Xcode 12.5 beta 3:

var nilString: String? = nil
var dict: [String: Any] = ["hello": nilString as Any]
var element = dict["hello"] as Any
print(element)
print(type(of: element))

switch element {
    case Optional<Any>.none:
        print("element is nil")
    default:
        assertionFailure("element is not nil. wtf?")
}

In Xcode 12.4 I get this printed

Optional(nil)
Optional<Any>
element is nil

Wheras in Xcode 12.5 beta 3 I get this printed

Optional(nil)
Optional<Any>
Fatal error: element is not nil. wtf?

Can somebody shed some light on this issue? How can I check Any for nil now? Would it be fixed in release candidate?

UPDATED Want to be confused even more?

var nilString: String? = nil
var element: Any = nilString as Any
print(element)
print(type(of: element.self))

switch element {
    case Optional<Any>.none:
        print("element is nil")
    default:
        assertionFailure("element is not nil")
}

outputs:

nil
Optional<String>
element is nil

I suppose something wrong happens with my nilString when being put in dictionary.

kas-kad

Thanks to Jessy for pointing out on the idea that Optional<Any> may actually have some value inside and that could be nil. I've come up with a function that checks Any for nil and it works in both old and newest Xcode (something has definitely been changed in 12.5 beta 3 that had broken the old test, i.e. putting nil casted to Any into a dictionary value leads the value to be wrapped into Optional<Any>.some instead of treating it as Optional<Any>.none):

func isOptional(_ instance: Any) -> Bool {
    let mirror = Mirror(reflecting: instance)
    let style = mirror.displayStyle
    return style == .optional
}

func checkIfAnyIsNil(_ v: Any) -> Bool {
    if (isOptional(v)) {
        switch v {
        case Optional<Any>.none:
            return true
        case Optional<Any>.some(let v):
            return checkIfAnyIsNil(v)
        default:
            return false
        }
    } else {
        return false
    }
}

assertions:

var nilString: String? = nil
var dict: [String: Any] = ["nilString": nilString as Any]

assert(checkIfAnyIsNil(dict["nilString"] as Any))
assert(checkIfAnyIsNil(nilString as Any))
assert(checkIfAnyIsNil("some string" as Any) == false)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

check if any property in an object is nil - Swift 3

How to successfully import AVFoundation using Swift in Xcode 12 beta 3?

UIView initializer swift Xcode 6 beta 5

Observable errors with Angular2 beta.12 and RxJs 5 beta.3

iOS Swift3 check nil value for ViewController Object

All IBOutlets become nil after switching to Xcode 6 Beta 5

Swift 2.0 beta: are protocols kinda broken in Xcode beta 5?

nil Check in User Defaults (Swift 5)

AudioKit 5 and Xcode 12 beta 6 Build Errors

Dictionary<AnyHashable: Any> where Any may hold nil value in Swift 3

Object != nil works in Beta 5 but not in Beta 6?

XCode 12 Beta 3 SwiftUI Space between sections on a form

Xcode 12 Beta 3: App Store Connect Operation Error

How to Check if 3 Variables are equal or not equal to each other in Swift 5?

"Cannot assign value of type 'String' to type 'AnyObject?'", Swift 3, Xcode 8 beta 6

Xcode6 beta 5 takes all empty memory, Swift

Parse signup error with swift 2 (xcode 7 beta 5)

.enumerateGroupsWithTypes block stop parameter Swift (Xcode 6 beta 5)

Xcode 7 beta 5 Swift 2 redundant conformance to protocol error

Swift : Issue with saving contact with AddressBook - (Xcode6-Beta5)

how to upload image (Multipart) using Alamofire 5.0.0-beta.3 (Swift 5)

Dictionary value always nil in swift 3

How to check Multiple string is not nil and not empty in swift 5?

Xcode 8 Beta 3 Use Legacy Swift issue

Xcode 8 beta 6 AnyObject Swift 3 changes

Swift 3 UnsafePointer($0) no longer compile in Xcode 8 beta 6

InputAccessoryView Not showing - Swift 3 Xcode 8.3 beta (Non storyBoard)

Xcode 8 Beta 4 Swift 3 - "round" behaviour changed

ios swift 3 xcode8 beta rounded imageView

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive