Swift: Optional variable

This entry is part 35 of 41 in the series Swift

For Swift, when you declare a variable, you need to provide an initial value.  If the value is unknown, you can declare it as an optional.

var name:String?

//name = "abcde"

if name != nil {
    print("Your name: \(name!)")
    
} else {
    print("There is no value in the Name")
    
}

swift

Swift: Reflection API

The code here is based on this tutorial.

import UIKit

public class Store {
    let storesToDisk: Bool = true
}
public class BookStore: Store {
    let itemCount: Int = 10
}
public struct Bookmark {
    enum Group {
        case Tech
        case News
    }
    private let store = {
        return BookStore()
    }()
    let title: String?
    let url: NSURL
    let keywords: [String]
    let group: Group
}

let aBookmark = Bookmark(title: "Swift Tutorials", url: NSURL(string: "yahoo.com")!, keywords: ["Swift", "iOS", "OSX"], group: .Tech)

let aMirror = Mirror(reflecting: aBookmark)
print(aMirror.displayStyle)
print(aMirror.subjectType)

for case let (label?, value) in aMirror.children {
    print (label, value)
}

let closure = { (a: Int) -> Int in return a * 2 }
let bMirror = Mirror(reflecting: closure)
print(bMirror.displayStyle)

print(aMirror.subjectType)
print(Mirror(reflecting: 5).subjectType)
print(Mirror(reflecting: "Test").subjectType)
print(Mirror(reflecting: NSNull()).subjectType)

let cMirror = Mirror(reflecting: aMirror)
print(cMirror.displayStyle)
print(cMirror.subjectType)

for case let (label?, value) in cMirror.children {
    print (label, value)
}

swift