├── .gitignore ├── LICENSE ├── README.md ├── WZLDemoCollection ├── WZLDemoCollection.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata ├── WZLDemoCollection │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Base.lproj │ │ ├── LaunchScreen.xib │ │ └── Main.storyboard │ ├── Biology.h │ ├── Biology.m │ ├── Images.xcassets │ │ └── AppIcon.appiconset │ │ │ └── Contents.json │ ├── Info.plist │ ├── Person.h │ ├── Person.m │ ├── ViewController.h │ ├── ViewController.m │ └── main.m └── WZLDemoCollectionTests │ ├── Info.plist │ └── WZLDemoCollectionTests.m ├── WZLSerializeKit.podspec └── WZLSerializeKit └── WZLSerializeKit.h /.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 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Zilin Weng 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 | # WZLSerializeKit 2 | iOS serialization in 1 line. 3 | 1行代码完成iOS序列化与反序列化,本文件提供iOS下的序列化与反序列集成方案。 4 | 5 | 本文件提供iOS下的序列化与反序列集成方案,封装后用一行代码即可轻松实现。 6 | 在传统方式中当你需要对某个对象进行序列化与反序列化时,往往需要实现协议以及协议(非必须), 7 | 在协议方法中对需要序列化的变量执行encode&decode操作。当变量较多并且需要序列化的自定义对象较多时容易出现过多冗余代码,稍有不慎容易出错。 8 | WZLSerializeKit在运行时对类以及所有层级父类的变量进行遍历,避免遗漏。属性越多,优势越明显。 9 | 10 | Usage: 11 | 12 | Person.h: 13 | ======================================== 14 | #import "WZLSerializeKit.h" 15 | 16 | @interface Person: SomeSuperclass 17 | { 18 | ... 19 | } 20 | 21 | @end 22 | ======================================== 23 | 24 | 25 | Person.m: 26 | ======================================== 27 | #import "Person.h" 28 | 29 | @implementation Person 30 | 31 | WZLSERIALIZE_CODER_DECODER(); 32 | WZLSERIALIZE_COPY_WITH_ZONE(); 33 | WZLSERIALIZE_DESCRIPTION();//(NOT NECESSARY) 34 | 35 | @end 36 | ======================================== 37 | 38 | 39 | at the place where you want to serialize Person instance: 40 | eg: 41 | 42 | //archive object to NSData 43 | WZLSERIALIZE_ARCHIVE(person, @"Person", [self filePath]); 44 | //unarchive NSData to object 45 | Person thePerson = nil; 46 | WZLSERIALIZE_UNARCHIVE(thePerson, @"Person", [self filePath]); 47 | -------------------------------------------------------------------------------- /WZLDemoCollection/WZLDemoCollection.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 19CF06FC1BCE222000B6D195 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 19CF06FB1BCE222000B6D195 /* main.m */; }; 11 | 19CF06FF1BCE222000B6D195 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 19CF06FE1BCE222000B6D195 /* AppDelegate.m */; }; 12 | 19CF07051BCE222000B6D195 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 19CF07031BCE222000B6D195 /* Main.storyboard */; }; 13 | 19CF07071BCE222000B6D195 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 19CF07061BCE222000B6D195 /* Images.xcassets */; }; 14 | 19CF070A1BCE222000B6D195 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 19CF07081BCE222000B6D195 /* LaunchScreen.xib */; }; 15 | 19CF07161BCE222000B6D195 /* WZLDemoCollectionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 19CF07151BCE222000B6D195 /* WZLDemoCollectionTests.m */; }; 16 | 19CF07241BCE56AC00B6D195 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 19CF07231BCE56AC00B6D195 /* ViewController.m */; }; 17 | 19CF07291BCE56BA00B6D195 /* Person.m in Sources */ = {isa = PBXBuildFile; fileRef = 19CF07261BCE56BA00B6D195 /* Person.m */; }; 18 | 19CF072A1BCE56BA00B6D195 /* Biology.m in Sources */ = {isa = PBXBuildFile; fileRef = 19CF07281BCE56BA00B6D195 /* Biology.m */; }; 19 | /* End PBXBuildFile section */ 20 | 21 | /* Begin PBXContainerItemProxy section */ 22 | 19CF07101BCE222000B6D195 /* PBXContainerItemProxy */ = { 23 | isa = PBXContainerItemProxy; 24 | containerPortal = 19CF06EE1BCE222000B6D195 /* Project object */; 25 | proxyType = 1; 26 | remoteGlobalIDString = 19CF06F51BCE222000B6D195; 27 | remoteInfo = WZLDemoCollection; 28 | }; 29 | /* End PBXContainerItemProxy section */ 30 | 31 | /* Begin PBXFileReference section */ 32 | 19CF06F61BCE222000B6D195 /* WZLDemoCollection.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WZLDemoCollection.app; sourceTree = BUILT_PRODUCTS_DIR; }; 33 | 19CF06FA1BCE222000B6D195 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 34 | 19CF06FB1BCE222000B6D195 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 35 | 19CF06FD1BCE222000B6D195 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 36 | 19CF06FE1BCE222000B6D195 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 37 | 19CF07041BCE222000B6D195 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 38 | 19CF07061BCE222000B6D195 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 39 | 19CF07091BCE222000B6D195 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; 40 | 19CF070F1BCE222000B6D195 /* WZLDemoCollectionTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = WZLDemoCollectionTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 41 | 19CF07141BCE222000B6D195 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 42 | 19CF07151BCE222000B6D195 /* WZLDemoCollectionTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = WZLDemoCollectionTests.m; sourceTree = ""; }; 43 | 19CF07221BCE56AC00B6D195 /* ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 44 | 19CF07231BCE56AC00B6D195 /* ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 45 | 19CF07251BCE56BA00B6D195 /* Person.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Person.h; sourceTree = ""; }; 46 | 19CF07261BCE56BA00B6D195 /* Person.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Person.m; sourceTree = ""; }; 47 | 19CF07271BCE56BA00B6D195 /* Biology.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Biology.h; sourceTree = ""; }; 48 | 19CF07281BCE56BA00B6D195 /* Biology.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Biology.m; sourceTree = ""; }; 49 | 19E2CC321BD0A89B003B21DD /* WZLSerializeKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WZLSerializeKit.h; sourceTree = ""; }; 50 | /* End PBXFileReference section */ 51 | 52 | /* Begin PBXFrameworksBuildPhase section */ 53 | 19CF06F31BCE222000B6D195 /* Frameworks */ = { 54 | isa = PBXFrameworksBuildPhase; 55 | buildActionMask = 2147483647; 56 | files = ( 57 | ); 58 | runOnlyForDeploymentPostprocessing = 0; 59 | }; 60 | 19CF070C1BCE222000B6D195 /* Frameworks */ = { 61 | isa = PBXFrameworksBuildPhase; 62 | buildActionMask = 2147483647; 63 | files = ( 64 | ); 65 | runOnlyForDeploymentPostprocessing = 0; 66 | }; 67 | /* End PBXFrameworksBuildPhase section */ 68 | 69 | /* Begin PBXGroup section */ 70 | 19CF06ED1BCE222000B6D195 = { 71 | isa = PBXGroup; 72 | children = ( 73 | 19CF06F81BCE222000B6D195 /* WZLDemoCollection */, 74 | 19CF07121BCE222000B6D195 /* WZLDemoCollectionTests */, 75 | 19CF06F71BCE222000B6D195 /* Products */, 76 | ); 77 | sourceTree = ""; 78 | }; 79 | 19CF06F71BCE222000B6D195 /* Products */ = { 80 | isa = PBXGroup; 81 | children = ( 82 | 19CF06F61BCE222000B6D195 /* WZLDemoCollection.app */, 83 | 19CF070F1BCE222000B6D195 /* WZLDemoCollectionTests.xctest */, 84 | ); 85 | name = Products; 86 | sourceTree = ""; 87 | }; 88 | 19CF06F81BCE222000B6D195 /* WZLDemoCollection */ = { 89 | isa = PBXGroup; 90 | children = ( 91 | 19E2CC311BD0A89B003B21DD /* WZLSerializeKit */, 92 | 19CF07251BCE56BA00B6D195 /* Person.h */, 93 | 19CF07261BCE56BA00B6D195 /* Person.m */, 94 | 19CF07271BCE56BA00B6D195 /* Biology.h */, 95 | 19CF07281BCE56BA00B6D195 /* Biology.m */, 96 | 19CF06FD1BCE222000B6D195 /* AppDelegate.h */, 97 | 19CF06FE1BCE222000B6D195 /* AppDelegate.m */, 98 | 19CF07221BCE56AC00B6D195 /* ViewController.h */, 99 | 19CF07231BCE56AC00B6D195 /* ViewController.m */, 100 | 19CF07031BCE222000B6D195 /* Main.storyboard */, 101 | 19CF07061BCE222000B6D195 /* Images.xcassets */, 102 | 19CF07081BCE222000B6D195 /* LaunchScreen.xib */, 103 | 19CF06F91BCE222000B6D195 /* Supporting Files */, 104 | ); 105 | path = WZLDemoCollection; 106 | sourceTree = ""; 107 | }; 108 | 19CF06F91BCE222000B6D195 /* Supporting Files */ = { 109 | isa = PBXGroup; 110 | children = ( 111 | 19CF06FA1BCE222000B6D195 /* Info.plist */, 112 | 19CF06FB1BCE222000B6D195 /* main.m */, 113 | ); 114 | name = "Supporting Files"; 115 | sourceTree = ""; 116 | }; 117 | 19CF07121BCE222000B6D195 /* WZLDemoCollectionTests */ = { 118 | isa = PBXGroup; 119 | children = ( 120 | 19CF07151BCE222000B6D195 /* WZLDemoCollectionTests.m */, 121 | 19CF07131BCE222000B6D195 /* Supporting Files */, 122 | ); 123 | path = WZLDemoCollectionTests; 124 | sourceTree = ""; 125 | }; 126 | 19CF07131BCE222000B6D195 /* Supporting Files */ = { 127 | isa = PBXGroup; 128 | children = ( 129 | 19CF07141BCE222000B6D195 /* Info.plist */, 130 | ); 131 | name = "Supporting Files"; 132 | sourceTree = ""; 133 | }; 134 | 19E2CC311BD0A89B003B21DD /* WZLSerializeKit */ = { 135 | isa = PBXGroup; 136 | children = ( 137 | 19E2CC321BD0A89B003B21DD /* WZLSerializeKit.h */, 138 | ); 139 | name = WZLSerializeKit; 140 | path = ../../WZLSerializeKit; 141 | sourceTree = ""; 142 | }; 143 | /* End PBXGroup section */ 144 | 145 | /* Begin PBXNativeTarget section */ 146 | 19CF06F51BCE222000B6D195 /* WZLDemoCollection */ = { 147 | isa = PBXNativeTarget; 148 | buildConfigurationList = 19CF07191BCE222000B6D195 /* Build configuration list for PBXNativeTarget "WZLDemoCollection" */; 149 | buildPhases = ( 150 | 19CF06F21BCE222000B6D195 /* Sources */, 151 | 19CF06F31BCE222000B6D195 /* Frameworks */, 152 | 19CF06F41BCE222000B6D195 /* Resources */, 153 | ); 154 | buildRules = ( 155 | ); 156 | dependencies = ( 157 | ); 158 | name = WZLDemoCollection; 159 | productName = WZLDemoCollection; 160 | productReference = 19CF06F61BCE222000B6D195 /* WZLDemoCollection.app */; 161 | productType = "com.apple.product-type.application"; 162 | }; 163 | 19CF070E1BCE222000B6D195 /* WZLDemoCollectionTests */ = { 164 | isa = PBXNativeTarget; 165 | buildConfigurationList = 19CF071C1BCE222000B6D195 /* Build configuration list for PBXNativeTarget "WZLDemoCollectionTests" */; 166 | buildPhases = ( 167 | 19CF070B1BCE222000B6D195 /* Sources */, 168 | 19CF070C1BCE222000B6D195 /* Frameworks */, 169 | 19CF070D1BCE222000B6D195 /* Resources */, 170 | ); 171 | buildRules = ( 172 | ); 173 | dependencies = ( 174 | 19CF07111BCE222000B6D195 /* PBXTargetDependency */, 175 | ); 176 | name = WZLDemoCollectionTests; 177 | productName = WZLDemoCollectionTests; 178 | productReference = 19CF070F1BCE222000B6D195 /* WZLDemoCollectionTests.xctest */; 179 | productType = "com.apple.product-type.bundle.unit-test"; 180 | }; 181 | /* End PBXNativeTarget section */ 182 | 183 | /* Begin PBXProject section */ 184 | 19CF06EE1BCE222000B6D195 /* Project object */ = { 185 | isa = PBXProject; 186 | attributes = { 187 | LastUpgradeCheck = 0640; 188 | ORGANIZATIONNAME = wengzilin; 189 | TargetAttributes = { 190 | 19CF06F51BCE222000B6D195 = { 191 | CreatedOnToolsVersion = 6.4; 192 | }; 193 | 19CF070E1BCE222000B6D195 = { 194 | CreatedOnToolsVersion = 6.4; 195 | TestTargetID = 19CF06F51BCE222000B6D195; 196 | }; 197 | }; 198 | }; 199 | buildConfigurationList = 19CF06F11BCE222000B6D195 /* Build configuration list for PBXProject "WZLDemoCollection" */; 200 | compatibilityVersion = "Xcode 3.2"; 201 | developmentRegion = English; 202 | hasScannedForEncodings = 0; 203 | knownRegions = ( 204 | en, 205 | Base, 206 | ); 207 | mainGroup = 19CF06ED1BCE222000B6D195; 208 | productRefGroup = 19CF06F71BCE222000B6D195 /* Products */; 209 | projectDirPath = ""; 210 | projectRoot = ""; 211 | targets = ( 212 | 19CF06F51BCE222000B6D195 /* WZLDemoCollection */, 213 | 19CF070E1BCE222000B6D195 /* WZLDemoCollectionTests */, 214 | ); 215 | }; 216 | /* End PBXProject section */ 217 | 218 | /* Begin PBXResourcesBuildPhase section */ 219 | 19CF06F41BCE222000B6D195 /* Resources */ = { 220 | isa = PBXResourcesBuildPhase; 221 | buildActionMask = 2147483647; 222 | files = ( 223 | 19CF07051BCE222000B6D195 /* Main.storyboard in Resources */, 224 | 19CF070A1BCE222000B6D195 /* LaunchScreen.xib in Resources */, 225 | 19CF07071BCE222000B6D195 /* Images.xcassets in Resources */, 226 | ); 227 | runOnlyForDeploymentPostprocessing = 0; 228 | }; 229 | 19CF070D1BCE222000B6D195 /* Resources */ = { 230 | isa = PBXResourcesBuildPhase; 231 | buildActionMask = 2147483647; 232 | files = ( 233 | ); 234 | runOnlyForDeploymentPostprocessing = 0; 235 | }; 236 | /* End PBXResourcesBuildPhase section */ 237 | 238 | /* Begin PBXSourcesBuildPhase section */ 239 | 19CF06F21BCE222000B6D195 /* Sources */ = { 240 | isa = PBXSourcesBuildPhase; 241 | buildActionMask = 2147483647; 242 | files = ( 243 | 19CF07241BCE56AC00B6D195 /* ViewController.m in Sources */, 244 | 19CF06FF1BCE222000B6D195 /* AppDelegate.m in Sources */, 245 | 19CF07291BCE56BA00B6D195 /* Person.m in Sources */, 246 | 19CF06FC1BCE222000B6D195 /* main.m in Sources */, 247 | 19CF072A1BCE56BA00B6D195 /* Biology.m in Sources */, 248 | ); 249 | runOnlyForDeploymentPostprocessing = 0; 250 | }; 251 | 19CF070B1BCE222000B6D195 /* Sources */ = { 252 | isa = PBXSourcesBuildPhase; 253 | buildActionMask = 2147483647; 254 | files = ( 255 | 19CF07161BCE222000B6D195 /* WZLDemoCollectionTests.m in Sources */, 256 | ); 257 | runOnlyForDeploymentPostprocessing = 0; 258 | }; 259 | /* End PBXSourcesBuildPhase section */ 260 | 261 | /* Begin PBXTargetDependency section */ 262 | 19CF07111BCE222000B6D195 /* PBXTargetDependency */ = { 263 | isa = PBXTargetDependency; 264 | target = 19CF06F51BCE222000B6D195 /* WZLDemoCollection */; 265 | targetProxy = 19CF07101BCE222000B6D195 /* PBXContainerItemProxy */; 266 | }; 267 | /* End PBXTargetDependency section */ 268 | 269 | /* Begin PBXVariantGroup section */ 270 | 19CF07031BCE222000B6D195 /* Main.storyboard */ = { 271 | isa = PBXVariantGroup; 272 | children = ( 273 | 19CF07041BCE222000B6D195 /* Base */, 274 | ); 275 | name = Main.storyboard; 276 | sourceTree = ""; 277 | }; 278 | 19CF07081BCE222000B6D195 /* LaunchScreen.xib */ = { 279 | isa = PBXVariantGroup; 280 | children = ( 281 | 19CF07091BCE222000B6D195 /* Base */, 282 | ); 283 | name = LaunchScreen.xib; 284 | sourceTree = ""; 285 | }; 286 | /* End PBXVariantGroup section */ 287 | 288 | /* Begin XCBuildConfiguration section */ 289 | 19CF07171BCE222000B6D195 /* Debug */ = { 290 | isa = XCBuildConfiguration; 291 | buildSettings = { 292 | ALWAYS_SEARCH_USER_PATHS = NO; 293 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 294 | CLANG_CXX_LIBRARY = "libc++"; 295 | CLANG_ENABLE_MODULES = YES; 296 | CLANG_ENABLE_OBJC_ARC = YES; 297 | CLANG_WARN_BOOL_CONVERSION = YES; 298 | CLANG_WARN_CONSTANT_CONVERSION = YES; 299 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 300 | CLANG_WARN_EMPTY_BODY = YES; 301 | CLANG_WARN_ENUM_CONVERSION = YES; 302 | CLANG_WARN_INT_CONVERSION = YES; 303 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 304 | CLANG_WARN_UNREACHABLE_CODE = YES; 305 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 306 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 307 | COPY_PHASE_STRIP = NO; 308 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 309 | ENABLE_STRICT_OBJC_MSGSEND = YES; 310 | GCC_C_LANGUAGE_STANDARD = gnu99; 311 | GCC_DYNAMIC_NO_PIC = NO; 312 | GCC_NO_COMMON_BLOCKS = YES; 313 | GCC_OPTIMIZATION_LEVEL = 0; 314 | GCC_PREPROCESSOR_DEFINITIONS = ( 315 | "DEBUG=1", 316 | "$(inherited)", 317 | ); 318 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 319 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 320 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 321 | GCC_WARN_UNDECLARED_SELECTOR = YES; 322 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 323 | GCC_WARN_UNUSED_FUNCTION = YES; 324 | GCC_WARN_UNUSED_VARIABLE = YES; 325 | IPHONEOS_DEPLOYMENT_TARGET = 8.4; 326 | MTL_ENABLE_DEBUG_INFO = YES; 327 | ONLY_ACTIVE_ARCH = YES; 328 | SDKROOT = iphoneos; 329 | }; 330 | name = Debug; 331 | }; 332 | 19CF07181BCE222000B6D195 /* Release */ = { 333 | isa = XCBuildConfiguration; 334 | buildSettings = { 335 | ALWAYS_SEARCH_USER_PATHS = NO; 336 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 337 | CLANG_CXX_LIBRARY = "libc++"; 338 | CLANG_ENABLE_MODULES = YES; 339 | CLANG_ENABLE_OBJC_ARC = YES; 340 | CLANG_WARN_BOOL_CONVERSION = YES; 341 | CLANG_WARN_CONSTANT_CONVERSION = YES; 342 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 343 | CLANG_WARN_EMPTY_BODY = YES; 344 | CLANG_WARN_ENUM_CONVERSION = YES; 345 | CLANG_WARN_INT_CONVERSION = YES; 346 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 347 | CLANG_WARN_UNREACHABLE_CODE = YES; 348 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 349 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 350 | COPY_PHASE_STRIP = NO; 351 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 352 | ENABLE_NS_ASSERTIONS = NO; 353 | ENABLE_STRICT_OBJC_MSGSEND = YES; 354 | GCC_C_LANGUAGE_STANDARD = gnu99; 355 | GCC_NO_COMMON_BLOCKS = YES; 356 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 357 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 358 | GCC_WARN_UNDECLARED_SELECTOR = YES; 359 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 360 | GCC_WARN_UNUSED_FUNCTION = YES; 361 | GCC_WARN_UNUSED_VARIABLE = YES; 362 | IPHONEOS_DEPLOYMENT_TARGET = 8.4; 363 | MTL_ENABLE_DEBUG_INFO = NO; 364 | SDKROOT = iphoneos; 365 | VALIDATE_PRODUCT = YES; 366 | }; 367 | name = Release; 368 | }; 369 | 19CF071A1BCE222000B6D195 /* Debug */ = { 370 | isa = XCBuildConfiguration; 371 | buildSettings = { 372 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 373 | INFOPLIST_FILE = WZLDemoCollection/Info.plist; 374 | IPHONEOS_DEPLOYMENT_TARGET = 6.0; 375 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 376 | PRODUCT_NAME = "$(TARGET_NAME)"; 377 | PROVISIONING_PROFILE = "87d35d55-5d56-45c5-831c-17634179a8de"; 378 | }; 379 | name = Debug; 380 | }; 381 | 19CF071B1BCE222000B6D195 /* Release */ = { 382 | isa = XCBuildConfiguration; 383 | buildSettings = { 384 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 385 | INFOPLIST_FILE = WZLDemoCollection/Info.plist; 386 | IPHONEOS_DEPLOYMENT_TARGET = 6.0; 387 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 388 | PRODUCT_NAME = "$(TARGET_NAME)"; 389 | PROVISIONING_PROFILE = "87d35d55-5d56-45c5-831c-17634179a8de"; 390 | }; 391 | name = Release; 392 | }; 393 | 19CF071D1BCE222000B6D195 /* Debug */ = { 394 | isa = XCBuildConfiguration; 395 | buildSettings = { 396 | BUNDLE_LOADER = "$(TEST_HOST)"; 397 | FRAMEWORK_SEARCH_PATHS = ( 398 | "$(SDKROOT)/Developer/Library/Frameworks", 399 | "$(inherited)", 400 | ); 401 | GCC_PREPROCESSOR_DEFINITIONS = ( 402 | "DEBUG=1", 403 | "$(inherited)", 404 | ); 405 | INFOPLIST_FILE = WZLDemoCollectionTests/Info.plist; 406 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 407 | PRODUCT_NAME = "$(TARGET_NAME)"; 408 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/WZLDemoCollection.app/WZLDemoCollection"; 409 | }; 410 | name = Debug; 411 | }; 412 | 19CF071E1BCE222000B6D195 /* Release */ = { 413 | isa = XCBuildConfiguration; 414 | buildSettings = { 415 | BUNDLE_LOADER = "$(TEST_HOST)"; 416 | FRAMEWORK_SEARCH_PATHS = ( 417 | "$(SDKROOT)/Developer/Library/Frameworks", 418 | "$(inherited)", 419 | ); 420 | INFOPLIST_FILE = WZLDemoCollectionTests/Info.plist; 421 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 422 | PRODUCT_NAME = "$(TARGET_NAME)"; 423 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/WZLDemoCollection.app/WZLDemoCollection"; 424 | }; 425 | name = Release; 426 | }; 427 | /* End XCBuildConfiguration section */ 428 | 429 | /* Begin XCConfigurationList section */ 430 | 19CF06F11BCE222000B6D195 /* Build configuration list for PBXProject "WZLDemoCollection" */ = { 431 | isa = XCConfigurationList; 432 | buildConfigurations = ( 433 | 19CF07171BCE222000B6D195 /* Debug */, 434 | 19CF07181BCE222000B6D195 /* Release */, 435 | ); 436 | defaultConfigurationIsVisible = 0; 437 | defaultConfigurationName = Release; 438 | }; 439 | 19CF07191BCE222000B6D195 /* Build configuration list for PBXNativeTarget "WZLDemoCollection" */ = { 440 | isa = XCConfigurationList; 441 | buildConfigurations = ( 442 | 19CF071A1BCE222000B6D195 /* Debug */, 443 | 19CF071B1BCE222000B6D195 /* Release */, 444 | ); 445 | defaultConfigurationIsVisible = 0; 446 | defaultConfigurationName = Release; 447 | }; 448 | 19CF071C1BCE222000B6D195 /* Build configuration list for PBXNativeTarget "WZLDemoCollectionTests" */ = { 449 | isa = XCConfigurationList; 450 | buildConfigurations = ( 451 | 19CF071D1BCE222000B6D195 /* Debug */, 452 | 19CF071E1BCE222000B6D195 /* Release */, 453 | ); 454 | defaultConfigurationIsVisible = 0; 455 | defaultConfigurationName = Release; 456 | }; 457 | /* End XCConfigurationList section */ 458 | }; 459 | rootObject = 19CF06EE1BCE222000B6D195 /* Project object */; 460 | } 461 | -------------------------------------------------------------------------------- /WZLDemoCollection/WZLDemoCollection.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /WZLDemoCollection/WZLDemoCollection/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // WZLDemoCollection 4 | // 5 | // Created by wengzilin on 15/10/14. 6 | // Copyright (c) 2015年 wengzilin. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /WZLDemoCollection/WZLDemoCollection/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // WZLDemoCollection 4 | // 5 | // Created by wengzilin on 15/10/14. 6 | // Copyright (c) 2015年 wengzilin. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | - (void)applicationWillResignActive:(UIApplication *)application { 24 | // 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. 25 | // 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. 26 | } 27 | 28 | - (void)applicationDidEnterBackground:(UIApplication *)application { 29 | // 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. 30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 31 | } 32 | 33 | - (void)applicationWillEnterForeground:(UIApplication *)application { 34 | // 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. 35 | } 36 | 37 | - (void)applicationDidBecomeActive:(UIApplication *)application { 38 | // 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. 39 | } 40 | 41 | - (void)applicationWillTerminate:(UIApplication *)application { 42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /WZLDemoCollection/WZLDemoCollection/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 | -------------------------------------------------------------------------------- /WZLDemoCollection/WZLDemoCollection/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 | -------------------------------------------------------------------------------- /WZLDemoCollection/WZLDemoCollection/Biology.h: -------------------------------------------------------------------------------- 1 | // 2 | // Biology.h 3 | // RuntimeDemo 4 | // 5 | // Created by wengzilin on 15/10/14. 6 | // Copyright (c) 2015年 wengzilin. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface Biology : NSObject 12 | { 13 | NSInteger *_hairCountInBiology; 14 | } 15 | 16 | @property (nonatomic, copy) NSString *introInBiology; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /WZLDemoCollection/WZLDemoCollection/Biology.m: -------------------------------------------------------------------------------- 1 | // 2 | // Biology.m 3 | // RuntimeDemo 4 | // 5 | // Created by wengzilin on 15/10/14. 6 | // Copyright (c) 2015年 wengzilin. All rights reserved. 7 | // 8 | 9 | #import "Biology.h" 10 | #import 11 | 12 | @implementation Biology 13 | 14 | @end 15 | -------------------------------------------------------------------------------- /WZLDemoCollection/WZLDemoCollection/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 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /WZLDemoCollection/WZLDemoCollection/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | com.autonavi.$(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 | 40 | 41 | -------------------------------------------------------------------------------- /WZLDemoCollection/WZLDemoCollection/Person.h: -------------------------------------------------------------------------------- 1 | // 2 | // Person.h 3 | // RuntimeDemo 4 | // 5 | // Created by wengzilin on 15/10/12. 6 | // Copyright (c) 2015年 wengzilin. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "Biology.h" 11 | 12 | @interface Person : Biology 13 | { 14 | NSString *_father; 15 | } 16 | 17 | @property (nonatomic, copy) NSString *name; 18 | @property (nonatomic, assign) NSInteger age; 19 | 20 | 21 | @end 22 | -------------------------------------------------------------------------------- /WZLDemoCollection/WZLDemoCollection/Person.m: -------------------------------------------------------------------------------- 1 | 2 | // 3 | // Person.m 4 | // RuntimeDemo 5 | // 6 | // Created by wengzilin on 15/10/12. 7 | // Copyright (c) 2015年 wengzilin. All rights reserved. 8 | // 9 | 10 | #import "Person.h" 11 | #import 12 | #import "WZLSerializeKit.h" 13 | 14 | //是否使用通用的encode/decode代码一次性encode/decode 15 | #define USING_ENCODE_KIT 1 16 | 17 | @implementation Person 18 | 19 | WZLSERIALIZE_CODER_DECODER(); 20 | 21 | WZLSERIALIZE_COPY_WITH_ZONE(); 22 | 23 | WZLSERIALIZE_DESCRIPTION(); 24 | 25 | @end 26 | -------------------------------------------------------------------------------- /WZLDemoCollection/WZLDemoCollection/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // RuntimeDemo 4 | // 5 | // Created by wengzilin on 15/10/8. 6 | // Copyright (c) 2015年 wengzilin. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /WZLDemoCollection/WZLDemoCollection/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // RuntimeDemo 4 | // 5 | // Created by wengzilin on 15/10/8. 6 | // Copyright (c) 2015年 wengzilin. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "Person.h" 11 | #import 12 | #import "WZLSerializeKit.h" 13 | 14 | @interface ViewController () 15 | 16 | @end 17 | 18 | @implementation ViewController 19 | 20 | - (void)viewDidLoad { 21 | [super viewDidLoad]; 22 | // Do any additional setup after loading the view, typically from a nib. 23 | 24 | Person *person = [[Person alloc] init]; 25 | person.name = @"wengzilin"; 26 | person.age = 26; 27 | [person setValue:@"laoweng" forKey:@"_father"]; 28 | //set value of superClass 29 | person.introInBiology = @"I am a biology on earth"; 30 | //[person setValue:@(10000) forKey:@"_hairCountInBiology"];//no access to private instance in super 31 | 32 | 33 | NSLog(@"Before archiver:\n%@", [person description]); 34 | 35 | WZLSERIALIZE_ARCHIVE(person, @"Person", [self filePath]); 36 | Person *thePerson = nil; 37 | WZLSERIALIZE_UNARCHIVE(thePerson, @"Person", [self filePath]); 38 | 39 | Person *copyPerson = [person copy]; 40 | NSLog(@"copyPerson:%@", [copyPerson description]); 41 | } 42 | 43 | - (NSString *)filePath 44 | { 45 | NSString *archiverFilePath = [NSString stringWithFormat:@"%@/archiver", NSHomeDirectory()]; 46 | return archiverFilePath; 47 | } 48 | 49 | - (void)didReceiveMemoryWarning { 50 | [super didReceiveMemoryWarning]; 51 | // Dispose of any resources that can be recreated. 52 | } 53 | 54 | @end 55 | -------------------------------------------------------------------------------- /WZLDemoCollection/WZLDemoCollection/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // WZLDemoCollection 4 | // 5 | // Created by wengzilin on 15/10/14. 6 | // Copyright (c) 2015年 wengzilin. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /WZLDemoCollection/WZLDemoCollectionTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | com.wengzilin.$(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 | -------------------------------------------------------------------------------- /WZLDemoCollection/WZLDemoCollectionTests/WZLDemoCollectionTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // WZLDemoCollectionTests.m 3 | // WZLDemoCollectionTests 4 | // 5 | // Created by wengzilin on 15/10/14. 6 | // Copyright (c) 2015年 wengzilin. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @interface WZLDemoCollectionTests : XCTestCase 13 | 14 | @end 15 | 16 | @implementation WZLDemoCollectionTests 17 | 18 | - (void)setUp { 19 | [super setUp]; 20 | // Put setup code here. This method is called before the invocation of each test method in the class. 21 | } 22 | 23 | - (void)tearDown { 24 | // Put teardown code here. This method is called after the invocation of each test method in the class. 25 | [super tearDown]; 26 | } 27 | 28 | - (void)testExample { 29 | // This is an example of a functional test case. 30 | XCTAssert(YES, @"Pass"); 31 | } 32 | 33 | - (void)testPerformanceExample { 34 | // This is an example of a performance test case. 35 | [self measureBlock:^{ 36 | // Put the code you want to measure the time of here. 37 | }]; 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /WZLSerializeKit.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = 'WZLSerializeKit' 3 | s.version = '1.2.2' 4 | s.summary = 'A four-line tool to enable serialize and deserialize in iOS platform' 5 | s.description = <<-DESC 6 | A four-line tool to enable serialize and deserialize in iOS platform. 4行代码完成iOS序列化与反序列化. 7 | DESC 8 | s.homepage = 'https://github.com/weng1250/WZLSerializeKit' 9 | s.license = { :type => 'None', :file => 'LICENSE' } 10 | s.author = { 'Zilin Weng翁子林' => "zilin_weng@163.com" } 11 | s.source = { :git => 'https://github.com/weng1250/WZLSerializeKit.git', :tag => s.version} 12 | s.platform = :ios, '6.0' 13 | s.requires_arc = true 14 | 15 | s.source_files = 'WZLSerializeKit/*.{h,m}' 16 | s.public_header_files = 'WZLSerializeKit/*.h' 17 | end 18 | -------------------------------------------------------------------------------- /WZLSerializeKit/WZLSerializeKit.h: -------------------------------------------------------------------------------- 1 | // 2 | // WZLSerializeKit.h 3 | // 4 | // Created by wengzilin on 15/10/15. 5 | // Copyright (c) 2015年 wengzilin. All rights reserved. 6 | // 7 | 8 | #ifndef WZLSerializeKit_h 9 | #define WZLSerializeKit_h 10 | 11 | #import 12 | 13 | /* (EN)Provide Serialization kit for class (in ARC mode). With this serialize kit, there is no need any more 14 | * to implement (encode:) or )decode:) everywhere, like: 15 | * 16 | * self.name = [coder decodeObjectForKey:@"name"]; 17 | self.age = [[coder decodeObjectForKey:@"age"] integerValue]; 18 | _father = [coder decodeObjectForKey:@"_father"]; 19 | 20 | [coder encodeObject:self.name forKey:@"name"]; 21 | [coder encodeObject:@(self.age) forKey:@"age"]; 22 | [coder encodeObject:_father forKey:@"_father"]; 23 | * 24 | * (CN)本文件提供iOS下的序列化与反序列集成方案,几行代码即可轻松实现。 25 | * 在传统方式中当你需要对某个对象进行序列化与反序列化时,往往需要实现协议以及协议(非必须), 26 | * 在协议方法中对需要序列化的变量执行encode&decode操作。当变量较多并且需要序列化的自定义对象较多时容易出现过多冗余代码,稍有不慎容易出错。 27 | * WZLSerializeKit在运行时对类以及所有层级父类的变量进行遍历,避免遗漏。属性越多,优势越明显。 28 | * 29 | * Usage: 30 | * 31 | * Person.h: 32 | * ======================================== 33 | * #import "WZLSerializeKit.h" 34 | * 35 | * @interface Person: SomeSuperclass 36 | * { 37 | * ... 38 | * } 39 | * 40 | * @end 41 | * ======================================== 42 | * 43 | * 44 | * Person.m: 45 | * ======================================== 46 | * #import "Person.h" 47 | * 48 | * @implementation Person 49 | * 50 | * WZLSERIALIZE_CODER_DECODER(); 51 | * WZLSERIALIZE_COPY_WITH_ZONE(); 52 | * WZLSERIALIZE_DESCRIPTION();//(NOT NECESSARY) 53 | * 54 | * @end 55 | * ======================================== 56 | * 57 | * 58 | * at the place where you want to serialize Person instance: 59 | * eg: 60 | * //archive object to NSData 61 | * WZLSERIALIZE_ARCHIVE(person, @"Person", [self filePath]); 62 | * 63 | * //unarchive NSData to object 64 | * Person *thePerson = nil; 65 | * WZLSERIALIZE_UNARCHIVE(thePerson, @"Person", [self filePath]); 66 | */ 67 | 68 | #define WZLSERIALIZE_CODER_DECODER() \ 69 | \ 70 | - (id)initWithCoder:(NSCoder *)coder \ 71 | { \ 72 | NSLog(@"%s",__func__); \ 73 | Class cls = [self class]; \ 74 | while (cls != [NSObject class]) { \ 75 | /*判断是自身类还是父类*/ \ 76 | BOOL bIsSelfClass = (cls == [self class]); \ 77 | unsigned int iVarCount = 0; \ 78 | unsigned int propVarCount = 0; \ 79 | unsigned int sharedVarCount = 0; \ 80 | Ivar *ivarList = bIsSelfClass ? class_copyIvarList([cls class], &iVarCount) : NULL;/*变量列表,含属性以及私有变量*/ \ 81 | objc_property_t *propList = bIsSelfClass ? NULL : class_copyPropertyList(cls, &propVarCount);/*属性列表*/ \ 82 | sharedVarCount = bIsSelfClass ? iVarCount : propVarCount; \ 83 | \ 84 | for (int i = 0; i < sharedVarCount; i++) { \ 85 | const char *varName = bIsSelfClass ? ivar_getName(*(ivarList + i)) : property_getName(*(propList + i)); \ 86 | NSString *key = [NSString stringWithUTF8String:varName]; \ 87 | id varValue = [coder decodeObjectForKey:key]; \ 88 | NSArray *filters = @[@"superclass", @"description", @"debugDescription", @"hash"]; \ 89 | if (varValue && [filters containsObject:key] == NO) { \ 90 | [self setValue:varValue forKey:key]; \ 91 | } \ 92 | } \ 93 | free(ivarList); \ 94 | free(propList); \ 95 | cls = class_getSuperclass(cls); \ 96 | } \ 97 | return self; \ 98 | } \ 99 | \ 100 | - (void)encodeWithCoder:(NSCoder *)coder \ 101 | { \ 102 | NSLog(@"%s",__func__); \ 103 | Class cls = [self class]; \ 104 | while (cls != [NSObject class]) { \ 105 | /*判断是自身类还是父类*/ \ 106 | BOOL bIsSelfClass = (cls == [self class]); \ 107 | unsigned int iVarCount = 0; \ 108 | unsigned int propVarCount = 0; \ 109 | unsigned int sharedVarCount = 0; \ 110 | Ivar *ivarList = bIsSelfClass ? class_copyIvarList([cls class], &iVarCount) : NULL;/*变量列表,含属性以及私有变量*/ \ 111 | objc_property_t *propList = bIsSelfClass ? NULL : class_copyPropertyList(cls, &propVarCount);/*属性列表*/ \ 112 | sharedVarCount = bIsSelfClass ? iVarCount : propVarCount; \ 113 | \ 114 | for (int i = 0; i < sharedVarCount; i++) { \ 115 | const char *varName = bIsSelfClass ? ivar_getName(*(ivarList + i)) : property_getName(*(propList + i)); \ 116 | NSString *key = [NSString stringWithUTF8String:varName]; \ 117 | /*valueForKey只能获取本类所有变量以及所有层级父类的属性,不包含任何父类的私有变量(会崩溃)*/ \ 118 | id varValue = [self valueForKey:key]; \ 119 | NSArray *filters = @[@"superclass", @"description", @"debugDescription", @"hash"]; \ 120 | if (varValue && [filters containsObject:key] == NO) { \ 121 | [coder encodeObject:varValue forKey:key]; \ 122 | } \ 123 | } \ 124 | free(ivarList); \ 125 | free(propList); \ 126 | cls = class_getSuperclass(cls); \ 127 | } \ 128 | } 129 | 130 | 131 | #define WZLSERIALIZE_COPY_WITH_ZONE() \ 132 | \ 133 | /*如果不实现copyWithZone:方法,则[personObject copy]时会崩溃*/ \ 134 | - (id)copyWithZone:(NSZone *)zone \ 135 | { \ 136 | NSLog(@"%s",__func__); \ 137 | id copy = [[[self class] allocWithZone:zone] init]; \ 138 | Class cls = [self class]; \ 139 | while (cls != [NSObject class]) { \ 140 | /*判断是自身类还是父类*/ \ 141 | BOOL bIsSelfClass = (cls == [self class]); \ 142 | unsigned int iVarCount = 0; \ 143 | unsigned int propVarCount = 0; \ 144 | unsigned int sharedVarCount = 0; \ 145 | Ivar *ivarList = bIsSelfClass ? class_copyIvarList([cls class], &iVarCount) : NULL;/*变量列表,含属性以及私有变量*/ \ 146 | objc_property_t *propList = bIsSelfClass ? NULL : class_copyPropertyList(cls, &propVarCount);/*属性列表*/ \ 147 | sharedVarCount = bIsSelfClass ? iVarCount : propVarCount; \ 148 | \ 149 | for (int i = 0; i < sharedVarCount; i++) { \ 150 | const char *varName = bIsSelfClass ? ivar_getName(*(ivarList + i)) : property_getName(*(propList + i)); \ 151 | NSString *key = [NSString stringWithUTF8String:varName]; \ 152 | /*valueForKey只能获取本类所有变量以及所有层级父类的属性,不包含任何父类的私有变量(会崩溃)*/ \ 153 | id varValue = [self valueForKey:key]; \ 154 | NSArray *filters = @[@"superclass", @"description", @"debugDescription", @"hash"]; \ 155 | if (varValue && [filters containsObject:key] == NO) { \ 156 | [copy setValue:varValue forKey:key]; \ 157 | } \ 158 | } \ 159 | free(ivarList); \ 160 | free(propList); \ 161 | cls = class_getSuperclass(cls); \ 162 | } \ 163 | return copy; \ 164 | } 165 | 166 | 167 | #define WZLSERIALIZE_DESCRIPTION() \ 168 | \ 169 | /* 用来打印本类的所有变量(成员变量+属性变量),所有层级父类的属性变量及其对应的值 */ \ 170 | - (NSString *)description \ 171 | { \ 172 | NSString *despStr = @""; \ 173 | Class cls = [self class]; \ 174 | while (cls != [NSObject class]) { \ 175 | /*判断是自身类还是父类*/ \ 176 | BOOL bIsSelfClass = (cls == [self class]); \ 177 | unsigned int iVarCount = 0; \ 178 | unsigned int propVarCount = 0; \ 179 | unsigned int sharedVarCount = 0; \ 180 | Ivar *ivarList = bIsSelfClass ? class_copyIvarList([cls class], &iVarCount) : NULL;/*变量列表,含属性以及私有变量*/ \ 181 | objc_property_t *propList = bIsSelfClass ? NULL : class_copyPropertyList(cls, &propVarCount);/*属性列表*/ \ 182 | sharedVarCount = bIsSelfClass ? iVarCount : propVarCount; \ 183 | \ 184 | for (int i = 0; i < sharedVarCount; i++) { \ 185 | const char *varName = bIsSelfClass ? ivar_getName(*(ivarList + i)) : property_getName(*(propList + i)); \ 186 | NSString *key = [NSString stringWithUTF8String:varName]; \ 187 | /*valueForKey只能获取本类所有变量以及所有层级父类的属性,不包含任何父类的私有变量(会崩溃)*/ \ 188 | id varValue = [self valueForKey:key]; \ 189 | NSArray *filters = @[@"superclass", @"description", @"debugDescription", @"hash"]; \ 190 | if (varValue && [filters containsObject:key] == NO) { \ 191 | despStr = [despStr stringByAppendingString:[NSString stringWithFormat:@"%@: %@\n", key, varValue]]; \ 192 | } \ 193 | } \ 194 | free(ivarList); \ 195 | free(propList); \ 196 | cls = class_getSuperclass(cls); \ 197 | } \ 198 | return despStr; \ 199 | } 200 | 201 | /* 封装归档keyedArchiver操作 */ 202 | #define WZLSERIALIZE_ARCHIVE(__objToBeArchived__, __key__, __filePath__) \ 203 | \ 204 | NSMutableData *data = [NSMutableData data]; \ 205 | NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; \ 206 | [archiver encodeObject:__objToBeArchived__ forKey:__key__]; \ 207 | [archiver finishEncoding]; \ 208 | [data writeToFile:__filePath__ atomically:YES] 209 | 210 | 211 | /* 封装反归档keyedUnarchiver操作 */ 212 | #define WZLSERIALIZE_UNARCHIVE(__objToStoreData__, __key__, __filePath__) \ 213 | NSMutableData *dedata = [NSMutableData dataWithContentsOfFile:__filePath__]; \ 214 | NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:dedata]; \ 215 | __objToStoreData__ = [unarchiver decodeObjectForKey:__key__]; \ 216 | [unarchiver finishDecoding] 217 | 218 | 219 | 220 | #endif 221 | --------------------------------------------------------------------------------