# How do I make this type optional?

**URL:** <https://forums.swift.org/t/how-do-i-make-this-type-optional/11875>\
**Category:** Using Swift\
**Created:** [April 13, 2018, 6:38pm UTC](https://forums.swift.org/t/how-do-i-make-this-type-optional/11875 "2018-04-13T18:38:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fnovello](https://avatars.discourse-cdn.com/v4/letter/f/e19adc/32.png) [@fnovello](https://forums.swift.org/u/fnovello)\
**Post date:** [April 13, 2018, 6:38pm UTC](https://forums.swift.org/t/how-do-i-make-this-type-optional/11875/1 "2018-04-13T18:38:46Z")

</div>

```
let ability1? = dict[0] as! [String: AnyObject]
let ability2? = dict[1] as! [String: AnyObject]

```

---

<div class="post-metadata">

**Author:** ![anthonylatsis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anthonylatsis/32/39232_2.png) [@anthonylatsis](https://forums.swift.org/u/anthonylatsis)\
**Post date:** [April 13, 2018, 6:41pm UTC](https://forums.swift.org/t/how-do-i-make-this-type-optional/11875/2 "2018-04-13T18:41:59Z")

</div>

Since you are dealing with JSON,

```swift
let ability1 = dict[0] as? [String: AnyObject]
let ability2 = dict[1] as? [String: AnyObject]

```

`arg as! T` - forced cast, returns `T` if it succeeds, otherwise `fatalError`.  
`arg as? T` - conditional cast, returns `T?`, which is `nil` if the cast fails.

FYI: [Type Casting](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html)

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [April 13, 2018, 10:11pm UTC](https://forums.swift.org/t/how-do-i-make-this-type-optional/11875/3 "2018-04-13T22:11:47Z")

</div>

You can also use `dict[0] as! [String: AnyObject]?` to say "I know this is either `nil` or this specific dictionary type".
