├── .gitignore ├── LICENSE ├── README.md ├── SwiftFactory.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ ├── jas.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── joshuasmith.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ ├── jas.xcuserdatad │ └── xcschemes │ │ ├── SwiftFactory.xcscheme │ │ └── xcschememanagement.plist │ └── joshuasmith.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── SwiftFactory.xcscheme │ └── xcschememanagement.plist └── SwiftFactory ├── Data Model.swift ├── OBJCObjectFactory.h ├── OBJCObjectFactory.m ├── ObjectFactory.swift ├── SwiftFactory-Bridging-Header.h └── main.swift /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | build/ 3 | *.pbxuser 4 | !default.pbxuser 5 | *.mode1v3 6 | !default.mode1v3 7 | *.mode2v3 8 | !default.mode2v3 9 | *.perspectivev3 10 | !default.perspectivev3 11 | *.xcworkspace 12 | !default.xcworkspace 13 | xcuserdata 14 | profile 15 | *.moved-aside 16 | DerivedData 17 | .idea/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Josh Smith 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | swift-factory 2 | ============= 3 | 4 | This simple repository shows how to instantiate classes by class name in Swift. 5 | It uses Objective-C interop to perform the actual object creation. 6 | A generic utility Swift class named ObjectFactory provides a convenience API. 7 | 8 | This code was tested against the first beta of iOS 8. 9 | The sample project is an OS X command line for the sake of simplicity. 10 | 11 | Refer to main.swift to see how to use ObjectFactory. 12 | 13 | For more information about the project refer to this blog post: 14 | http://ijoshsmith.com/2014/06/05/instantiating-classes-by-name-in-swift/ 15 | -------------------------------------------------------------------------------- /SwiftFactory.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | D0D97E8E194021DC005C4C62 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D97E8D194021DC005C4C62 /* main.swift */; }; 11 | D0D97E9F1940224D005C4C62 /* Data Model.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D97E9E1940224D005C4C62 /* Data Model.swift */; }; 12 | D0D97EA119402284005C4C62 /* ObjectFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D97EA019402284005C4C62 /* ObjectFactory.swift */; }; 13 | D0D97EA719402319005C4C62 /* OBJCObjectFactory.m in Sources */ = {isa = PBXBuildFile; fileRef = D0D97EA619402319005C4C62 /* OBJCObjectFactory.m */; }; 14 | /* End PBXBuildFile section */ 15 | 16 | /* Begin PBXCopyFilesBuildPhase section */ 17 | D0D97E88194021DC005C4C62 /* CopyFiles */ = { 18 | isa = PBXCopyFilesBuildPhase; 19 | buildActionMask = 2147483647; 20 | dstPath = /usr/share/man/man1/; 21 | dstSubfolderSpec = 0; 22 | files = ( 23 | ); 24 | runOnlyForDeploymentPostprocessing = 1; 25 | }; 26 | /* End PBXCopyFilesBuildPhase section */ 27 | 28 | /* Begin PBXFileReference section */ 29 | D0D97E8A194021DC005C4C62 /* SwiftFactory */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = SwiftFactory; sourceTree = BUILT_PRODUCTS_DIR; }; 30 | D0D97E8D194021DC005C4C62 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 31 | D0D97E9E1940224D005C4C62 /* Data Model.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Data Model.swift"; sourceTree = ""; }; 32 | D0D97EA019402284005C4C62 /* ObjectFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjectFactory.swift; sourceTree = ""; }; 33 | D0D97EA419402319005C4C62 /* SwiftFactory-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SwiftFactory-Bridging-Header.h"; sourceTree = ""; }; 34 | D0D97EA519402319005C4C62 /* OBJCObjectFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OBJCObjectFactory.h; sourceTree = ""; }; 35 | D0D97EA619402319005C4C62 /* OBJCObjectFactory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OBJCObjectFactory.m; sourceTree = ""; }; 36 | /* End PBXFileReference section */ 37 | 38 | /* Begin PBXFrameworksBuildPhase section */ 39 | D0D97E87194021DC005C4C62 /* Frameworks */ = { 40 | isa = PBXFrameworksBuildPhase; 41 | buildActionMask = 2147483647; 42 | files = ( 43 | ); 44 | runOnlyForDeploymentPostprocessing = 0; 45 | }; 46 | /* End PBXFrameworksBuildPhase section */ 47 | 48 | /* Begin PBXGroup section */ 49 | D0D97E81194021DC005C4C62 = { 50 | isa = PBXGroup; 51 | children = ( 52 | D0D97E8C194021DC005C4C62 /* SwiftFactory */, 53 | D0D97E8B194021DC005C4C62 /* Products */, 54 | ); 55 | sourceTree = ""; 56 | }; 57 | D0D97E8B194021DC005C4C62 /* Products */ = { 58 | isa = PBXGroup; 59 | children = ( 60 | D0D97E8A194021DC005C4C62 /* SwiftFactory */, 61 | ); 62 | name = Products; 63 | sourceTree = ""; 64 | }; 65 | D0D97E8C194021DC005C4C62 /* SwiftFactory */ = { 66 | isa = PBXGroup; 67 | children = ( 68 | D0D97E8D194021DC005C4C62 /* main.swift */, 69 | D0D97E9E1940224D005C4C62 /* Data Model.swift */, 70 | D0D97EA519402319005C4C62 /* OBJCObjectFactory.h */, 71 | D0D97EA619402319005C4C62 /* OBJCObjectFactory.m */, 72 | D0D97EA019402284005C4C62 /* ObjectFactory.swift */, 73 | D0D97EA419402319005C4C62 /* SwiftFactory-Bridging-Header.h */, 74 | ); 75 | path = SwiftFactory; 76 | sourceTree = ""; 77 | }; 78 | /* End PBXGroup section */ 79 | 80 | /* Begin PBXNativeTarget section */ 81 | D0D97E89194021DC005C4C62 /* SwiftFactory */ = { 82 | isa = PBXNativeTarget; 83 | buildConfigurationList = D0D97E91194021DC005C4C62 /* Build configuration list for PBXNativeTarget "SwiftFactory" */; 84 | buildPhases = ( 85 | D0D97E86194021DC005C4C62 /* Sources */, 86 | D0D97E87194021DC005C4C62 /* Frameworks */, 87 | D0D97E88194021DC005C4C62 /* CopyFiles */, 88 | ); 89 | buildRules = ( 90 | ); 91 | dependencies = ( 92 | ); 93 | name = SwiftFactory; 94 | productName = SwiftFactory; 95 | productReference = D0D97E8A194021DC005C4C62 /* SwiftFactory */; 96 | productType = "com.apple.product-type.tool"; 97 | }; 98 | /* End PBXNativeTarget section */ 99 | 100 | /* Begin PBXProject section */ 101 | D0D97E82194021DC005C4C62 /* Project object */ = { 102 | isa = PBXProject; 103 | attributes = { 104 | LastUpgradeCheck = 0600; 105 | ORGANIZATIONNAME = iJoshSmith; 106 | TargetAttributes = { 107 | D0D97E89194021DC005C4C62 = { 108 | CreatedOnToolsVersion = 6.0; 109 | }; 110 | }; 111 | }; 112 | buildConfigurationList = D0D97E85194021DC005C4C62 /* Build configuration list for PBXProject "SwiftFactory" */; 113 | compatibilityVersion = "Xcode 3.2"; 114 | developmentRegion = English; 115 | hasScannedForEncodings = 0; 116 | knownRegions = ( 117 | en, 118 | ); 119 | mainGroup = D0D97E81194021DC005C4C62; 120 | productRefGroup = D0D97E8B194021DC005C4C62 /* Products */; 121 | projectDirPath = ""; 122 | projectRoot = ""; 123 | targets = ( 124 | D0D97E89194021DC005C4C62 /* SwiftFactory */, 125 | ); 126 | }; 127 | /* End PBXProject section */ 128 | 129 | /* Begin PBXSourcesBuildPhase section */ 130 | D0D97E86194021DC005C4C62 /* Sources */ = { 131 | isa = PBXSourcesBuildPhase; 132 | buildActionMask = 2147483647; 133 | files = ( 134 | D0D97E8E194021DC005C4C62 /* main.swift in Sources */, 135 | D0D97EA119402284005C4C62 /* ObjectFactory.swift in Sources */, 136 | D0D97E9F1940224D005C4C62 /* Data Model.swift in Sources */, 137 | D0D97EA719402319005C4C62 /* OBJCObjectFactory.m in Sources */, 138 | ); 139 | runOnlyForDeploymentPostprocessing = 0; 140 | }; 141 | /* End PBXSourcesBuildPhase section */ 142 | 143 | /* Begin XCBuildConfiguration section */ 144 | D0D97E8F194021DC005C4C62 /* Debug */ = { 145 | isa = XCBuildConfiguration; 146 | buildSettings = { 147 | ALWAYS_SEARCH_USER_PATHS = NO; 148 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 149 | CLANG_CXX_LIBRARY = "libc++"; 150 | CLANG_ENABLE_MODULES = YES; 151 | CLANG_ENABLE_OBJC_ARC = YES; 152 | CLANG_WARN_BOOL_CONVERSION = YES; 153 | CLANG_WARN_CONSTANT_CONVERSION = YES; 154 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 155 | CLANG_WARN_EMPTY_BODY = YES; 156 | CLANG_WARN_ENUM_CONVERSION = YES; 157 | CLANG_WARN_INT_CONVERSION = YES; 158 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 159 | CLANG_WARN_UNREACHABLE_CODE = YES; 160 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 161 | COPY_PHASE_STRIP = NO; 162 | ENABLE_STRICT_OBJC_MSGSEND = YES; 163 | GCC_C_LANGUAGE_STANDARD = gnu99; 164 | GCC_DYNAMIC_NO_PIC = NO; 165 | GCC_OPTIMIZATION_LEVEL = 0; 166 | GCC_PREPROCESSOR_DEFINITIONS = ( 167 | "DEBUG=1", 168 | "$(inherited)", 169 | ); 170 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 171 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 172 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 173 | GCC_WARN_UNDECLARED_SELECTOR = YES; 174 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 175 | GCC_WARN_UNUSED_FUNCTION = YES; 176 | GCC_WARN_UNUSED_VARIABLE = YES; 177 | MACOSX_DEPLOYMENT_TARGET = 10.9; 178 | METAL_ENABLE_DEBUG_INFO = YES; 179 | ONLY_ACTIVE_ARCH = YES; 180 | SDKROOT = macosx; 181 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 182 | }; 183 | name = Debug; 184 | }; 185 | D0D97E90194021DC005C4C62 /* Release */ = { 186 | isa = XCBuildConfiguration; 187 | buildSettings = { 188 | ALWAYS_SEARCH_USER_PATHS = NO; 189 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 190 | CLANG_CXX_LIBRARY = "libc++"; 191 | CLANG_ENABLE_MODULES = YES; 192 | CLANG_ENABLE_OBJC_ARC = YES; 193 | CLANG_WARN_BOOL_CONVERSION = YES; 194 | CLANG_WARN_CONSTANT_CONVERSION = YES; 195 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 196 | CLANG_WARN_EMPTY_BODY = YES; 197 | CLANG_WARN_ENUM_CONVERSION = YES; 198 | CLANG_WARN_INT_CONVERSION = YES; 199 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 200 | CLANG_WARN_UNREACHABLE_CODE = YES; 201 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 202 | COPY_PHASE_STRIP = YES; 203 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 204 | ENABLE_NS_ASSERTIONS = NO; 205 | ENABLE_STRICT_OBJC_MSGSEND = YES; 206 | GCC_C_LANGUAGE_STANDARD = gnu99; 207 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 208 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 209 | GCC_WARN_UNDECLARED_SELECTOR = YES; 210 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 211 | GCC_WARN_UNUSED_FUNCTION = YES; 212 | GCC_WARN_UNUSED_VARIABLE = YES; 213 | MACOSX_DEPLOYMENT_TARGET = 10.9; 214 | METAL_ENABLE_DEBUG_INFO = NO; 215 | SDKROOT = macosx; 216 | }; 217 | name = Release; 218 | }; 219 | D0D97E92194021DC005C4C62 /* Debug */ = { 220 | isa = XCBuildConfiguration; 221 | buildSettings = { 222 | CLANG_ENABLE_MODULES = YES; 223 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; 224 | PRODUCT_NAME = "$(TARGET_NAME)"; 225 | SWIFT_OBJC_BRIDGING_HEADER = "SwiftFactory/SwiftFactory-Bridging-Header.h"; 226 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 227 | }; 228 | name = Debug; 229 | }; 230 | D0D97E93194021DC005C4C62 /* Release */ = { 231 | isa = XCBuildConfiguration; 232 | buildSettings = { 233 | CLANG_ENABLE_MODULES = YES; 234 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; 235 | PRODUCT_NAME = "$(TARGET_NAME)"; 236 | SWIFT_OBJC_BRIDGING_HEADER = "SwiftFactory/SwiftFactory-Bridging-Header.h"; 237 | }; 238 | name = Release; 239 | }; 240 | /* End XCBuildConfiguration section */ 241 | 242 | /* Begin XCConfigurationList section */ 243 | D0D97E85194021DC005C4C62 /* Build configuration list for PBXProject "SwiftFactory" */ = { 244 | isa = XCConfigurationList; 245 | buildConfigurations = ( 246 | D0D97E8F194021DC005C4C62 /* Debug */, 247 | D0D97E90194021DC005C4C62 /* Release */, 248 | ); 249 | defaultConfigurationIsVisible = 0; 250 | defaultConfigurationName = Release; 251 | }; 252 | D0D97E91194021DC005C4C62 /* Build configuration list for PBXNativeTarget "SwiftFactory" */ = { 253 | isa = XCConfigurationList; 254 | buildConfigurations = ( 255 | D0D97E92194021DC005C4C62 /* Debug */, 256 | D0D97E93194021DC005C4C62 /* Release */, 257 | ); 258 | defaultConfigurationIsVisible = 0; 259 | defaultConfigurationName = Release; 260 | }; 261 | /* End XCConfigurationList section */ 262 | }; 263 | rootObject = D0D97E82194021DC005C4C62 /* Project object */; 264 | } 265 | -------------------------------------------------------------------------------- /SwiftFactory.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SwiftFactory.xcodeproj/project.xcworkspace/xcuserdata/jas.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ijoshsmith/swift-factory/74681412d423fdf75405446525cf8e5cd41d1450/SwiftFactory.xcodeproj/project.xcworkspace/xcuserdata/jas.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /SwiftFactory.xcodeproj/project.xcworkspace/xcuserdata/joshuasmith.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ijoshsmith/swift-factory/74681412d423fdf75405446525cf8e5cd41d1450/SwiftFactory.xcodeproj/project.xcworkspace/xcuserdata/joshuasmith.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /SwiftFactory.xcodeproj/xcuserdata/jas.xcuserdatad/xcschemes/SwiftFactory.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 51 | 52 | 58 | 59 | 60 | 61 | 62 | 63 | 69 | 70 | 76 | 77 | 78 | 79 | 81 | 82 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /SwiftFactory.xcodeproj/xcuserdata/jas.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | SwiftFactory.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | D0D97E89194021DC005C4C62 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /SwiftFactory.xcodeproj/xcuserdata/joshuasmith.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /SwiftFactory.xcodeproj/xcuserdata/joshuasmith.xcuserdatad/xcschemes/SwiftFactory.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 51 | 52 | 58 | 59 | 60 | 61 | 62 | 63 | 69 | 70 | 76 | 77 | 78 | 79 | 81 | 82 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /SwiftFactory.xcodeproj/xcuserdata/joshuasmith.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | SwiftFactory.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | D0D97E89194021DC005C4C62 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /SwiftFactory/Data Model.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Class Hierarchy.swift 3 | // SwiftFactory 4 | // 5 | // Created by Joshua Smith on 6/4/14. 6 | // Copyright (c) 2014 iJoshSmith. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | // Subclass NSObject to enable factory creation. 12 | class Person : NSObject 13 | { 14 | override init() { self.name = "?" } 15 | init(name: String) { self.name = name } 16 | 17 | var name: String 18 | } 19 | 20 | class Friend : Person 21 | { 22 | var nickname : String? 23 | } 24 | 25 | class Stranger : Person 26 | { 27 | // Ooh, how misanthropic! 28 | var seemsFriendly = false 29 | } 30 | -------------------------------------------------------------------------------- /SwiftFactory/OBJCObjectFactory.h: -------------------------------------------------------------------------------- 1 | // 2 | // OBJCObjectFactory.h 3 | // SwiftFactory 4 | // 5 | // Created by Joshua Smith on 6/4/14. 6 | // Copyright (c) 2014 iJoshSmith. All rights reserved. 7 | // 8 | 9 | /* 10 | Be sure to import this file in your project's bridging header file. 11 | #import "OBJCObjectFactory.h" 12 | */ 13 | 14 | @import Foundation; 15 | 16 | /** Instantiates NSObject subclasses. */ 17 | @interface OBJCObjectFactory : NSObject 18 | 19 | /** 20 | Instantiates the specified class, which must 21 | descend (dircectly or indirectly) from NSObject. 22 | Uses the class's parameterless initializer. 23 | */ 24 | + (id)create:(NSString *)className; 25 | 26 | /** 27 | Instantiates the specified class, which must 28 | descend (dircectly or indirectly) from NSObject. 29 | Uses the specified initializer, passing it the 30 | argument provided via the `argument` parameter. 31 | */ 32 | + (id)create:(NSString *)className 33 | initializer:(SEL)initializer 34 | argument:(id)argument; 35 | 36 | @end 37 | -------------------------------------------------------------------------------- /SwiftFactory/OBJCObjectFactory.m: -------------------------------------------------------------------------------- 1 | // 2 | // OBJCObjectFactory.m 3 | // SwiftFactory 4 | // 5 | // Created by Joshua Smith on 6/4/14. 6 | // Copyright (c) 2014 iJoshSmith. All rights reserved. 7 | // 8 | 9 | #import "OBJCObjectFactory.h" 10 | 11 | static id OBJCInitWithArg(id target, 12 | SEL initializer, 13 | id argument) 14 | { 15 | IMP imp = [target methodForSelector:initializer]; 16 | id (*initFunc)(id, SEL, id) = (void *)imp; 17 | return initFunc(target, initializer, argument); 18 | } 19 | 20 | @implementation OBJCObjectFactory 21 | 22 | + (id)create:(NSString *)className 23 | { 24 | return [NSClassFromString(className) new]; 25 | } 26 | 27 | + (id)create:(NSString *)className 28 | initializer:(SEL)init 29 | argument:(id)arg 30 | { 31 | Class class = NSClassFromString(className); 32 | return (class && init) 33 | ? OBJCInitWithArg([class alloc], init, arg) 34 | : nil; 35 | } 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /SwiftFactory/ObjectFactory.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ObjectFactory.swift 3 | // SwiftFactory 4 | // 5 | // Created by Joshua Smith on 6/4/14. 6 | // Copyright (c) 2014 iJoshSmith. All rights reserved. 7 | // 8 | 9 | /* 10 | This class requires: 11 | #import "OBJCObjectFactory.h" 12 | in your project's bridging header file. 13 | */ 14 | 15 | import Foundation 16 | 17 | /** Instantiates NSObject subclasses by name. */ 18 | class ObjectFactory 19 | { 20 | /** 21 | Returns a new instance of the specified class, 22 | which should be `TBase` or a subclass thereof. 23 | Uses the parameterless initializer. 24 | */ 25 | class func createInstance(#className: String!) -> TBase? 26 | { 27 | return OBJCObjectFactory.create(className) as TBase? 28 | } 29 | 30 | /** 31 | Returns a new instance of the specified class, 32 | which should be `TBase` or a subclass thereof. 33 | Uses the specified single-parameter initializer. 34 | */ 35 | class func createInstance( 36 | #className: String!, 37 | initializer: Selector!, 38 | argument: AnyObject) -> TBase? 39 | { 40 | return OBJCObjectFactory.create( 41 | className, 42 | initializer: initializer, 43 | argument: argument) as TBase? 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /SwiftFactory/SwiftFactory-Bridging-Header.h: -------------------------------------------------------------------------------- 1 | // 2 | // Use this file to import your target's public headers that you would like to expose to Swift. 3 | // 4 | 5 | #import "OBJCObjectFactory.h" 6 | -------------------------------------------------------------------------------- /SwiftFactory/main.swift: -------------------------------------------------------------------------------- 1 | // 2 | // main.swift 3 | // SwiftFactory 4 | // 5 | // Created by Joshua Smith on 6/4/14. 6 | // Copyright (c) 2014 iJoshSmith. All rights reserved. 7 | // 8 | 9 | let namespace = "SwiftFactory" 10 | 11 | typealias PersonFactory = ObjectFactory 12 | 13 | func createPersonWithClassName( 14 | className: String!, 15 | personName: String? = nil) 16 | { 17 | let qualifiedName = "\(namespace).\(className)" 18 | if personName != nil 19 | { 20 | if let person = PersonFactory.createInstance( 21 | className: qualifiedName, 22 | initializer: "initWithName:", 23 | argument: personName!) 24 | { 25 | println("\(className) name = \(person.name)"); 26 | } 27 | } 28 | else 29 | { 30 | if let person = PersonFactory.createInstance( 31 | className: qualifiedName) 32 | { 33 | println("Created a \(className)"); 34 | } 35 | } 36 | } 37 | 38 | createPersonWithClassName("Stranger") 39 | createPersonWithClassName("Friend", personName: "Steve") 40 | --------------------------------------------------------------------------------