├── .gitignore ├── LICENSE ├── README.md ├── SwiftCoding.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata ├── SwiftCoding ├── AppDelegate.swift ├── Base.lproj │ ├── LaunchScreen.xib │ └── Main.storyboard ├── Images.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Info.plist ├── SwiftCoding.swift └── ViewController.swift └── SwiftCodingTests ├── Book.swift ├── Info.plist └── SwiftCodingTests.swift /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 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 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | 20 | # CocoaPods 21 | # 22 | # We recommend against adding the Pods directory to your .gitignore. However 23 | # you should judge for yourself, the pros and cons are mentioned at: 24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 25 | # 26 | # Pods/ 27 | 28 | # Carthage 29 | # 30 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 31 | # Carthage/Checkouts 32 | 33 | Carthage/Build 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Mustafa Furniturewala 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 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SwiftCoding 2 | Automatic NSCoding for Swift 3 | 4 | ### Installing 5 | 6 | Just add `SwiftCoding.swift` to your project. 7 | 8 | ### Example 9 | 10 | ```swift 11 | @objc public class Book: NSObject, NSCoding { 12 | dynamic var title: String? 13 | dynamic var author: String? 14 | dynamic var pageCount: NSNumber? 15 | dynamic var categories: [String]? 16 | dynamic var available: NSNumber? 17 | 18 | // MARK: NSCoding 19 | 20 | required convenience public init(coder decoder: NSCoder) { 21 | self.init() 22 | setupWithCoder(coder: decoder) 23 | } 24 | 25 | public func encodeWithCoder(coder: NSCoder) { 26 | encode(coder) 27 | } 28 | } 29 | ``` 30 | 31 | ### Contributing 32 | 33 | Pull requests are welcome! 34 | 35 | TODO: 36 | 37 | [] Add support for Swift types that are not representable in Obj-C 38 | 39 | [] 40 | -------------------------------------------------------------------------------- /SwiftCoding.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 458FE4A01B0024AC00C05493 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 458FE49F1B0024AC00C05493 /* AppDelegate.swift */; }; 11 | 458FE4A21B0024AC00C05493 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 458FE4A11B0024AC00C05493 /* ViewController.swift */; }; 12 | 458FE4A51B0024AC00C05493 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 458FE4A31B0024AC00C05493 /* Main.storyboard */; }; 13 | 458FE4A71B0024AC00C05493 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 458FE4A61B0024AC00C05493 /* Images.xcassets */; }; 14 | 458FE4AA1B0024AC00C05493 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 458FE4A81B0024AC00C05493 /* LaunchScreen.xib */; }; 15 | 458FE4B61B0024AC00C05493 /* SwiftCodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 458FE4B51B0024AC00C05493 /* SwiftCodingTests.swift */; }; 16 | 458FE4C01B0024C900C05493 /* SwiftCoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 458FE4BF1B0024C900C05493 /* SwiftCoding.swift */; }; 17 | 458FE4C11B002B3100C05493 /* SwiftCoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 458FE4BF1B0024C900C05493 /* SwiftCoding.swift */; }; 18 | 458FE4C31B002B4500C05493 /* Book.swift in Sources */ = {isa = PBXBuildFile; fileRef = 458FE4C21B002B4500C05493 /* Book.swift */; }; 19 | /* End PBXBuildFile section */ 20 | 21 | /* Begin PBXContainerItemProxy section */ 22 | 458FE4B01B0024AC00C05493 /* PBXContainerItemProxy */ = { 23 | isa = PBXContainerItemProxy; 24 | containerPortal = 458FE4921B0024AB00C05493 /* Project object */; 25 | proxyType = 1; 26 | remoteGlobalIDString = 458FE4991B0024AB00C05493; 27 | remoteInfo = SwiftCoding; 28 | }; 29 | /* End PBXContainerItemProxy section */ 30 | 31 | /* Begin PBXFileReference section */ 32 | 458FE49A1B0024AC00C05493 /* SwiftCoding.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftCoding.app; sourceTree = BUILT_PRODUCTS_DIR; }; 33 | 458FE49E1B0024AC00C05493 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 34 | 458FE49F1B0024AC00C05493 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 35 | 458FE4A11B0024AC00C05493 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 36 | 458FE4A41B0024AC00C05493 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 37 | 458FE4A61B0024AC00C05493 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 38 | 458FE4A91B0024AC00C05493 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; 39 | 458FE4AF1B0024AC00C05493 /* SwiftCodingTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftCodingTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 40 | 458FE4B41B0024AC00C05493 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 41 | 458FE4B51B0024AC00C05493 /* SwiftCodingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftCodingTests.swift; sourceTree = ""; }; 42 | 458FE4BF1B0024C900C05493 /* SwiftCoding.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftCoding.swift; sourceTree = ""; }; 43 | 458FE4C21B002B4500C05493 /* Book.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Book.swift; sourceTree = ""; }; 44 | /* End PBXFileReference section */ 45 | 46 | /* Begin PBXFrameworksBuildPhase section */ 47 | 458FE4971B0024AB00C05493 /* Frameworks */ = { 48 | isa = PBXFrameworksBuildPhase; 49 | buildActionMask = 2147483647; 50 | files = ( 51 | ); 52 | runOnlyForDeploymentPostprocessing = 0; 53 | }; 54 | 458FE4AC1B0024AC00C05493 /* Frameworks */ = { 55 | isa = PBXFrameworksBuildPhase; 56 | buildActionMask = 2147483647; 57 | files = ( 58 | ); 59 | runOnlyForDeploymentPostprocessing = 0; 60 | }; 61 | /* End PBXFrameworksBuildPhase section */ 62 | 63 | /* Begin PBXGroup section */ 64 | 458FE4911B0024AB00C05493 = { 65 | isa = PBXGroup; 66 | children = ( 67 | 458FE49C1B0024AC00C05493 /* SwiftCoding */, 68 | 458FE4B21B0024AC00C05493 /* SwiftCodingTests */, 69 | 458FE49B1B0024AC00C05493 /* Products */, 70 | ); 71 | sourceTree = ""; 72 | }; 73 | 458FE49B1B0024AC00C05493 /* Products */ = { 74 | isa = PBXGroup; 75 | children = ( 76 | 458FE49A1B0024AC00C05493 /* SwiftCoding.app */, 77 | 458FE4AF1B0024AC00C05493 /* SwiftCodingTests.xctest */, 78 | ); 79 | name = Products; 80 | sourceTree = ""; 81 | }; 82 | 458FE49C1B0024AC00C05493 /* SwiftCoding */ = { 83 | isa = PBXGroup; 84 | children = ( 85 | 458FE49F1B0024AC00C05493 /* AppDelegate.swift */, 86 | 458FE4A11B0024AC00C05493 /* ViewController.swift */, 87 | 458FE4A31B0024AC00C05493 /* Main.storyboard */, 88 | 458FE4A61B0024AC00C05493 /* Images.xcassets */, 89 | 458FE4A81B0024AC00C05493 /* LaunchScreen.xib */, 90 | 458FE49D1B0024AC00C05493 /* Supporting Files */, 91 | 458FE4BF1B0024C900C05493 /* SwiftCoding.swift */, 92 | ); 93 | path = SwiftCoding; 94 | sourceTree = ""; 95 | }; 96 | 458FE49D1B0024AC00C05493 /* Supporting Files */ = { 97 | isa = PBXGroup; 98 | children = ( 99 | 458FE49E1B0024AC00C05493 /* Info.plist */, 100 | ); 101 | name = "Supporting Files"; 102 | sourceTree = ""; 103 | }; 104 | 458FE4B21B0024AC00C05493 /* SwiftCodingTests */ = { 105 | isa = PBXGroup; 106 | children = ( 107 | 458FE4B51B0024AC00C05493 /* SwiftCodingTests.swift */, 108 | 458FE4B31B0024AC00C05493 /* Supporting Files */, 109 | 458FE4C21B002B4500C05493 /* Book.swift */, 110 | ); 111 | path = SwiftCodingTests; 112 | sourceTree = ""; 113 | }; 114 | 458FE4B31B0024AC00C05493 /* Supporting Files */ = { 115 | isa = PBXGroup; 116 | children = ( 117 | 458FE4B41B0024AC00C05493 /* Info.plist */, 118 | ); 119 | name = "Supporting Files"; 120 | sourceTree = ""; 121 | }; 122 | /* End PBXGroup section */ 123 | 124 | /* Begin PBXNativeTarget section */ 125 | 458FE4991B0024AB00C05493 /* SwiftCoding */ = { 126 | isa = PBXNativeTarget; 127 | buildConfigurationList = 458FE4B91B0024AC00C05493 /* Build configuration list for PBXNativeTarget "SwiftCoding" */; 128 | buildPhases = ( 129 | 458FE4961B0024AB00C05493 /* Sources */, 130 | 458FE4971B0024AB00C05493 /* Frameworks */, 131 | 458FE4981B0024AB00C05493 /* Resources */, 132 | ); 133 | buildRules = ( 134 | ); 135 | dependencies = ( 136 | ); 137 | name = SwiftCoding; 138 | productName = SwiftCoding; 139 | productReference = 458FE49A1B0024AC00C05493 /* SwiftCoding.app */; 140 | productType = "com.apple.product-type.application"; 141 | }; 142 | 458FE4AE1B0024AC00C05493 /* SwiftCodingTests */ = { 143 | isa = PBXNativeTarget; 144 | buildConfigurationList = 458FE4BC1B0024AC00C05493 /* Build configuration list for PBXNativeTarget "SwiftCodingTests" */; 145 | buildPhases = ( 146 | 458FE4AB1B0024AC00C05493 /* Sources */, 147 | 458FE4AC1B0024AC00C05493 /* Frameworks */, 148 | 458FE4AD1B0024AC00C05493 /* Resources */, 149 | ); 150 | buildRules = ( 151 | ); 152 | dependencies = ( 153 | 458FE4B11B0024AC00C05493 /* PBXTargetDependency */, 154 | ); 155 | name = SwiftCodingTests; 156 | productName = SwiftCodingTests; 157 | productReference = 458FE4AF1B0024AC00C05493 /* SwiftCodingTests.xctest */; 158 | productType = "com.apple.product-type.bundle.unit-test"; 159 | }; 160 | /* End PBXNativeTarget section */ 161 | 162 | /* Begin PBXProject section */ 163 | 458FE4921B0024AB00C05493 /* Project object */ = { 164 | isa = PBXProject; 165 | attributes = { 166 | LastUpgradeCheck = 0630; 167 | ORGANIZATIONNAME = Mustafa; 168 | TargetAttributes = { 169 | 458FE4991B0024AB00C05493 = { 170 | CreatedOnToolsVersion = 6.3.1; 171 | }; 172 | 458FE4AE1B0024AC00C05493 = { 173 | CreatedOnToolsVersion = 6.3.1; 174 | TestTargetID = 458FE4991B0024AB00C05493; 175 | }; 176 | }; 177 | }; 178 | buildConfigurationList = 458FE4951B0024AB00C05493 /* Build configuration list for PBXProject "SwiftCoding" */; 179 | compatibilityVersion = "Xcode 3.2"; 180 | developmentRegion = English; 181 | hasScannedForEncodings = 0; 182 | knownRegions = ( 183 | en, 184 | Base, 185 | ); 186 | mainGroup = 458FE4911B0024AB00C05493; 187 | productRefGroup = 458FE49B1B0024AC00C05493 /* Products */; 188 | projectDirPath = ""; 189 | projectRoot = ""; 190 | targets = ( 191 | 458FE4991B0024AB00C05493 /* SwiftCoding */, 192 | 458FE4AE1B0024AC00C05493 /* SwiftCodingTests */, 193 | ); 194 | }; 195 | /* End PBXProject section */ 196 | 197 | /* Begin PBXResourcesBuildPhase section */ 198 | 458FE4981B0024AB00C05493 /* Resources */ = { 199 | isa = PBXResourcesBuildPhase; 200 | buildActionMask = 2147483647; 201 | files = ( 202 | 458FE4A51B0024AC00C05493 /* Main.storyboard in Resources */, 203 | 458FE4AA1B0024AC00C05493 /* LaunchScreen.xib in Resources */, 204 | 458FE4A71B0024AC00C05493 /* Images.xcassets in Resources */, 205 | ); 206 | runOnlyForDeploymentPostprocessing = 0; 207 | }; 208 | 458FE4AD1B0024AC00C05493 /* Resources */ = { 209 | isa = PBXResourcesBuildPhase; 210 | buildActionMask = 2147483647; 211 | files = ( 212 | ); 213 | runOnlyForDeploymentPostprocessing = 0; 214 | }; 215 | /* End PBXResourcesBuildPhase section */ 216 | 217 | /* Begin PBXSourcesBuildPhase section */ 218 | 458FE4961B0024AB00C05493 /* Sources */ = { 219 | isa = PBXSourcesBuildPhase; 220 | buildActionMask = 2147483647; 221 | files = ( 222 | 458FE4A21B0024AC00C05493 /* ViewController.swift in Sources */, 223 | 458FE4A01B0024AC00C05493 /* AppDelegate.swift in Sources */, 224 | 458FE4C01B0024C900C05493 /* SwiftCoding.swift in Sources */, 225 | ); 226 | runOnlyForDeploymentPostprocessing = 0; 227 | }; 228 | 458FE4AB1B0024AC00C05493 /* Sources */ = { 229 | isa = PBXSourcesBuildPhase; 230 | buildActionMask = 2147483647; 231 | files = ( 232 | 458FE4B61B0024AC00C05493 /* SwiftCodingTests.swift in Sources */, 233 | 458FE4C11B002B3100C05493 /* SwiftCoding.swift in Sources */, 234 | 458FE4C31B002B4500C05493 /* Book.swift in Sources */, 235 | ); 236 | runOnlyForDeploymentPostprocessing = 0; 237 | }; 238 | /* End PBXSourcesBuildPhase section */ 239 | 240 | /* Begin PBXTargetDependency section */ 241 | 458FE4B11B0024AC00C05493 /* PBXTargetDependency */ = { 242 | isa = PBXTargetDependency; 243 | target = 458FE4991B0024AB00C05493 /* SwiftCoding */; 244 | targetProxy = 458FE4B01B0024AC00C05493 /* PBXContainerItemProxy */; 245 | }; 246 | /* End PBXTargetDependency section */ 247 | 248 | /* Begin PBXVariantGroup section */ 249 | 458FE4A31B0024AC00C05493 /* Main.storyboard */ = { 250 | isa = PBXVariantGroup; 251 | children = ( 252 | 458FE4A41B0024AC00C05493 /* Base */, 253 | ); 254 | name = Main.storyboard; 255 | sourceTree = ""; 256 | }; 257 | 458FE4A81B0024AC00C05493 /* LaunchScreen.xib */ = { 258 | isa = PBXVariantGroup; 259 | children = ( 260 | 458FE4A91B0024AC00C05493 /* Base */, 261 | ); 262 | name = LaunchScreen.xib; 263 | sourceTree = ""; 264 | }; 265 | /* End PBXVariantGroup section */ 266 | 267 | /* Begin XCBuildConfiguration section */ 268 | 458FE4B71B0024AC00C05493 /* Debug */ = { 269 | isa = XCBuildConfiguration; 270 | buildSettings = { 271 | ALWAYS_SEARCH_USER_PATHS = NO; 272 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 273 | CLANG_CXX_LIBRARY = "libc++"; 274 | CLANG_ENABLE_MODULES = YES; 275 | CLANG_ENABLE_OBJC_ARC = YES; 276 | CLANG_WARN_BOOL_CONVERSION = YES; 277 | CLANG_WARN_CONSTANT_CONVERSION = YES; 278 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 279 | CLANG_WARN_EMPTY_BODY = YES; 280 | CLANG_WARN_ENUM_CONVERSION = YES; 281 | CLANG_WARN_INT_CONVERSION = YES; 282 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 283 | CLANG_WARN_UNREACHABLE_CODE = YES; 284 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 285 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 286 | COPY_PHASE_STRIP = NO; 287 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 288 | ENABLE_STRICT_OBJC_MSGSEND = YES; 289 | GCC_C_LANGUAGE_STANDARD = gnu99; 290 | GCC_DYNAMIC_NO_PIC = NO; 291 | GCC_NO_COMMON_BLOCKS = YES; 292 | GCC_OPTIMIZATION_LEVEL = 0; 293 | GCC_PREPROCESSOR_DEFINITIONS = ( 294 | "DEBUG=1", 295 | "$(inherited)", 296 | ); 297 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 298 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 299 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 300 | GCC_WARN_UNDECLARED_SELECTOR = YES; 301 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 302 | GCC_WARN_UNUSED_FUNCTION = YES; 303 | GCC_WARN_UNUSED_VARIABLE = YES; 304 | IPHONEOS_DEPLOYMENT_TARGET = 8.3; 305 | MTL_ENABLE_DEBUG_INFO = YES; 306 | ONLY_ACTIVE_ARCH = YES; 307 | SDKROOT = iphoneos; 308 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 309 | TARGETED_DEVICE_FAMILY = "1,2"; 310 | }; 311 | name = Debug; 312 | }; 313 | 458FE4B81B0024AC00C05493 /* Release */ = { 314 | isa = XCBuildConfiguration; 315 | buildSettings = { 316 | ALWAYS_SEARCH_USER_PATHS = NO; 317 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 318 | CLANG_CXX_LIBRARY = "libc++"; 319 | CLANG_ENABLE_MODULES = YES; 320 | CLANG_ENABLE_OBJC_ARC = YES; 321 | CLANG_WARN_BOOL_CONVERSION = YES; 322 | CLANG_WARN_CONSTANT_CONVERSION = YES; 323 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 324 | CLANG_WARN_EMPTY_BODY = YES; 325 | CLANG_WARN_ENUM_CONVERSION = YES; 326 | CLANG_WARN_INT_CONVERSION = YES; 327 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 328 | CLANG_WARN_UNREACHABLE_CODE = YES; 329 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 330 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 331 | COPY_PHASE_STRIP = NO; 332 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 333 | ENABLE_NS_ASSERTIONS = NO; 334 | ENABLE_STRICT_OBJC_MSGSEND = YES; 335 | GCC_C_LANGUAGE_STANDARD = gnu99; 336 | GCC_NO_COMMON_BLOCKS = YES; 337 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 338 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 339 | GCC_WARN_UNDECLARED_SELECTOR = YES; 340 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 341 | GCC_WARN_UNUSED_FUNCTION = YES; 342 | GCC_WARN_UNUSED_VARIABLE = YES; 343 | IPHONEOS_DEPLOYMENT_TARGET = 8.3; 344 | MTL_ENABLE_DEBUG_INFO = NO; 345 | SDKROOT = iphoneos; 346 | TARGETED_DEVICE_FAMILY = "1,2"; 347 | VALIDATE_PRODUCT = YES; 348 | }; 349 | name = Release; 350 | }; 351 | 458FE4BA1B0024AC00C05493 /* Debug */ = { 352 | isa = XCBuildConfiguration; 353 | buildSettings = { 354 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 355 | INFOPLIST_FILE = SwiftCoding/Info.plist; 356 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 357 | PRODUCT_NAME = "$(TARGET_NAME)"; 358 | }; 359 | name = Debug; 360 | }; 361 | 458FE4BB1B0024AC00C05493 /* Release */ = { 362 | isa = XCBuildConfiguration; 363 | buildSettings = { 364 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 365 | INFOPLIST_FILE = SwiftCoding/Info.plist; 366 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 367 | PRODUCT_NAME = "$(TARGET_NAME)"; 368 | }; 369 | name = Release; 370 | }; 371 | 458FE4BD1B0024AC00C05493 /* Debug */ = { 372 | isa = XCBuildConfiguration; 373 | buildSettings = { 374 | BUNDLE_LOADER = "$(TEST_HOST)"; 375 | FRAMEWORK_SEARCH_PATHS = ( 376 | "$(SDKROOT)/Developer/Library/Frameworks", 377 | "$(inherited)", 378 | ); 379 | GCC_PREPROCESSOR_DEFINITIONS = ( 380 | "DEBUG=1", 381 | "$(inherited)", 382 | ); 383 | INFOPLIST_FILE = SwiftCodingTests/Info.plist; 384 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 385 | PRODUCT_NAME = "$(TARGET_NAME)"; 386 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SwiftCoding.app/SwiftCoding"; 387 | }; 388 | name = Debug; 389 | }; 390 | 458FE4BE1B0024AC00C05493 /* Release */ = { 391 | isa = XCBuildConfiguration; 392 | buildSettings = { 393 | BUNDLE_LOADER = "$(TEST_HOST)"; 394 | FRAMEWORK_SEARCH_PATHS = ( 395 | "$(SDKROOT)/Developer/Library/Frameworks", 396 | "$(inherited)", 397 | ); 398 | INFOPLIST_FILE = SwiftCodingTests/Info.plist; 399 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 400 | PRODUCT_NAME = "$(TARGET_NAME)"; 401 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SwiftCoding.app/SwiftCoding"; 402 | }; 403 | name = Release; 404 | }; 405 | /* End XCBuildConfiguration section */ 406 | 407 | /* Begin XCConfigurationList section */ 408 | 458FE4951B0024AB00C05493 /* Build configuration list for PBXProject "SwiftCoding" */ = { 409 | isa = XCConfigurationList; 410 | buildConfigurations = ( 411 | 458FE4B71B0024AC00C05493 /* Debug */, 412 | 458FE4B81B0024AC00C05493 /* Release */, 413 | ); 414 | defaultConfigurationIsVisible = 0; 415 | defaultConfigurationName = Release; 416 | }; 417 | 458FE4B91B0024AC00C05493 /* Build configuration list for PBXNativeTarget "SwiftCoding" */ = { 418 | isa = XCConfigurationList; 419 | buildConfigurations = ( 420 | 458FE4BA1B0024AC00C05493 /* Debug */, 421 | 458FE4BB1B0024AC00C05493 /* Release */, 422 | ); 423 | defaultConfigurationIsVisible = 0; 424 | }; 425 | 458FE4BC1B0024AC00C05493 /* Build configuration list for PBXNativeTarget "SwiftCodingTests" */ = { 426 | isa = XCConfigurationList; 427 | buildConfigurations = ( 428 | 458FE4BD1B0024AC00C05493 /* Debug */, 429 | 458FE4BE1B0024AC00C05493 /* Release */, 430 | ); 431 | defaultConfigurationIsVisible = 0; 432 | }; 433 | /* End XCConfigurationList section */ 434 | }; 435 | rootObject = 458FE4921B0024AB00C05493 /* Project object */; 436 | } 437 | -------------------------------------------------------------------------------- /SwiftCoding.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SwiftCoding/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // SwiftCoding 4 | // 5 | // Created by Mustafa Furniturewala on 5/10/15. 6 | // Copyright (c) 2015 Mustafa. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(application: UIApplication) { 33 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /SwiftCoding/Base.lproj/LaunchScreen.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 20 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /SwiftCoding/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /SwiftCoding/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /SwiftCoding/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | org.mustafa.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /SwiftCoding/SwiftCoding.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SwiftCoding.swift 3 | // SwiftCoding 4 | // 5 | // Created by Mustafa Furniturewala on 5/10/15. 6 | // Copyright (c) 2015 Mustafa. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | extension NSObject { 12 | func codableProperties() -> Dictionary { 13 | var codableProperties = [String: Any?]() 14 | let mirrorOfSelf: MirrorType = reflect(self) 15 | let numberOfProperties = reflect(self).count 16 | for var i = 0; i < numberOfProperties; i++ { 17 | if i == 0 { 18 | // super 19 | continue; 20 | } 21 | let (name,some) = mirrorOfSelf[i] 22 | codableProperties[name] = unwrap(some.value) 23 | } 24 | return codableProperties 25 | } 26 | 27 | func unwrap(any: Any) -> Any? { 28 | let mi:MirrorType = reflect(any) 29 | if mi.disposition != .Optional { 30 | return any 31 | } 32 | if mi.count == 0 { return nil } // Optional.None 33 | let (name,some) = mi[0] 34 | return some.value 35 | } 36 | 37 | public func setupWithCoder(coder decoder: NSCoder) { 38 | for (key, value) in codableProperties() { 39 | let object: AnyObject? = decoder.decodeObjectForKey(key) 40 | setValue(object, forKey: key) 41 | } 42 | } 43 | 44 | public func encode(aCoder: NSCoder) { 45 | for (key, value) in codableProperties() { 46 | switch value { 47 | case let property as AnyObject: 48 | aCoder.encodeObject(property, forKey: key) 49 | case let property as Int: 50 | aCoder.encodeInt(Int32(property), forKey: key) 51 | case let property as Bool: 52 | aCoder.encodeBool(property, forKey: key) 53 | default: 54 | println("Nil value for \(key)") 55 | } 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /SwiftCoding/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // SwiftCoding 4 | // 5 | // Created by Mustafa Furniturewala on 5/10/15. 6 | // Copyright (c) 2015 Mustafa. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | override func viewDidLoad() { 14 | super.viewDidLoad() 15 | // Do any additional setup after loading the view, typically from a nib. 16 | } 17 | 18 | override func didReceiveMemoryWarning() { 19 | super.didReceiveMemoryWarning() 20 | // Dispose of any resources that can be recreated. 21 | } 22 | 23 | 24 | } 25 | 26 | -------------------------------------------------------------------------------- /SwiftCodingTests/Book.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Book.swift 3 | // SwiftCoding 4 | // 5 | // Created by Mustafa Furniturewala on 5/10/15. 6 | // Copyright (c) 2015 Mustafa. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | 12 | @objc public class Book: NSObject, NSCoding { 13 | dynamic var title: String? 14 | dynamic var author: String? 15 | dynamic var pageCount: NSNumber? 16 | dynamic var categories: [String]? 17 | dynamic var available: NSNumber? 18 | 19 | // MARK: NSCoding 20 | 21 | required convenience public init(coder decoder: NSCoder) { 22 | self.init() 23 | setupWithCoder(coder: decoder) 24 | } 25 | 26 | public func encodeWithCoder(coder: NSCoder) { 27 | encode(coder) 28 | } 29 | } -------------------------------------------------------------------------------- /SwiftCodingTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | org.mustafa.$(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 | -------------------------------------------------------------------------------- /SwiftCodingTests/SwiftCodingTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SwiftCodingTests.swift 3 | // SwiftCodingTests 4 | // 5 | // Created by Mustafa Furniturewala on 5/10/15. 6 | // Copyright (c) 2015 Mustafa. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import XCTest 11 | 12 | class SwiftCodingTests: XCTestCase { 13 | 14 | override func setUp() { 15 | super.setUp() 16 | // Put setup code here. This method is called before the invocation of each test method in the class. 17 | } 18 | 19 | override func tearDown() { 20 | // Put teardown code here. This method is called after the invocation of each test method in the class. 21 | super.tearDown() 22 | } 23 | 24 | func testExample() { 25 | let book = Book() 26 | book.title = "Hello World!" 27 | book.pageCount = 3 28 | book.categories = ["one", "two"] 29 | book.available = true 30 | 31 | var pageCount: Int? 32 | var categories: [String]? 33 | var available: Bool? 34 | let data = NSKeyedArchiver.archivedDataWithRootObject(book) 35 | NSUserDefaults.standardUserDefaults().setObject(data, forKey: "book") 36 | if let data = NSUserDefaults.standardUserDefaults().objectForKey("book") as? NSData { 37 | let book = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! Book 38 | XCTAssertEqual(book.title!, "Hello World!") 39 | XCTAssertEqual(book.pageCount!, 3) 40 | XCTAssertEqual(book.categories!.count, 2) 41 | XCTAssertNil(book.author) 42 | } 43 | } 44 | 45 | } 46 | --------------------------------------------------------------------------------