{"id":462,"date":"2014-08-23T23:04:46","date_gmt":"2014-08-23T14:04:46","guid":{"rendered":"https:\/\/blog.ymyzk.com\/?p=462"},"modified":"2015-04-10T14:17:55","modified_gmt":"2015-04-10T05:17:55","slug":"swift-singleton","status":"publish","type":"post","link":"https:\/\/blog.ymyzk.com\/2014\/08\/swift-singleton\/","title":{"rendered":"Swift \u3067 Singleton \u30d1\u30bf\u30fc\u30f3\u3092\u5b9f\u88c5\u3059\u308b (Swift 1.2 \u5bfe\u5fdc)"},"content":{"rendered":"

Swift \u3067 Singleton (\u30b7\u30f3\u30b0\u30eb\u30c8\u30f3) \u30d1\u30bf\u30fc\u30f3\u3092\u5b9f\u88c5\u3059\u308b\u65b9\u6cd5\u306f\u3044\u304f\u3064\u304b\u63d0\u6848\u3055\u308c\u3066\u3044\u307e\u3059\u304c\b, \u3053\u3053\u3067\u306f Swift 1.2 \u4ee5\u964d\u3067\u5229\u7528\u3067\u304d\u308b\u65b9\u6cd5\u3068 Xcode 6 Beta 4 \u304b\u3089\u5229\u7528\u51fa\u6765\u308b\u53e4\u3044\u65b9\u6cd5\u306e2\u3064\u3092\u7d39\u4ecb\u3057\u307e\u3059.<\/p>\n

<\/p>\n

Xcode 6.3 \/ Swift 1.2 \u4ee5\u964d<\/h2>\n

\u30b3\u30fc\u30c9<\/h3>\n
\r\nclass SingletonSampleClass {\r\n    private init() {\r\n        \/\/ Initialization here\r\n    }\r\n    \r\n    static let sharedInstance = SingletonSampleClass()\r\n}\r\n<\/pre>\n

\u8aac\u660e<\/h3>\n
\r\n    private init() {\r\n        \/\/ Initialization here\r\n    }\r\n<\/pre>\n

\u30a4\u30cb\u30b7\u30e3\u30e9\u30a4\u30b6\u306f private \u306b\u3057\u3066\u304a\u304d, sharedInstance \u7d4c\u7531\u3067\u306e\u30a2\u30af\u30bb\u30b9\u3092\u5f37\u5236\u3057\u307e\u3059. \u305f\u3060\u3057 Swift \u306e private \u306e\u30b9\u30b3\u30fc\u30d7\u306f\u540c\u3058\u30d5\u30a1\u30a4\u30eb\u5185\u3068\u306a\u308b\u306e\u3067, \u540c\u3058\u30d5\u30a1\u30a4\u30eb\u5185\u304b\u3089\u306f\u30a4\u30cb\u30b7\u30e3\u30e9\u30a4\u30b6\u3092\u547c\u3073\u51fa\u305b\u3066\u3057\u307e\u3046\u3053\u3068\u306b\u6ce8\u610f\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059.<\/p>\n

\r\n    static let sharedInstance = SingletonSampleClass()\r\n<\/pre>\n

Swift 1.2 \u304b\u3089\u30af\u30e9\u30b9\u5185\u306b let \u3067\u5b9a\u6570\u3092\u5ba3\u8a00\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3057\u305f. \u307e\u305f Swift \u306e\u4ed5\u69d8\u306b\u3088\u308a static \u5909\u6570\u3067\u3042\u308b singletonInstance \u306f, \u6700\u521d\u306b\u53c2\u7167\u3055\u308c\u305f\u6642\u306b SingletonSampleClass \u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u4f5c\u6210\u3055\u308c\u307e\u3059.<\/p>\n

Xcode 6 Beta 4 \u4ee5\u964d<\/h2>\n

\u53e4\u3044\u60c5\u5831\u3067\u3059\u304c, Xcode 6 Beta 4 \u304b\u3089\u5c0e\u5165\u3055\u308c\u305f Access Control \u3068\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u306e\u9045\u5ef6\u521d\u671f\u5316\u3092\u7528\u3044\u305f\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3059.<\/p>\n

\u30b3\u30fc\u30c9<\/h3>\n
\r\nprivate let singletonInstance = SingletonSampleClass()\r\n\r\nclass SingletonSampleClass {\r\n    private init() {\r\n        \/\/ Initialization here\r\n    }\r\n\r\n    class var sharedInstance: SingletonSampleClass {\r\n        return singletonInstance\r\n    }\r\n}\r\n<\/pre>\n

\u8aac\u660e<\/h3>\n
\r\nprivate let singletonInstance = SingletonSampleClass()\r\n<\/pre>\n

Singleton \u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4fdd\u6301\u3059\u308b\u5b9a\u6570\u3092\u5ba3\u8a00\u3057, \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3057\u4fdd\u6301\u3057\u307e\u3059.\u00a0Xcode 6 Beta 4 \u304b\u3089\u5c0e\u5165\u3055\u308c\u305f private \u304c\u4ed8\u52a0\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u5916\u90e8\u304b\u3089\u306f\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u305b\u3093. \u307e\u305f Swift \u306e\u4ed5\u69d8\u306b\u3088\u308a, singletonInstance \u304c\u6700\u521d\u306b\u53c2\u7167\u3055\u308c\u305f\u6642\u306b, SingletonSampleClass \u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u4f5c\u6210\u3055\u308c\u307e\u3059.<\/p>\n

\r\n    private init() {\r\n        \/\/ Initialization here\r\n    }\r\n<\/pre>\n

\u30a4\u30cb\u30b7\u30e3\u30e9\u30a4\u30b6\u306f private \u306b\u3057\u3066\u304a\u304d, sharedInstance \u7d4c\u7531\u3067\u306e\u30a2\u30af\u30bb\u30b9\u3092\u5f37\u5236\u3057\u307e\u3059.<\/p>\n

\r\n    class var sharedInstance: SingletonSampleClass {\r\n        return singletonInstance\r\n    }\r\n<\/pre>\n

sharedInstance \u3068\u3044\u3046\u30d7\u30ed\u30d1\u30c6\u30a3\u3092 Read-Only Computed Property \u3068\u3057\u3066\u5b9a\u7fa9\u3057\u307e\u3059. \u3053\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u304c\u4e0a\u3067\u4f5c\u6210\u3057\u305f\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u8fd4\u3059\u3088\u3046\u306b\u3057\u307e\u3059. class \u3092\u4ed8\u52a0\u3057\u3066\u3044\u308b\u306e\u3067\u3053\u308c\u306f Type Property \u3068\u306a\u308a\u307e\u3059.<\/p>\n

\u53c2\u8003<\/h2>\n