2016-03-01 26 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() { 
     //... 
    } 
} 
相关问题