2016-03-01 86 views
0

Im Swift新。我怎樣才能聲明一個靜態類,所以我不必實例化它。swift - 如何在項目中聲明一個靜態類

我將創建一個主題類,所以在那裏我可以設置所有我的色彩,圖像等

是否有任何的裝飾,如:

static class Theme { 
} 

感謝

+0

的[?你如何在迅速靜態類(可能的複製http://stackoverflow.com/questions/26472801/how尚北道任您做出-A-靜態類中,SWIFT) –

回答

1

這不是階級本身的內容必須標記爲靜態

class Theme { 

    // static constant 
    static let foo = "foo" 

    // static variable 
    static var foo2 : String { return "foo2" } 

    // static method 
    class func bar(x: Int) -> Int 
    { 
    return 2 * x 
    } 
} 

let a = Theme.foo // "foo" 
let b = Theme.foo2 // "foo2" 

let y = Theme.bar(10) // 20 
0

static mea NS沒有實例,所以我會讓它沒有初始化一個結構:

struct Theme { 
    @available(*, unavailable) private init() {} 

    static var foo = "foo" 

    static func doSomething() { 
     //... 
    } 
} 
相關問題