├── .gitignore ├── .travis.yml ├── LICENSE ├── NSDictionary-NilSafe.podspec ├── NSDictionary-NilSafe.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcshareddata │ └── xcschemes │ ├── NSDictionary-NilSafe.xcscheme │ └── Tests.xcscheme ├── NSDictionary-NilSafe ├── NSDictionary+NilSafe.h └── NSDictionary+NilSafe.m ├── README.md └── Tests ├── Info.plist └── Tests.m /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/objc,xcode,osx 3 | 4 | #!! ERROR: objc is undefined. Use list command to see defined gitignore types !!# 5 | 6 | ### Xcode ### 7 | # Xcode 8 | # 9 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 10 | 11 | ## Build generated 12 | build/ 13 | DerivedData/ 14 | 15 | ## Various settings 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | xcuserdata/ 25 | 26 | ## Other 27 | *.moved-aside 28 | *.xccheckout 29 | *.xcscmblueprint 30 | 31 | 32 | ### OSX ### 33 | *.DS_Store 34 | .AppleDouble 35 | .LSOverride 36 | 37 | # Icon must end with two \r 38 | Icon 39 | 40 | # Thumbnails 41 | ._* 42 | 43 | # Files that might appear in the root of a volume 44 | .DocumentRevisions-V100 45 | .fseventsd 46 | .Spotlight-V100 47 | .TemporaryItems 48 | .Trashes 49 | .VolumeIcon.icns 50 | .com.apple.timemachine.donotpresent 51 | 52 | # Directories potentially created on remote AFP share 53 | .AppleDB 54 | .AppleDesktop 55 | Network Trash Folder 56 | Temporary Items 57 | .apdisk 58 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | osx_image: xcode7.3 2 | language: objective-c 3 | script: 4 | - set -o pipefail && xcodebuild test -project NSDictionary-NilSafe.xcodeproj -scheme Tests -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO | xcpretty 5 | - pod lib lint 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Allen Hsu 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /NSDictionary-NilSafe.podspec: -------------------------------------------------------------------------------- 1 | # 2 | # Be sure to run `pod lib lint NSDictionary-NilSafe.podspec' to ensure this is a 3 | # valid spec before submitting. 4 | # 5 | # Any lines starting with a # are optional, but their use is encouraged 6 | # To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html 7 | # 8 | 9 | Pod::Spec.new do |s| 10 | s.name = 'NSDictionary-NilSafe' 11 | s.version = '0.2.0' 12 | s.summary = 'Nil safe dictionary' 13 | 14 | # This description is used to generate tags and improve search results. 15 | # * Think: What does it do? Why did you write it? What is the focus? 16 | # * Try to keep it short, snappy and to the point. 17 | # * Write the description between the DESC delimiters below. 18 | # * Finally, don't worry about the indent, CocoaPods strips it! 19 | 20 | s.description = <<-DESC 21 | With this category, now you can insert nil into NSDictionary, which will be replaced with NSNull, and NSNull is safer now, which can take any arbitrary method calls like nil. 22 | DESC 23 | 24 | s.homepage = 'https://github.com/allenhsu/NSDictionary-NilSafe' 25 | # s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2' 26 | s.license = { :type => 'MIT', :file => 'LICENSE' } 27 | s.author = { 'Allen Hsu' => 'allen@glowing.com' } 28 | s.source = { :git => 'https://github.com/allenhsu/NSDictionary-NilSafe.git', :tag => s.version.to_s } 29 | # s.social_media_url = 'https://twitter.com/' 30 | 31 | s.ios.deployment_target = '8.0' 32 | 33 | s.source_files = 'NSDictionary-NilSafe/*.{m,h}' 34 | 35 | # s.resource_bundles = { 36 | # 'NSDictionary-NilSafe' => ['NSDictionary-NilSafe/Assets/*.png'] 37 | # } 38 | 39 | # s.public_header_files = 'Pod/Classes/**/*.h' 40 | # s.frameworks = 'UIKit', 'MapKit' 41 | # s.dependency 'AFNetworking', '~> 2.3' 42 | end 43 | -------------------------------------------------------------------------------- /NSDictionary-NilSafe.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 9FB0E0E81D1AB8B600846FE9 /* NSDictionary+NilSafe.m in Sources */ = {isa = PBXBuildFile; fileRef = 9FB0E0E71D1AB8B600846FE9 /* NSDictionary+NilSafe.m */; }; 11 | 9FB0E0EA1D1AB91B00846FE9 /* NSDictionary+NilSafe.h in Headers */ = {isa = PBXBuildFile; fileRef = 9FB0E0E61D1AB8B600846FE9 /* NSDictionary+NilSafe.h */; }; 12 | 9FB0E0F21D1AB97700846FE9 /* Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9FB0E0F11D1AB97700846FE9 /* Tests.m */; }; 13 | 9FB0E0F41D1AB97700846FE9 /* libNSDictionary-NilSafe.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 9FB0E0DA1D1AB61E00846FE9 /* libNSDictionary-NilSafe.a */; }; 14 | /* End PBXBuildFile section */ 15 | 16 | /* Begin PBXContainerItemProxy section */ 17 | 9FB0E0F51D1AB97700846FE9 /* PBXContainerItemProxy */ = { 18 | isa = PBXContainerItemProxy; 19 | containerPortal = 9FB0E0D21D1AB61D00846FE9 /* Project object */; 20 | proxyType = 1; 21 | remoteGlobalIDString = 9FB0E0D91D1AB61E00846FE9; 22 | remoteInfo = "NSDictionary-NilSafe"; 23 | }; 24 | /* End PBXContainerItemProxy section */ 25 | 26 | /* Begin PBXCopyFilesBuildPhase section */ 27 | 9FB0E0D81D1AB61E00846FE9 /* CopyFiles */ = { 28 | isa = PBXCopyFilesBuildPhase; 29 | buildActionMask = 2147483647; 30 | dstPath = "include/$(PRODUCT_NAME)"; 31 | dstSubfolderSpec = 16; 32 | files = ( 33 | ); 34 | runOnlyForDeploymentPostprocessing = 0; 35 | }; 36 | /* End PBXCopyFilesBuildPhase section */ 37 | 38 | /* Begin PBXFileReference section */ 39 | 9FB0E0DA1D1AB61E00846FE9 /* libNSDictionary-NilSafe.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libNSDictionary-NilSafe.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 40 | 9FB0E0E61D1AB8B600846FE9 /* NSDictionary+NilSafe.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDictionary+NilSafe.h"; sourceTree = ""; }; 41 | 9FB0E0E71D1AB8B600846FE9 /* NSDictionary+NilSafe.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDictionary+NilSafe.m"; sourceTree = ""; }; 42 | 9FB0E0EF1D1AB97600846FE9 /* Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 43 | 9FB0E0F11D1AB97700846FE9 /* Tests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Tests.m; sourceTree = ""; }; 44 | 9FB0E0F31D1AB97700846FE9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 45 | /* End PBXFileReference section */ 46 | 47 | /* Begin PBXFrameworksBuildPhase section */ 48 | 9FB0E0D71D1AB61E00846FE9 /* Frameworks */ = { 49 | isa = PBXFrameworksBuildPhase; 50 | buildActionMask = 2147483647; 51 | files = ( 52 | ); 53 | runOnlyForDeploymentPostprocessing = 0; 54 | }; 55 | 9FB0E0EC1D1AB97600846FE9 /* Frameworks */ = { 56 | isa = PBXFrameworksBuildPhase; 57 | buildActionMask = 2147483647; 58 | files = ( 59 | 9FB0E0F41D1AB97700846FE9 /* libNSDictionary-NilSafe.a in Frameworks */, 60 | ); 61 | runOnlyForDeploymentPostprocessing = 0; 62 | }; 63 | /* End PBXFrameworksBuildPhase section */ 64 | 65 | /* Begin PBXGroup section */ 66 | 9FB0E0D11D1AB61D00846FE9 = { 67 | isa = PBXGroup; 68 | children = ( 69 | 9FB0E0DC1D1AB61E00846FE9 /* NSDictionary-NilSafe */, 70 | 9FB0E0F01D1AB97700846FE9 /* Tests */, 71 | 9FB0E0DB1D1AB61E00846FE9 /* Products */, 72 | ); 73 | sourceTree = ""; 74 | }; 75 | 9FB0E0DB1D1AB61E00846FE9 /* Products */ = { 76 | isa = PBXGroup; 77 | children = ( 78 | 9FB0E0DA1D1AB61E00846FE9 /* libNSDictionary-NilSafe.a */, 79 | 9FB0E0EF1D1AB97600846FE9 /* Tests.xctest */, 80 | ); 81 | name = Products; 82 | sourceTree = ""; 83 | }; 84 | 9FB0E0DC1D1AB61E00846FE9 /* NSDictionary-NilSafe */ = { 85 | isa = PBXGroup; 86 | children = ( 87 | 9FB0E0E61D1AB8B600846FE9 /* NSDictionary+NilSafe.h */, 88 | 9FB0E0E71D1AB8B600846FE9 /* NSDictionary+NilSafe.m */, 89 | ); 90 | path = "NSDictionary-NilSafe"; 91 | sourceTree = ""; 92 | }; 93 | 9FB0E0F01D1AB97700846FE9 /* Tests */ = { 94 | isa = PBXGroup; 95 | children = ( 96 | 9FB0E0F11D1AB97700846FE9 /* Tests.m */, 97 | 9FB0E0F31D1AB97700846FE9 /* Info.plist */, 98 | ); 99 | path = Tests; 100 | sourceTree = ""; 101 | }; 102 | /* End PBXGroup section */ 103 | 104 | /* Begin PBXHeadersBuildPhase section */ 105 | 9FB0E0E91D1AB90F00846FE9 /* Headers */ = { 106 | isa = PBXHeadersBuildPhase; 107 | buildActionMask = 2147483647; 108 | files = ( 109 | 9FB0E0EA1D1AB91B00846FE9 /* NSDictionary+NilSafe.h in Headers */, 110 | ); 111 | runOnlyForDeploymentPostprocessing = 0; 112 | }; 113 | /* End PBXHeadersBuildPhase section */ 114 | 115 | /* Begin PBXNativeTarget section */ 116 | 9FB0E0D91D1AB61E00846FE9 /* NSDictionary-NilSafe */ = { 117 | isa = PBXNativeTarget; 118 | buildConfigurationList = 9FB0E0E31D1AB61E00846FE9 /* Build configuration list for PBXNativeTarget "NSDictionary-NilSafe" */; 119 | buildPhases = ( 120 | 9FB0E0E91D1AB90F00846FE9 /* Headers */, 121 | 9FB0E0D61D1AB61E00846FE9 /* Sources */, 122 | 9FB0E0D71D1AB61E00846FE9 /* Frameworks */, 123 | 9FB0E0D81D1AB61E00846FE9 /* CopyFiles */, 124 | ); 125 | buildRules = ( 126 | ); 127 | dependencies = ( 128 | ); 129 | name = "NSDictionary-NilSafe"; 130 | productName = "NSDictionary-NilSafe"; 131 | productReference = 9FB0E0DA1D1AB61E00846FE9 /* libNSDictionary-NilSafe.a */; 132 | productType = "com.apple.product-type.library.static"; 133 | }; 134 | 9FB0E0EE1D1AB97600846FE9 /* Tests */ = { 135 | isa = PBXNativeTarget; 136 | buildConfigurationList = 9FB0E0F71D1AB97700846FE9 /* Build configuration list for PBXNativeTarget "Tests" */; 137 | buildPhases = ( 138 | 9FB0E0EB1D1AB97600846FE9 /* Sources */, 139 | 9FB0E0EC1D1AB97600846FE9 /* Frameworks */, 140 | 9FB0E0ED1D1AB97600846FE9 /* Resources */, 141 | ); 142 | buildRules = ( 143 | ); 144 | dependencies = ( 145 | 9FB0E0F61D1AB97700846FE9 /* PBXTargetDependency */, 146 | ); 147 | name = Tests; 148 | productName = Tests; 149 | productReference = 9FB0E0EF1D1AB97600846FE9 /* Tests.xctest */; 150 | productType = "com.apple.product-type.bundle.unit-test"; 151 | }; 152 | /* End PBXNativeTarget section */ 153 | 154 | /* Begin PBXProject section */ 155 | 9FB0E0D21D1AB61D00846FE9 /* Project object */ = { 156 | isa = PBXProject; 157 | attributes = { 158 | LastUpgradeCheck = 0730; 159 | ORGANIZATIONNAME = "Glow Inc."; 160 | TargetAttributes = { 161 | 9FB0E0D91D1AB61E00846FE9 = { 162 | CreatedOnToolsVersion = 7.3.1; 163 | }; 164 | 9FB0E0EE1D1AB97600846FE9 = { 165 | CreatedOnToolsVersion = 7.3.1; 166 | }; 167 | }; 168 | }; 169 | buildConfigurationList = 9FB0E0D51D1AB61E00846FE9 /* Build configuration list for PBXProject "NSDictionary-NilSafe" */; 170 | compatibilityVersion = "Xcode 3.2"; 171 | developmentRegion = English; 172 | hasScannedForEncodings = 0; 173 | knownRegions = ( 174 | en, 175 | ); 176 | mainGroup = 9FB0E0D11D1AB61D00846FE9; 177 | productRefGroup = 9FB0E0DB1D1AB61E00846FE9 /* Products */; 178 | projectDirPath = ""; 179 | projectRoot = ""; 180 | targets = ( 181 | 9FB0E0D91D1AB61E00846FE9 /* NSDictionary-NilSafe */, 182 | 9FB0E0EE1D1AB97600846FE9 /* Tests */, 183 | ); 184 | }; 185 | /* End PBXProject section */ 186 | 187 | /* Begin PBXResourcesBuildPhase section */ 188 | 9FB0E0ED1D1AB97600846FE9 /* Resources */ = { 189 | isa = PBXResourcesBuildPhase; 190 | buildActionMask = 2147483647; 191 | files = ( 192 | ); 193 | runOnlyForDeploymentPostprocessing = 0; 194 | }; 195 | /* End PBXResourcesBuildPhase section */ 196 | 197 | /* Begin PBXSourcesBuildPhase section */ 198 | 9FB0E0D61D1AB61E00846FE9 /* Sources */ = { 199 | isa = PBXSourcesBuildPhase; 200 | buildActionMask = 2147483647; 201 | files = ( 202 | 9FB0E0E81D1AB8B600846FE9 /* NSDictionary+NilSafe.m in Sources */, 203 | ); 204 | runOnlyForDeploymentPostprocessing = 0; 205 | }; 206 | 9FB0E0EB1D1AB97600846FE9 /* Sources */ = { 207 | isa = PBXSourcesBuildPhase; 208 | buildActionMask = 2147483647; 209 | files = ( 210 | 9FB0E0F21D1AB97700846FE9 /* Tests.m in Sources */, 211 | ); 212 | runOnlyForDeploymentPostprocessing = 0; 213 | }; 214 | /* End PBXSourcesBuildPhase section */ 215 | 216 | /* Begin PBXTargetDependency section */ 217 | 9FB0E0F61D1AB97700846FE9 /* PBXTargetDependency */ = { 218 | isa = PBXTargetDependency; 219 | target = 9FB0E0D91D1AB61E00846FE9 /* NSDictionary-NilSafe */; 220 | targetProxy = 9FB0E0F51D1AB97700846FE9 /* PBXContainerItemProxy */; 221 | }; 222 | /* End PBXTargetDependency section */ 223 | 224 | /* Begin XCBuildConfiguration section */ 225 | 9FB0E0E11D1AB61E00846FE9 /* Debug */ = { 226 | isa = XCBuildConfiguration; 227 | buildSettings = { 228 | ALWAYS_SEARCH_USER_PATHS = NO; 229 | CLANG_ANALYZER_NONNULL = YES; 230 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 231 | CLANG_CXX_LIBRARY = "libc++"; 232 | CLANG_ENABLE_MODULES = YES; 233 | CLANG_ENABLE_OBJC_ARC = YES; 234 | CLANG_WARN_BOOL_CONVERSION = YES; 235 | CLANG_WARN_CONSTANT_CONVERSION = YES; 236 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 237 | CLANG_WARN_EMPTY_BODY = YES; 238 | CLANG_WARN_ENUM_CONVERSION = YES; 239 | CLANG_WARN_INT_CONVERSION = YES; 240 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 241 | CLANG_WARN_UNREACHABLE_CODE = YES; 242 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 243 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 244 | COPY_PHASE_STRIP = NO; 245 | DEBUG_INFORMATION_FORMAT = dwarf; 246 | ENABLE_STRICT_OBJC_MSGSEND = YES; 247 | ENABLE_TESTABILITY = YES; 248 | GCC_C_LANGUAGE_STANDARD = gnu99; 249 | GCC_DYNAMIC_NO_PIC = NO; 250 | GCC_NO_COMMON_BLOCKS = YES; 251 | GCC_OPTIMIZATION_LEVEL = 0; 252 | GCC_PREPROCESSOR_DEFINITIONS = ( 253 | "DEBUG=1", 254 | "$(inherited)", 255 | ); 256 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 257 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 258 | GCC_WARN_UNDECLARED_SELECTOR = YES; 259 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 260 | GCC_WARN_UNUSED_FUNCTION = YES; 261 | GCC_WARN_UNUSED_VARIABLE = YES; 262 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 263 | MTL_ENABLE_DEBUG_INFO = YES; 264 | ONLY_ACTIVE_ARCH = YES; 265 | }; 266 | name = Debug; 267 | }; 268 | 9FB0E0E21D1AB61E00846FE9 /* Release */ = { 269 | isa = XCBuildConfiguration; 270 | buildSettings = { 271 | ALWAYS_SEARCH_USER_PATHS = NO; 272 | CLANG_ANALYZER_NONNULL = YES; 273 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 274 | CLANG_CXX_LIBRARY = "libc++"; 275 | CLANG_ENABLE_MODULES = YES; 276 | CLANG_ENABLE_OBJC_ARC = YES; 277 | CLANG_WARN_BOOL_CONVERSION = YES; 278 | CLANG_WARN_CONSTANT_CONVERSION = YES; 279 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 280 | CLANG_WARN_EMPTY_BODY = YES; 281 | CLANG_WARN_ENUM_CONVERSION = YES; 282 | CLANG_WARN_INT_CONVERSION = YES; 283 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 284 | CLANG_WARN_UNREACHABLE_CODE = YES; 285 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 286 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 287 | COPY_PHASE_STRIP = NO; 288 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 289 | ENABLE_NS_ASSERTIONS = NO; 290 | ENABLE_STRICT_OBJC_MSGSEND = YES; 291 | GCC_C_LANGUAGE_STANDARD = gnu99; 292 | GCC_NO_COMMON_BLOCKS = YES; 293 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 294 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 295 | GCC_WARN_UNDECLARED_SELECTOR = YES; 296 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 297 | GCC_WARN_UNUSED_FUNCTION = YES; 298 | GCC_WARN_UNUSED_VARIABLE = YES; 299 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 300 | MTL_ENABLE_DEBUG_INFO = NO; 301 | VALIDATE_PRODUCT = YES; 302 | }; 303 | name = Release; 304 | }; 305 | 9FB0E0E41D1AB61E00846FE9 /* Debug */ = { 306 | isa = XCBuildConfiguration; 307 | buildSettings = { 308 | OTHER_LDFLAGS = "-ObjC"; 309 | PRODUCT_NAME = "$(TARGET_NAME)"; 310 | SKIP_INSTALL = YES; 311 | }; 312 | name = Debug; 313 | }; 314 | 9FB0E0E51D1AB61E00846FE9 /* Release */ = { 315 | isa = XCBuildConfiguration; 316 | buildSettings = { 317 | OTHER_LDFLAGS = "-ObjC"; 318 | PRODUCT_NAME = "$(TARGET_NAME)"; 319 | SKIP_INSTALL = YES; 320 | }; 321 | name = Release; 322 | }; 323 | 9FB0E0F81D1AB97700846FE9 /* Debug */ = { 324 | isa = XCBuildConfiguration; 325 | buildSettings = { 326 | INFOPLIST_FILE = Tests/Info.plist; 327 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 328 | OTHER_LDFLAGS = "-ObjC"; 329 | PRODUCT_BUNDLE_IDENTIFIER = com.glowing.Tests; 330 | PRODUCT_NAME = "$(TARGET_NAME)"; 331 | }; 332 | name = Debug; 333 | }; 334 | 9FB0E0F91D1AB97700846FE9 /* Release */ = { 335 | isa = XCBuildConfiguration; 336 | buildSettings = { 337 | INFOPLIST_FILE = Tests/Info.plist; 338 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 339 | OTHER_LDFLAGS = "-ObjC"; 340 | PRODUCT_BUNDLE_IDENTIFIER = com.glowing.Tests; 341 | PRODUCT_NAME = "$(TARGET_NAME)"; 342 | }; 343 | name = Release; 344 | }; 345 | /* End XCBuildConfiguration section */ 346 | 347 | /* Begin XCConfigurationList section */ 348 | 9FB0E0D51D1AB61E00846FE9 /* Build configuration list for PBXProject "NSDictionary-NilSafe" */ = { 349 | isa = XCConfigurationList; 350 | buildConfigurations = ( 351 | 9FB0E0E11D1AB61E00846FE9 /* Debug */, 352 | 9FB0E0E21D1AB61E00846FE9 /* Release */, 353 | ); 354 | defaultConfigurationIsVisible = 0; 355 | defaultConfigurationName = Release; 356 | }; 357 | 9FB0E0E31D1AB61E00846FE9 /* Build configuration list for PBXNativeTarget "NSDictionary-NilSafe" */ = { 358 | isa = XCConfigurationList; 359 | buildConfigurations = ( 360 | 9FB0E0E41D1AB61E00846FE9 /* Debug */, 361 | 9FB0E0E51D1AB61E00846FE9 /* Release */, 362 | ); 363 | defaultConfigurationIsVisible = 0; 364 | defaultConfigurationName = Release; 365 | }; 366 | 9FB0E0F71D1AB97700846FE9 /* Build configuration list for PBXNativeTarget "Tests" */ = { 367 | isa = XCConfigurationList; 368 | buildConfigurations = ( 369 | 9FB0E0F81D1AB97700846FE9 /* Debug */, 370 | 9FB0E0F91D1AB97700846FE9 /* Release */, 371 | ); 372 | defaultConfigurationIsVisible = 0; 373 | }; 374 | /* End XCConfigurationList section */ 375 | }; 376 | rootObject = 9FB0E0D21D1AB61D00846FE9 /* Project object */; 377 | } 378 | -------------------------------------------------------------------------------- /NSDictionary-NilSafe.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /NSDictionary-NilSafe.xcodeproj/xcshareddata/xcschemes/NSDictionary-NilSafe.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 42 | 43 | 49 | 50 | 51 | 52 | 53 | 54 | 64 | 65 | 71 | 72 | 73 | 74 | 75 | 76 | 82 | 83 | 89 | 90 | 91 | 92 | 94 | 95 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /NSDictionary-NilSafe.xcodeproj/xcshareddata/xcschemes/Tests.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 14 | 15 | 17 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 39 | 40 | 41 | 42 | 48 | 49 | 51 | 52 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /NSDictionary-NilSafe/NSDictionary+NilSafe.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSDictionary+NilSafe.h 3 | // NSDictionary-NilSafe 4 | // 5 | // Created by Allen Hsu on 6/22/16. 6 | // Copyright © 2016 Glow Inc. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface NSDictionary (NilSafe) 12 | 13 | @end 14 | 15 | @interface NSMutableDictionary (NilSafe) 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /NSDictionary-NilSafe/NSDictionary+NilSafe.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSDictionary+NilSafe.m 3 | // NSDictionary-NilSafe 4 | // 5 | // Created by Allen Hsu on 6/22/16. 6 | // Copyright © 2016 Glow Inc. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "NSDictionary+NilSafe.h" 11 | 12 | @implementation NSObject (Swizzling) 13 | 14 | + (BOOL)gl_swizzleMethod:(SEL)origSel withMethod:(SEL)altSel { 15 | Method origMethod = class_getInstanceMethod(self, origSel); 16 | Method altMethod = class_getInstanceMethod(self, altSel); 17 | if (!origMethod || !altMethod) { 18 | return NO; 19 | } 20 | class_addMethod(self, 21 | origSel, 22 | class_getMethodImplementation(self, origSel), 23 | method_getTypeEncoding(origMethod)); 24 | class_addMethod(self, 25 | altSel, 26 | class_getMethodImplementation(self, altSel), 27 | method_getTypeEncoding(altMethod)); 28 | method_exchangeImplementations(class_getInstanceMethod(self, origSel), 29 | class_getInstanceMethod(self, altSel)); 30 | return YES; 31 | } 32 | 33 | + (BOOL)gl_swizzleClassMethod:(SEL)origSel withMethod:(SEL)altSel { 34 | return [object_getClass((id)self) gl_swizzleMethod:origSel withMethod:altSel]; 35 | } 36 | 37 | @end 38 | 39 | @implementation NSDictionary (NilSafe) 40 | 41 | + (void)load { 42 | static dispatch_once_t onceToken; 43 | dispatch_once(&onceToken, ^{ 44 | [self gl_swizzleMethod:@selector(initWithObjects:forKeys:count:) withMethod:@selector(gl_initWithObjects:forKeys:count:)]; 45 | [self gl_swizzleClassMethod:@selector(dictionaryWithObjects:forKeys:count:) withMethod:@selector(gl_dictionaryWithObjects:forKeys:count:)]; 46 | }); 47 | } 48 | 49 | + (instancetype)gl_dictionaryWithObjects:(const id [])objects forKeys:(const id [])keys count:(NSUInteger)cnt { 50 | id safeObjects[cnt]; 51 | id safeKeys[cnt]; 52 | NSUInteger j = 0; 53 | for (NSUInteger i = 0; i < cnt; i++) { 54 | id key = keys[i]; 55 | id obj = objects[i]; 56 | if (!key || !obj) { 57 | continue; 58 | } 59 | safeKeys[j] = key; 60 | safeObjects[j] = obj; 61 | j++; 62 | } 63 | return [self gl_dictionaryWithObjects:safeObjects forKeys:safeKeys count:j]; 64 | } 65 | 66 | - (instancetype)gl_initWithObjects:(const id [])objects forKeys:(const id [])keys count:(NSUInteger)cnt { 67 | id safeObjects[cnt]; 68 | id safeKeys[cnt]; 69 | NSUInteger j = 0; 70 | for (NSUInteger i = 0; i < cnt; i++) { 71 | id key = keys[i]; 72 | id obj = objects[i]; 73 | if (!key || !obj) { 74 | continue; 75 | } 76 | if (!obj) { 77 | obj = [NSNull null]; 78 | } 79 | safeKeys[j] = key; 80 | safeObjects[j] = obj; 81 | j++; 82 | } 83 | return [self gl_initWithObjects:safeObjects forKeys:safeKeys count:j]; 84 | } 85 | 86 | @end 87 | 88 | @implementation NSMutableDictionary (NilSafe) 89 | 90 | + (void)load { 91 | static dispatch_once_t onceToken; 92 | dispatch_once(&onceToken, ^{ 93 | Class class = NSClassFromString(@"__NSDictionaryM"); 94 | [class gl_swizzleMethod:@selector(setObject:forKey:) withMethod:@selector(gl_setObject:forKey:)]; 95 | [class gl_swizzleMethod:@selector(setObject:forKeyedSubscript:) withMethod:@selector(gl_setObject:forKeyedSubscript:)]; 96 | }); 97 | } 98 | 99 | - (void)gl_setObject:(id)anObject forKey:(id)aKey { 100 | if (!aKey || !anObject) { 101 | return; 102 | } 103 | [self gl_setObject:anObject forKey:aKey]; 104 | } 105 | 106 | - (void)gl_setObject:(id)obj forKeyedSubscript:(id)key { 107 | if (!key || !obj) { 108 | return; 109 | } 110 | [self gl_setObject:obj forKeyedSubscript:key]; 111 | } 112 | 113 | @end 114 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NSDictionary-NilSafe 2 | 3 | How we made NSDictionary nil safe at Glow, read more on [Glow Tech Blog](http://tech.glowing.com/cn/how-we-made-nsdictionary-nil-safe/). 4 | 5 | [![CI Status](http://img.shields.io/travis/allenhsu/NSDictionary-NilSafe.svg?style=flat)](https://travis-ci.org/allenhsu/NSDictionary-NilSafe) 6 | [![Version](https://img.shields.io/cocoapods/v/NSDictionary-NilSafe.svg?style=flat)](http://cocoapods.org/pods/NSDictionary-NilSafe) 7 | [![License](https://img.shields.io/cocoapods/l/NSDictionary-NilSafe.svg?style=flat)](http://cocoapods.org/pods/NSDictionary-NilSafe) 8 | [![Platform](https://img.shields.io/cocoapods/p/NSDictionary-NilSafe.svg?style=flat)](http://cocoapods.org/pods/NSDictionary-NilSafe) 9 | 10 | ## Example 11 | 12 | To run the example project, clone the repo, and run `pod install` from the Example directory first. 13 | 14 | ## Requirements 15 | 16 | ## Installation 17 | 18 | NSDictionary-NilSafe is available through [CocoaPods](http://cocoapods.org). To install 19 | it, simply add the following line to your Podfile: 20 | 21 | ```ruby 22 | pod "NSDictionary-NilSafe" 23 | ``` 24 | 25 | ## Author 26 | 27 | Allen Hsu, allen@glowing.com 28 | 29 | ## License 30 | 31 | NSDictionary-NilSafe is available under the MIT license. See the LICENSE file for more info. 32 | -------------------------------------------------------------------------------- /Tests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 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 | -------------------------------------------------------------------------------- /Tests/Tests.m: -------------------------------------------------------------------------------- 1 | // 2 | // Tests.m 3 | // Tests 4 | // 5 | // Created by Allen Hsu on 6/22/16. 6 | // Copyright © 2016 Glow Inc. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "NSDictionary+NilSafe.h" 11 | 12 | @interface Tests : XCTestCase 13 | 14 | @end 15 | 16 | @implementation Tests 17 | 18 | - (void)setUp { 19 | [super setUp]; 20 | } 21 | 22 | - (void)tearDown { 23 | [super tearDown]; 24 | } 25 | 26 | - (void)testLiteral { 27 | id nilVal = nil; 28 | id nilKey = nil; 29 | id nonNilKey = @"non-nil-key"; 30 | id nonNilVal = @"non-nil-val"; 31 | NSDictionary *dict = @{ 32 | nonNilKey: nilVal, 33 | nilKey: nonNilVal, 34 | }; 35 | XCTAssertEqualObjects([dict allKeys], @[]); 36 | XCTAssertNoThrow([dict objectForKey:nonNilKey]); 37 | id val = dict[nonNilKey]; 38 | XCTAssertNil(val); 39 | XCTAssertNoThrow([val length]); 40 | XCTAssertNoThrow([val count]); 41 | XCTAssertNoThrow([val anyObject]); 42 | XCTAssertNoThrow([val intValue]); 43 | XCTAssertNoThrow([val integerValue]); 44 | } 45 | 46 | - (void)testKeyedSubscript { 47 | NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 48 | id nilVal = nil; 49 | id nilKey = nil; 50 | id nonNilKey = @"non-nil-key"; 51 | id nonNilVal = @"non-nil-val"; 52 | dict[nonNilKey] = nilVal; 53 | dict[nilKey] = nonNilVal; 54 | XCTAssertEqualObjects([dict allKeys], @[]); 55 | XCTAssertNoThrow([dict objectForKey:nonNilKey]); 56 | } 57 | 58 | - (void)testSetObject { 59 | NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 60 | id nilVal = nil; 61 | id nilKey = nil; 62 | id nonNilKey = @"non-nil-key"; 63 | id nonNilVal = @"non-nil-val"; 64 | [dict setObject:nilVal forKey:nonNilKey]; 65 | [dict setObject:nonNilVal forKey:nilKey]; 66 | XCTAssertEqualObjects([dict allKeys], @[]); 67 | XCTAssertNoThrow([dict objectForKey:nonNilKey]); 68 | } 69 | 70 | - (void)testArchive { 71 | id nilVal = nil; 72 | id nilKey = nil; 73 | id nonNilKey = @"non-nil-key"; 74 | id nonNilVal = @"non-nil-val"; 75 | NSDictionary *dict = @{ 76 | nonNilKey: nilVal, 77 | nilKey: nonNilVal, 78 | }; 79 | NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dict]; 80 | NSDictionary *dict2 = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 81 | XCTAssertEqualObjects([dict2 allKeys], @[]); 82 | XCTAssertNoThrow([dict2 objectForKey:nonNilKey]); 83 | } 84 | 85 | - (void)testJSON { 86 | id nilVal = nil; 87 | id nilKey = nil; 88 | id nonNilKey = @"non-nil-key"; 89 | id nonNilVal = @"non-nil-val"; 90 | NSDictionary *dict = @{ 91 | nonNilKey: nilVal, 92 | nilKey: nonNilVal, 93 | }; 94 | NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL]; 95 | NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 96 | NSString *expectedString = @"{}"; 97 | XCTAssertEqualObjects(jsonString, expectedString); 98 | } 99 | 100 | @end 101 | --------------------------------------------------------------------------------