8
@IBOutlet var navBar: UINavigationBar!
@IBOutlet var menuButton: UIBarButtonItem!
@IBOutlet var topicNameLabel: UILabel!
var topicName:String!
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    menuButton.target = self.revealViewController()
    menuButton.action = Selector("revealToggle:")

    navBar.barTintColor = UIColor(red: 0, green: 0.4176, blue: 0.4608, alpha: 1)
    topicNameLabel.text = self.topicName

}

That's my code, I will pass a string to the topicName by prepareForSegue, however, I find that in the simulator, my topicNameLabel shows "Optional(The text I want)". I just want the "The text I want", but do not need the Optional. Could any one help me?

5
  • 1
    You can use the nil coalescing operator: topicNameLabel.text = self.topicName ?? "". If you optional variable topicName is nil, a default empty text "" will be shown, whereas if it's non-nil, the value will be unwrapped and assigned to your label text. Also, you probably want to declare your String variable as var topicName : String?, rather than String!. Commented Jan 27, 2016 at 20:10
  • Where do you set the value of the topicName property? Commented Jan 27, 2016 at 20:14
  • It will always be optional if you declare it this way but don't worry, it won't change its behaviour in any way except if you forget to initialize it. If you pass it or print it, it will act normal. But if it is that big of a deal for you, you can give it default value of "" Commented Jan 27, 2016 at 20:15
  • Consider to declare topicName as non-optional empty string. An "absent" value on a label is an empty string anyway. Commented Jan 27, 2016 at 20:25
  • I try to use "topicNameLabel.text = self.topicName!", and "var topicName:String?" However, that still does not work. :-( Commented Jan 27, 2016 at 22:16

3 Answers 3

10

Optional string means that the string may be nil. From "The Basics" in the Swift Programming Language

Swift also introduces optional types, which handle the absence of a value.

When you print an optional string on console it will tell you that it is an optional. So the value of the string dos not contain the "Optional" keyword...

For example

var str : String?
str = "Hello" // This will print "Optional("Hello")"
print(str)
print(str!) // This will print("Hello") 

But str value is "Hello" . It is an optional string

Sign up to request clarification or add additional context in comments.

2 Comments

I try to edit that to "topicNameLabel.text = self.topicName!" However, that still not works. :-(
The OP defined topicName ad an implicitly unwrapped String, this is the declaration var topicName:String! and he is using it simply writing topicName. It's the same result of declaring it as String? and then performing a force unwrap as you are doing.
5

The problem is not here

Your property is declared as an implicitly unwrapped optional String.

var topicName: String!

So when you use it the value is automatically unwrapped.

Example:

var topicName:String!
topicName = "Life is good"
print(topicName)

Output

Life is good

As you can see there is no Optional(Life is good) in the output. So this code is correct.

My theory

My guess is that you are populating topicName with a String that already contains the Optional(...) word.

This is the reason why you are getting the Optional(...) in the output.

Testing my theory

To test this scenario let's add an observer to your property

willSet(newValue) {
    print("topicaName will be set with this: \(newValue)")
}

I expect you will see something like this in the log

topicaName will be set with this: Optional(Hello)

Finding the real problem (aka who is writing the String 'Optional("Hello")'?)

If this does happen just put a breakpoint in the observer and find the instruction in your project that is writing the String Optional("Hello") in your property.

9 Comments

I tried, however, there is some thing strange, print("StackOverflow told me this (topicName)") However, it prints StackOverflow told me this Optional("Optional(My baby don\'t eat food)")
@HanslenChen: Did you create the property observer?
I find my problem 0.0 But I still have some questions to ask that let topic:String = "Recruiting a private doctor" self.performSegueWithIdentifier("topicDetailSegue", sender: topic) I use these to pass the topic string to my destination viewController, actually the topic is the topicName, and the type is string!, why it will come with an Optional?
@HanslenChen: then why are you printing topicName? In my instructions I used this code willSet(newValue) { print("topicaName will be set with this: \(newValue)") }. Please follow my instruction and tell me the result.
Also in the prepareForSegue I have, [ var topic:String topic = String(sender) topicVC.topicName = topic as String!]
|
3

I think your problem is your string really contains with a "optional", you can try indexAt(0) to see is "o" or not.

1 Comment

Thank you very much. This is the case with me. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.