How to unwrap an optional value from Any type?

mythz

Given an array of [Any] that has a mix of optional and non optional values, e.g:

let int:Int? = 1
let str:String? = "foo"

let values:[Any] = [int,2,str,"bar"]

How can we extract the value of the Optional in the Any type (if there is one) so we can create a generic print function that only prints out the values.

E.g. this printArray function goes through and prints each element:

func printArray(values:[Any]) {
    for i in 0..<values.count {
        println("value[\(i)] = \(values[i])")
    }
}

printArray(values)

Which will output:

value[0] = Optional(1)
value[1] = 2
value[2] = Optional("foo")
value[3] = bar

How can we change it so it only prints the underlying value so that it unwraps the value if it's Optional? e.g:

value[0] = 1
value[1] = 2
value[2] = foo
value[3] = bar

Update Progress...

It can work when changing the argument to [Any?], e.g:

let values:[Any?] = [int,2,str,"bar"]

func printArray(values:[Any?]) {
    for i in 0..<values.count {
        println("value[\(i)] = \(values[i]!)")
    }
}

printArray(values)

Which will print the desired:

value[0] = 1
value[1] = 2
value[2] = foo
value[3] = bar

But would still like to see how we can unwrap an Optional from Any as this is what MirrorType.value returns making it difficult to extract the Optional value, e.g:

class Person {
    var id:Int = 1
    var name:String?
}

var person = Person()
person.name = "foo"

var mt:MirrorType = reflect(person)
for i in 0 ..< mt.count {
    let (name, pt) = mt[i]
    println("\(name) = \(pt.value)")
}

Prints out:

id = 1
name = Optional("foo")

When I need:

id = 1
name = foo
bubuxu

For Xcode 7 and Swift 2:

func unwrap(any:Any) -> Any {

    let mi = Mirror(reflecting: any)
    if mi.displayStyle != .Optional {
        return any
    }

    if mi.children.count == 0 { return NSNull() }
    let (_, some) = mi.children.first!
    return some

}


let int:Int? = 1
let str:String? = "foo"
let null:Any? = nil
let values:[Any] = [unwrap(int),2,unwrap(str),"bar", unwrap(null)]

This will give you [1, 2, "foo", "bar", {NSObject}]

Change NSNull() to nil and the return value of unwrap func to Any? will always unwrap any type.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot force unwrap value of non-optional type: Avoid "Optional()"

From Dev

"Cannot force unwrap value of non-optional type 'Int'" error?

From Dev

How to unwrap optional value returns nil although it has value?

From Dev

How to use string variable without unwrap optional value

From Dev

fatal error: unexpectedly found nil while unwrapping an Optional value (cannot force unwrap value of non-optional type 'String')

From Java

How to Unwrap Type of a Promise

From Dev

Unwrapping Optional Type Any from Dictionary

From Dev

How can I write a function that will unwrap a generic property in swift assuming it is an optional type?

From Dev

How to unwrap a chain of Optionals from PFUser to access a Boolean value

From Dev

How to unwrap the content value from XML in Objective-C

From Dev

How do I safely unwrap this URL optional which i call from my database in Firebase?

From Dev

Is there a way to unwrap a type from an IO monad?

From Dev

Is there better way to unwrap record from sum type?

From Dev

Is there a way to unwrap a type from an IO monad?

From Dev

Optional null value with a type

From Dev

Determine if Any.Type is Optional

From Dev

How to retrieve a value from HashMap with optional argument

From Dev

How do I unwrap an Optional when pattern matching tuples in Swift?

From Dev

How to unwrap optional tuple of options to tuple of options in Scala?

From Dev

Unwrap an Optional only if it is present

From Dev

Unwrap variable which is not optional?

From Dev

Returning Tuple From Delegate (Value of optional type not unwrapped)

From Dev

Returning Tuple From Delegate (Value of optional type not unwrapped)

From Dev

How to get the unwrapped type from an optional type in Swift?

From Dev

how to unwrap union value in list in f#

From Dev

How to return any type value like -performSelector?

From Dev

How to return any type value like -performSelector?

From Dev

How to code optional default annotation value for Annotation TYPE

From Dev

Swift: how to set function type value to optional variable?

Related Related

  1. 1

    Cannot force unwrap value of non-optional type: Avoid "Optional()"

  2. 2

    "Cannot force unwrap value of non-optional type 'Int'" error?

  3. 3

    How to unwrap optional value returns nil although it has value?

  4. 4

    How to use string variable without unwrap optional value

  5. 5

    fatal error: unexpectedly found nil while unwrapping an Optional value (cannot force unwrap value of non-optional type 'String')

  6. 6

    How to Unwrap Type of a Promise

  7. 7

    Unwrapping Optional Type Any from Dictionary

  8. 8

    How can I write a function that will unwrap a generic property in swift assuming it is an optional type?

  9. 9

    How to unwrap a chain of Optionals from PFUser to access a Boolean value

  10. 10

    How to unwrap the content value from XML in Objective-C

  11. 11

    How do I safely unwrap this URL optional which i call from my database in Firebase?

  12. 12

    Is there a way to unwrap a type from an IO monad?

  13. 13

    Is there better way to unwrap record from sum type?

  14. 14

    Is there a way to unwrap a type from an IO monad?

  15. 15

    Optional null value with a type

  16. 16

    Determine if Any.Type is Optional

  17. 17

    How to retrieve a value from HashMap with optional argument

  18. 18

    How do I unwrap an Optional when pattern matching tuples in Swift?

  19. 19

    How to unwrap optional tuple of options to tuple of options in Scala?

  20. 20

    Unwrap an Optional only if it is present

  21. 21

    Unwrap variable which is not optional?

  22. 22

    Returning Tuple From Delegate (Value of optional type not unwrapped)

  23. 23

    Returning Tuple From Delegate (Value of optional type not unwrapped)

  24. 24

    How to get the unwrapped type from an optional type in Swift?

  25. 25

    how to unwrap union value in list in f#

  26. 26

    How to return any type value like -performSelector?

  27. 27

    How to return any type value like -performSelector?

  28. 28

    How to code optional default annotation value for Annotation TYPE

  29. 29

    Swift: how to set function type value to optional variable?

HotTag

Archive