├── .gitignore ├── LICENSE ├── README.md ├── Singleton.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── hermespique.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── hermespique.xcuserdatad │ └── xcschemes │ ├── Singleton.xcscheme │ └── xcschememanagement.plist ├── Singleton ├── Info.plist ├── Singleton.h ├── SingletonA.swift ├── SingletonB.swift └── SingletonC.swift └── SingletonTests ├── Info.plist ├── SingletonATests.swift ├── SingletonBTests.swift └── SingletonCTests.swift /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | .DS_Store 3 | */build/* 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | profile 14 | *.moved-aside 15 | DerivedData 16 | .idea/ 17 | *.hmap 18 | *.xccheckout 19 | 20 | #CocoaPods 21 | Pods 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SwiftSingleton 2 | ============== 3 | 4 | _tl;dr: Use the **class constant** approach if you are using Swift 1.2 or above and the **nested struct** approach if you need to support earlier versions._ 5 | 6 | An exploration of the Singleton pattern in Swift. All approaches below support lazy initialization and thread safety. 7 | 8 | Issues and pull requests welcome. 9 | 10 | ### Approach A: Class constant 11 | 12 | ```swift 13 | class SingletonA { 14 | 15 | static let sharedInstance = SingletonA() 16 | 17 | init() { 18 | println("AAA"); 19 | } 20 | 21 | } 22 | ``` 23 | 24 | This approach supports lazy initialization because Swift lazily initializes class constants (and variables), and is thread safe by the definition of `let`. 25 | 26 | Class constants were introduced in Swift 1.2. If you need to support an earlier version of Swift, use the nested struct approach below or a global constant. 27 | 28 | ### Approach B: Nested struct 29 | 30 | ```swift 31 | class SingletonB { 32 | 33 | class var sharedInstance: SingletonB { 34 | struct Static { 35 | static let instance: SingletonB = SingletonB() 36 | } 37 | return Static.instance 38 | } 39 | 40 | } 41 | ``` 42 | 43 | Here we are using the static constant of a nested struct as a class constant. This is a workaround for the lack of static class constants in Swift 1.1 and earlier, and still works as a workaround for the lack of static constants and variables in functions. 44 | 45 | ### Approach C: dispatch_once 46 | 47 | The traditional Objective-C approach ported to Swift. 48 | 49 | ```swift 50 | class SingletonC { 51 | 52 | class var sharedInstance: SingletonC { 53 | struct Static { 54 | static var onceToken: dispatch_once_t = 0 55 | static var instance: SingletonC? = nil 56 | } 57 | dispatch_once(&Static.onceToken) { 58 | Static.instance = SingletonC() 59 | } 60 | return Static.instance! 61 | } 62 | } 63 | ``` 64 | 65 | I'm fairly certain there's no advantage over the nested struct approach but I'm including it anyway as I find the differences in syntax interesting. 66 | -------------------------------------------------------------------------------- /Singleton.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | A0E63744197DBF9C00354078 /* Singleton.h in Headers */ = {isa = PBXBuildFile; fileRef = A0E63743197DBF9C00354078 /* Singleton.h */; settings = {ATTRIBUTES = (Public, ); }; }; 11 | A0E6374A197DBF9C00354078 /* Singleton.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A0E6373E197DBF9C00354078 /* Singleton.framework */; }; 12 | A0E6375D197DBFCA00354078 /* SingletonC.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0E6375A197DBFCA00354078 /* SingletonC.swift */; }; 13 | A0E6375E197DBFCA00354078 /* SingletonB.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0E6375B197DBFCA00354078 /* SingletonB.swift */; }; 14 | A0E6375F197DBFCA00354078 /* SingletonA.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0E6375C197DBFCA00354078 /* SingletonA.swift */; }; 15 | A0E63763197DBFD700354078 /* SingletonCTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0E63760197DBFD700354078 /* SingletonCTests.swift */; }; 16 | A0E63764197DBFD700354078 /* SingletonBTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0E63761197DBFD700354078 /* SingletonBTests.swift */; }; 17 | A0E63765197DBFD700354078 /* SingletonATests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0E63762197DBFD700354078 /* SingletonATests.swift */; }; 18 | A0E63766197DC00E00354078 /* SingletonC.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0E6375A197DBFCA00354078 /* SingletonC.swift */; }; 19 | A0E63767197DC00E00354078 /* SingletonB.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0E6375B197DBFCA00354078 /* SingletonB.swift */; }; 20 | A0E63768197DC00E00354078 /* SingletonA.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0E6375C197DBFCA00354078 /* SingletonA.swift */; }; 21 | /* End PBXBuildFile section */ 22 | 23 | /* Begin PBXContainerItemProxy section */ 24 | A0E6374B197DBF9C00354078 /* PBXContainerItemProxy */ = { 25 | isa = PBXContainerItemProxy; 26 | containerPortal = A0E63735197DBF9C00354078 /* Project object */; 27 | proxyType = 1; 28 | remoteGlobalIDString = A0E6373D197DBF9C00354078; 29 | remoteInfo = Singleton; 30 | }; 31 | /* End PBXContainerItemProxy section */ 32 | 33 | /* Begin PBXFileReference section */ 34 | A0E6373E197DBF9C00354078 /* Singleton.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Singleton.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 35 | A0E63742197DBF9C00354078 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 36 | A0E63743197DBF9C00354078 /* Singleton.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Singleton.h; sourceTree = ""; }; 37 | A0E63749197DBF9C00354078 /* SingletonTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SingletonTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 38 | A0E6374F197DBF9C00354078 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 39 | A0E6375A197DBFCA00354078 /* SingletonC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SingletonC.swift; sourceTree = ""; }; 40 | A0E6375B197DBFCA00354078 /* SingletonB.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SingletonB.swift; sourceTree = ""; }; 41 | A0E6375C197DBFCA00354078 /* SingletonA.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SingletonA.swift; sourceTree = ""; }; 42 | A0E63760197DBFD700354078 /* SingletonCTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SingletonCTests.swift; sourceTree = ""; }; 43 | A0E63761197DBFD700354078 /* SingletonBTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SingletonBTests.swift; sourceTree = ""; }; 44 | A0E63762197DBFD700354078 /* SingletonATests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SingletonATests.swift; sourceTree = ""; }; 45 | /* End PBXFileReference section */ 46 | 47 | /* Begin PBXFrameworksBuildPhase section */ 48 | A0E6373A197DBF9C00354078 /* Frameworks */ = { 49 | isa = PBXFrameworksBuildPhase; 50 | buildActionMask = 2147483647; 51 | files = ( 52 | ); 53 | runOnlyForDeploymentPostprocessing = 0; 54 | }; 55 | A0E63746197DBF9C00354078 /* Frameworks */ = { 56 | isa = PBXFrameworksBuildPhase; 57 | buildActionMask = 2147483647; 58 | files = ( 59 | A0E6374A197DBF9C00354078 /* Singleton.framework in Frameworks */, 60 | ); 61 | runOnlyForDeploymentPostprocessing = 0; 62 | }; 63 | /* End PBXFrameworksBuildPhase section */ 64 | 65 | /* Begin PBXGroup section */ 66 | A0E63734197DBF9C00354078 = { 67 | isa = PBXGroup; 68 | children = ( 69 | A0E63740197DBF9C00354078 /* Singleton */, 70 | A0E6374D197DBF9C00354078 /* SingletonTests */, 71 | A0E6373F197DBF9C00354078 /* Products */, 72 | ); 73 | sourceTree = ""; 74 | }; 75 | A0E6373F197DBF9C00354078 /* Products */ = { 76 | isa = PBXGroup; 77 | children = ( 78 | A0E6373E197DBF9C00354078 /* Singleton.framework */, 79 | A0E63749197DBF9C00354078 /* SingletonTests.xctest */, 80 | ); 81 | name = Products; 82 | sourceTree = ""; 83 | }; 84 | A0E63740197DBF9C00354078 /* Singleton */ = { 85 | isa = PBXGroup; 86 | children = ( 87 | A0E6375A197DBFCA00354078 /* SingletonC.swift */, 88 | A0E6375B197DBFCA00354078 /* SingletonB.swift */, 89 | A0E6375C197DBFCA00354078 /* SingletonA.swift */, 90 | A0E63743197DBF9C00354078 /* Singleton.h */, 91 | A0E63741197DBF9C00354078 /* Supporting Files */, 92 | ); 93 | path = Singleton; 94 | sourceTree = ""; 95 | }; 96 | A0E63741197DBF9C00354078 /* Supporting Files */ = { 97 | isa = PBXGroup; 98 | children = ( 99 | A0E63742197DBF9C00354078 /* Info.plist */, 100 | ); 101 | name = "Supporting Files"; 102 | sourceTree = ""; 103 | }; 104 | A0E6374D197DBF9C00354078 /* SingletonTests */ = { 105 | isa = PBXGroup; 106 | children = ( 107 | A0E63760197DBFD700354078 /* SingletonCTests.swift */, 108 | A0E63761197DBFD700354078 /* SingletonBTests.swift */, 109 | A0E63762197DBFD700354078 /* SingletonATests.swift */, 110 | A0E6374E197DBF9C00354078 /* Supporting Files */, 111 | ); 112 | path = SingletonTests; 113 | sourceTree = ""; 114 | }; 115 | A0E6374E197DBF9C00354078 /* Supporting Files */ = { 116 | isa = PBXGroup; 117 | children = ( 118 | A0E6374F197DBF9C00354078 /* Info.plist */, 119 | ); 120 | name = "Supporting Files"; 121 | sourceTree = ""; 122 | }; 123 | /* End PBXGroup section */ 124 | 125 | /* Begin PBXHeadersBuildPhase section */ 126 | A0E6373B197DBF9C00354078 /* Headers */ = { 127 | isa = PBXHeadersBuildPhase; 128 | buildActionMask = 2147483647; 129 | files = ( 130 | A0E63744197DBF9C00354078 /* Singleton.h in Headers */, 131 | ); 132 | runOnlyForDeploymentPostprocessing = 0; 133 | }; 134 | /* End PBXHeadersBuildPhase section */ 135 | 136 | /* Begin PBXNativeTarget section */ 137 | A0E6373D197DBF9C00354078 /* Singleton */ = { 138 | isa = PBXNativeTarget; 139 | buildConfigurationList = A0E63754197DBF9C00354078 /* Build configuration list for PBXNativeTarget "Singleton" */; 140 | buildPhases = ( 141 | A0E63739197DBF9C00354078 /* Sources */, 142 | A0E6373A197DBF9C00354078 /* Frameworks */, 143 | A0E6373B197DBF9C00354078 /* Headers */, 144 | A0E6373C197DBF9C00354078 /* Resources */, 145 | ); 146 | buildRules = ( 147 | ); 148 | dependencies = ( 149 | ); 150 | name = Singleton; 151 | productName = Singleton; 152 | productReference = A0E6373E197DBF9C00354078 /* Singleton.framework */; 153 | productType = "com.apple.product-type.framework"; 154 | }; 155 | A0E63748197DBF9C00354078 /* SingletonTests */ = { 156 | isa = PBXNativeTarget; 157 | buildConfigurationList = A0E63757197DBF9C00354078 /* Build configuration list for PBXNativeTarget "SingletonTests" */; 158 | buildPhases = ( 159 | A0E63745197DBF9C00354078 /* Sources */, 160 | A0E63746197DBF9C00354078 /* Frameworks */, 161 | A0E63747197DBF9C00354078 /* Resources */, 162 | ); 163 | buildRules = ( 164 | ); 165 | dependencies = ( 166 | A0E6374C197DBF9C00354078 /* PBXTargetDependency */, 167 | ); 168 | name = SingletonTests; 169 | productName = SingletonTests; 170 | productReference = A0E63749197DBF9C00354078 /* SingletonTests.xctest */; 171 | productType = "com.apple.product-type.bundle.unit-test"; 172 | }; 173 | /* End PBXNativeTarget section */ 174 | 175 | /* Begin PBXProject section */ 176 | A0E63735197DBF9C00354078 /* Project object */ = { 177 | isa = PBXProject; 178 | attributes = { 179 | LastUpgradeCheck = 0600; 180 | ORGANIZATIONNAME = "Hermes Pique"; 181 | TargetAttributes = { 182 | A0E6373D197DBF9C00354078 = { 183 | CreatedOnToolsVersion = 6.0; 184 | }; 185 | A0E63748197DBF9C00354078 = { 186 | CreatedOnToolsVersion = 6.0; 187 | TestTargetID = A0E6373D197DBF9C00354078; 188 | }; 189 | }; 190 | }; 191 | buildConfigurationList = A0E63738197DBF9C00354078 /* Build configuration list for PBXProject "Singleton" */; 192 | compatibilityVersion = "Xcode 3.2"; 193 | developmentRegion = English; 194 | hasScannedForEncodings = 0; 195 | knownRegions = ( 196 | en, 197 | ); 198 | mainGroup = A0E63734197DBF9C00354078; 199 | productRefGroup = A0E6373F197DBF9C00354078 /* Products */; 200 | projectDirPath = ""; 201 | projectRoot = ""; 202 | targets = ( 203 | A0E6373D197DBF9C00354078 /* Singleton */, 204 | A0E63748197DBF9C00354078 /* SingletonTests */, 205 | ); 206 | }; 207 | /* End PBXProject section */ 208 | 209 | /* Begin PBXResourcesBuildPhase section */ 210 | A0E6373C197DBF9C00354078 /* Resources */ = { 211 | isa = PBXResourcesBuildPhase; 212 | buildActionMask = 2147483647; 213 | files = ( 214 | ); 215 | runOnlyForDeploymentPostprocessing = 0; 216 | }; 217 | A0E63747197DBF9C00354078 /* Resources */ = { 218 | isa = PBXResourcesBuildPhase; 219 | buildActionMask = 2147483647; 220 | files = ( 221 | ); 222 | runOnlyForDeploymentPostprocessing = 0; 223 | }; 224 | /* End PBXResourcesBuildPhase section */ 225 | 226 | /* Begin PBXSourcesBuildPhase section */ 227 | A0E63739197DBF9C00354078 /* Sources */ = { 228 | isa = PBXSourcesBuildPhase; 229 | buildActionMask = 2147483647; 230 | files = ( 231 | A0E6375E197DBFCA00354078 /* SingletonB.swift in Sources */, 232 | A0E6375F197DBFCA00354078 /* SingletonA.swift in Sources */, 233 | A0E6375D197DBFCA00354078 /* SingletonC.swift in Sources */, 234 | ); 235 | runOnlyForDeploymentPostprocessing = 0; 236 | }; 237 | A0E63745197DBF9C00354078 /* Sources */ = { 238 | isa = PBXSourcesBuildPhase; 239 | buildActionMask = 2147483647; 240 | files = ( 241 | A0E63764197DBFD700354078 /* SingletonBTests.swift in Sources */, 242 | A0E63768197DC00E00354078 /* SingletonA.swift in Sources */, 243 | A0E63767197DC00E00354078 /* SingletonB.swift in Sources */, 244 | A0E63766197DC00E00354078 /* SingletonC.swift in Sources */, 245 | A0E63763197DBFD700354078 /* SingletonCTests.swift in Sources */, 246 | A0E63765197DBFD700354078 /* SingletonATests.swift in Sources */, 247 | ); 248 | runOnlyForDeploymentPostprocessing = 0; 249 | }; 250 | /* End PBXSourcesBuildPhase section */ 251 | 252 | /* Begin PBXTargetDependency section */ 253 | A0E6374C197DBF9C00354078 /* PBXTargetDependency */ = { 254 | isa = PBXTargetDependency; 255 | target = A0E6373D197DBF9C00354078 /* Singleton */; 256 | targetProxy = A0E6374B197DBF9C00354078 /* PBXContainerItemProxy */; 257 | }; 258 | /* End PBXTargetDependency section */ 259 | 260 | /* Begin XCBuildConfiguration section */ 261 | A0E63752197DBF9C00354078 /* Debug */ = { 262 | isa = XCBuildConfiguration; 263 | buildSettings = { 264 | ALWAYS_SEARCH_USER_PATHS = NO; 265 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 266 | CLANG_CXX_LIBRARY = "libc++"; 267 | CLANG_ENABLE_MODULES = YES; 268 | CLANG_ENABLE_OBJC_ARC = YES; 269 | CLANG_WARN_BOOL_CONVERSION = YES; 270 | CLANG_WARN_CONSTANT_CONVERSION = YES; 271 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 272 | CLANG_WARN_EMPTY_BODY = YES; 273 | CLANG_WARN_ENUM_CONVERSION = YES; 274 | CLANG_WARN_INT_CONVERSION = YES; 275 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 276 | CLANG_WARN_UNREACHABLE_CODE = YES; 277 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 278 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 279 | COPY_PHASE_STRIP = NO; 280 | CURRENT_PROJECT_VERSION = 1; 281 | ENABLE_STRICT_OBJC_MSGSEND = YES; 282 | GCC_C_LANGUAGE_STANDARD = gnu99; 283 | GCC_DYNAMIC_NO_PIC = NO; 284 | GCC_OPTIMIZATION_LEVEL = 0; 285 | GCC_PREPROCESSOR_DEFINITIONS = ( 286 | "DEBUG=1", 287 | "$(inherited)", 288 | ); 289 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 290 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 291 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 292 | GCC_WARN_UNDECLARED_SELECTOR = YES; 293 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 294 | GCC_WARN_UNUSED_FUNCTION = YES; 295 | GCC_WARN_UNUSED_VARIABLE = YES; 296 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 297 | MTL_ENABLE_DEBUG_INFO = YES; 298 | ONLY_ACTIVE_ARCH = YES; 299 | SDKROOT = iphoneos; 300 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 301 | TARGETED_DEVICE_FAMILY = "1,2"; 302 | VERSIONING_SYSTEM = "apple-generic"; 303 | VERSION_INFO_PREFIX = ""; 304 | }; 305 | name = Debug; 306 | }; 307 | A0E63753197DBF9C00354078 /* Release */ = { 308 | isa = XCBuildConfiguration; 309 | buildSettings = { 310 | ALWAYS_SEARCH_USER_PATHS = NO; 311 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 312 | CLANG_CXX_LIBRARY = "libc++"; 313 | CLANG_ENABLE_MODULES = YES; 314 | CLANG_ENABLE_OBJC_ARC = YES; 315 | CLANG_WARN_BOOL_CONVERSION = YES; 316 | CLANG_WARN_CONSTANT_CONVERSION = YES; 317 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 318 | CLANG_WARN_EMPTY_BODY = YES; 319 | CLANG_WARN_ENUM_CONVERSION = YES; 320 | CLANG_WARN_INT_CONVERSION = YES; 321 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 322 | CLANG_WARN_UNREACHABLE_CODE = YES; 323 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 324 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 325 | COPY_PHASE_STRIP = YES; 326 | CURRENT_PROJECT_VERSION = 1; 327 | ENABLE_NS_ASSERTIONS = NO; 328 | ENABLE_STRICT_OBJC_MSGSEND = YES; 329 | GCC_C_LANGUAGE_STANDARD = gnu99; 330 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 331 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 332 | GCC_WARN_UNDECLARED_SELECTOR = YES; 333 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 334 | GCC_WARN_UNUSED_FUNCTION = YES; 335 | GCC_WARN_UNUSED_VARIABLE = YES; 336 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 337 | MTL_ENABLE_DEBUG_INFO = NO; 338 | SDKROOT = iphoneos; 339 | TARGETED_DEVICE_FAMILY = "1,2"; 340 | VALIDATE_PRODUCT = YES; 341 | VERSIONING_SYSTEM = "apple-generic"; 342 | VERSION_INFO_PREFIX = ""; 343 | }; 344 | name = Release; 345 | }; 346 | A0E63755197DBF9C00354078 /* Debug */ = { 347 | isa = XCBuildConfiguration; 348 | buildSettings = { 349 | CLANG_ENABLE_MODULES = YES; 350 | DEFINES_MODULE = YES; 351 | DYLIB_COMPATIBILITY_VERSION = 1; 352 | DYLIB_CURRENT_VERSION = 1; 353 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 354 | INFOPLIST_FILE = Singleton/Info.plist; 355 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 356 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 357 | PRODUCT_NAME = "$(TARGET_NAME)"; 358 | SKIP_INSTALL = YES; 359 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 360 | }; 361 | name = Debug; 362 | }; 363 | A0E63756197DBF9C00354078 /* Release */ = { 364 | isa = XCBuildConfiguration; 365 | buildSettings = { 366 | CLANG_ENABLE_MODULES = YES; 367 | DEFINES_MODULE = YES; 368 | DYLIB_COMPATIBILITY_VERSION = 1; 369 | DYLIB_CURRENT_VERSION = 1; 370 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 371 | INFOPLIST_FILE = Singleton/Info.plist; 372 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 373 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 374 | PRODUCT_NAME = "$(TARGET_NAME)"; 375 | SKIP_INSTALL = YES; 376 | }; 377 | name = Release; 378 | }; 379 | A0E63758197DBF9C00354078 /* Debug */ = { 380 | isa = XCBuildConfiguration; 381 | buildSettings = { 382 | FRAMEWORK_SEARCH_PATHS = ( 383 | "$(SDKROOT)/Developer/Library/Frameworks", 384 | "$(inherited)", 385 | ); 386 | GCC_PREPROCESSOR_DEFINITIONS = ( 387 | "DEBUG=1", 388 | "$(inherited)", 389 | ); 390 | INFOPLIST_FILE = SingletonTests/Info.plist; 391 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 392 | PRODUCT_NAME = "$(TARGET_NAME)"; 393 | }; 394 | name = Debug; 395 | }; 396 | A0E63759197DBF9C00354078 /* Release */ = { 397 | isa = XCBuildConfiguration; 398 | buildSettings = { 399 | FRAMEWORK_SEARCH_PATHS = ( 400 | "$(SDKROOT)/Developer/Library/Frameworks", 401 | "$(inherited)", 402 | ); 403 | INFOPLIST_FILE = SingletonTests/Info.plist; 404 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 405 | PRODUCT_NAME = "$(TARGET_NAME)"; 406 | }; 407 | name = Release; 408 | }; 409 | /* End XCBuildConfiguration section */ 410 | 411 | /* Begin XCConfigurationList section */ 412 | A0E63738197DBF9C00354078 /* Build configuration list for PBXProject "Singleton" */ = { 413 | isa = XCConfigurationList; 414 | buildConfigurations = ( 415 | A0E63752197DBF9C00354078 /* Debug */, 416 | A0E63753197DBF9C00354078 /* Release */, 417 | ); 418 | defaultConfigurationIsVisible = 0; 419 | defaultConfigurationName = Release; 420 | }; 421 | A0E63754197DBF9C00354078 /* Build configuration list for PBXNativeTarget "Singleton" */ = { 422 | isa = XCConfigurationList; 423 | buildConfigurations = ( 424 | A0E63755197DBF9C00354078 /* Debug */, 425 | A0E63756197DBF9C00354078 /* Release */, 426 | ); 427 | defaultConfigurationIsVisible = 0; 428 | defaultConfigurationName = Release; 429 | }; 430 | A0E63757197DBF9C00354078 /* Build configuration list for PBXNativeTarget "SingletonTests" */ = { 431 | isa = XCConfigurationList; 432 | buildConfigurations = ( 433 | A0E63758197DBF9C00354078 /* Debug */, 434 | A0E63759197DBF9C00354078 /* Release */, 435 | ); 436 | defaultConfigurationIsVisible = 0; 437 | defaultConfigurationName = Release; 438 | }; 439 | /* End XCConfigurationList section */ 440 | }; 441 | rootObject = A0E63735197DBF9C00354078 /* Project object */; 442 | } 443 | -------------------------------------------------------------------------------- /Singleton.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Singleton.xcodeproj/project.xcworkspace/xcuserdata/hermespique.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpique/SwiftSingleton/7a1d314546d69003983f8ae84c32d95d1100cbcc/Singleton.xcodeproj/project.xcworkspace/xcuserdata/hermespique.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Singleton.xcodeproj/xcuserdata/hermespique.xcuserdatad/xcschemes/Singleton.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 42 | 43 | 49 | 50 | 51 | 52 | 61 | 62 | 68 | 69 | 70 | 71 | 72 | 73 | 79 | 80 | 86 | 87 | 88 | 89 | 91 | 92 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /Singleton.xcodeproj/xcuserdata/hermespique.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | Singleton.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | A0E6373D197DBF9C00354078 16 | 17 | primary 18 | 19 | 20 | A0E63748197DBF9C00354078 21 | 22 | primary 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Singleton/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | com.hpique.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | ${CURRENT_PROJECT_VERSION} 23 | NSPrincipalClass 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Singleton/Singleton.h: -------------------------------------------------------------------------------- 1 | // 2 | // Singleton.h 3 | // Singleton 4 | // 5 | // Created by Hermes Pique on 7/21/14. 6 | // Copyright (c) 2014 Hermes Pique. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for Singleton. 12 | FOUNDATION_EXPORT double SingletonVersionNumber; 13 | 14 | //! Project version string for Singleton. 15 | FOUNDATION_EXPORT const unsigned char SingletonVersionString[]; 16 | 17 | // In this header, you should import all the public headers of your framework using statements like #import 18 | 19 | 20 | -------------------------------------------------------------------------------- /Singleton/SingletonA.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SingletonA.swift 3 | // Singleton 4 | // 5 | // Created by Hermes Pique on 6/9/14. 6 | // Copyright (c) 2014 Hermes Pique. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class SingletonA { 12 | 13 | static let sharedInstance: SingletonA = SingletonA() 14 | 15 | private init() { 16 | print("AAA"); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /Singleton/SingletonB.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SingletonB.swift 3 | // Singleton 4 | // 5 | // Created by Hermes Pique on 6/9/14. 6 | // Copyright (c) 2014 Hermes Pique. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class SingletonB { 12 | 13 | class var sharedInstance: SingletonB { 14 | struct Static { 15 | static let instance: SingletonB = SingletonB() 16 | } 17 | return Static.instance 18 | } 19 | 20 | private init() { 21 | print("BBB"); 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /Singleton/SingletonC.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SingletonC.swift 3 | // Singleton 4 | // 5 | // Created by Hermes Pique on 6/9/14. 6 | // Copyright (c) 2014 Hermes Pique. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class SingletonC { 12 | 13 | class var sharedInstance: SingletonC { 14 | struct Static { 15 | static var onceToken: dispatch_once_t = 0 16 | static var instance: SingletonC? = nil 17 | } 18 | dispatch_once(&Static.onceToken) { 19 | Static.instance = SingletonC() 20 | } 21 | return Static.instance! 22 | } 23 | 24 | private init() { 25 | print("CCC"); 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /SingletonTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | com.hpique.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /SingletonTests/SingletonATests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SingletonATests.swift 3 | // Singleton 4 | // 5 | // Created by Hermes Pique on 6/9/14. 6 | // Copyright (c) 2014 Hermes Pique. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | import Singleton 11 | 12 | class SingletonATests: XCTestCase { 13 | 14 | func testSharedInstance() { 15 | let instance = SingletonA.sharedInstance 16 | XCTAssertNotNil(instance, "") 17 | } 18 | 19 | func testSharedInstance_Unique() { 20 | let instance1 = SingletonA() 21 | let instance2 = SingletonA.sharedInstance 22 | XCTAssertFalse(instance1 === instance2) 23 | } 24 | 25 | func testSharedInstance_Twice() { 26 | let instance1 = SingletonA.sharedInstance 27 | let instance2 = SingletonA.sharedInstance 28 | XCTAssertTrue(instance1 === instance2) 29 | } 30 | 31 | func testSharedInstance_ThreadSafety() { 32 | var instance1 : SingletonA! 33 | var instance2 : SingletonA! 34 | 35 | let expectation1 = expectationWithDescription("Instance 1") 36 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { 37 | instance1 = SingletonA.sharedInstance 38 | expectation1.fulfill() 39 | } 40 | let expectation2 = expectationWithDescription("Instance 2") 41 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { 42 | instance2 = SingletonA.sharedInstance 43 | expectation2.fulfill() 44 | } 45 | 46 | waitForExpectationsWithTimeout(1.0) { (_) in 47 | XCTAssertTrue(instance1 === instance2) 48 | } 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /SingletonTests/SingletonBTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SingletonBTests.swift 3 | // Singleton 4 | // 5 | // Created by Hermes Pique on 6/9/14. 6 | // Copyright (c) 2014 Hermes Pique. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | import Singleton 11 | 12 | class SingletonBTests: XCTestCase { 13 | 14 | func testSharedInstance() { 15 | let instance = SingletonB.sharedInstance 16 | XCTAssertNotNil(instance, "") 17 | } 18 | 19 | func testSharedInstance_Unique() { 20 | let instance1 = SingletonB() 21 | let instance2 = SingletonB.sharedInstance 22 | XCTAssertFalse(instance1 === instance2) 23 | } 24 | 25 | func testSharedInstance_Twice() { 26 | let instance1 = SingletonB.sharedInstance 27 | let instance2 = SingletonB.sharedInstance 28 | XCTAssertTrue(instance1 === instance2) 29 | } 30 | 31 | func testSharedInstance_ThreadSafety() { 32 | var instance1 : SingletonB! 33 | var instance2 : SingletonB! 34 | 35 | let expectation1 = expectationWithDescription("Instance 1") 36 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { 37 | instance1 = SingletonB.sharedInstance 38 | expectation1.fulfill() 39 | } 40 | let expectation2 = expectationWithDescription("Instance 2") 41 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { 42 | instance2 = SingletonB.sharedInstance 43 | expectation2.fulfill() 44 | } 45 | 46 | waitForExpectationsWithTimeout(1.0) { (_) in 47 | XCTAssertTrue(instance1 === instance2) 48 | } 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /SingletonTests/SingletonCTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SingletonCTests.swift 3 | // Singleton 4 | // 5 | // Created by Hermes Pique on 6/9/14. 6 | // Copyright (c) 2014 Hermes Pique. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | import Singleton 11 | 12 | class SingletonCTests: XCTestCase { 13 | 14 | func testSharedInstance() { 15 | let instance = SingletonC.sharedInstance 16 | XCTAssertNotNil(instance, "") 17 | } 18 | 19 | func testSharedInstance_Unique() { 20 | let instance1 = SingletonC() 21 | let instance2 = SingletonC.sharedInstance 22 | XCTAssertFalse(instance1 === instance2) 23 | } 24 | 25 | func testSharedInstance_Twice() { 26 | let instance1 = SingletonC.sharedInstance 27 | let instance2 = SingletonC.sharedInstance 28 | XCTAssertTrue(instance1 === instance2) 29 | } 30 | 31 | func testSharedInstance_ThreadSafety() { 32 | var instance1 : SingletonC! 33 | var instance2 : SingletonC! 34 | 35 | let expectation1 = expectationWithDescription("Instance 1") 36 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { 37 | instance1 = SingletonC.sharedInstance 38 | expectation1.fulfill() 39 | } 40 | let expectation2 = expectationWithDescription("Instance 2") 41 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { 42 | instance2 = SingletonC.sharedInstance 43 | expectation2.fulfill() 44 | } 45 | 46 | waitForExpectationsWithTimeout(1.0) { (_) in 47 | XCTAssertTrue(instance1 === instance2) 48 | } 49 | } 50 | 51 | } --------------------------------------------------------------------------------