4

I tried out the following code in playground

import UIKit

let label = UILabel()
let textView = UITextView()

let labelText = label.text           // nil
let textViewText = textView.text     // ""

If I Option click lableText, the type is String?.
If I Option click textViewText, the type is String!.

Why is the Label text an optional while the TextView text isn't?

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • In what exact version of Xcode? – nhgrif Aug 09 '15 at 23:46
  • I got the same results testing this in Xcode 6.4 and Xcode 7.0 beta. – Suragch Aug 09 '15 at 23:48
  • 5
    Ultimately, the answer is that Apple hasn't finished updating all of their libraries to use [Objective-C nullability annotations](http://stackoverflow.com/a/29401454/2792531). Eventually, there should be no implicitly unwrapped optionals (`!`) left in the Apple libraries (by the time they're done). – nhgrif Aug 09 '15 at 23:49
  • Apparently this still stands as a legitimate question in 2023. @nhgrif if Apple was working on updating these values they would have done it already. – alobaili Feb 23 '23 at 12:39

0 Answers0