Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional("var") in println

Tags:

var

swift

println

I got the Problem if i try print a var i sometimes get a "Optional("var")" if i try log it or print it to a lable, but else when i dont get it.

See here

ViewController.swift

import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        NSUserDefaults.standardUserDefaults().setObject("notWorking", forKey: "testvar")
        NSUserDefaults.standardUserDefaults().synchronize()
        println(NSUserDefaults.standardUserDefaults().stringForKey("testvar"))
        var test = "working"
        println(test)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

Anyone can explain when this "Optional" appears and what its use for? How can i remove it?

like image 900
Fabian Boulegue Avatar asked Aug 25 '14 11:08

Fabian Boulegue


1 Answers

oki just need to add a "!"

println(NSUserDefaults.standardUserDefaults().stringForKey("testvar")!)

like image 173
Fabian Boulegue Avatar answered Dec 30 '22 10:12

Fabian Boulegue