https://github.com/343max/nsobject-expectation
inline object checking
https://github.com/343max/nsobject-expectation
Last synced: 16 days ago
JSON representation
inline object checking
- Host: GitHub
- URL: https://github.com/343max/nsobject-expectation
- Owner: 343max
- License: bsd-3-clause
- Created: 2013-05-17T08:09:40.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2013-06-08T16:49:27.000Z (almost 13 years ago)
- Last Synced: 2025-04-23T17:14:10.375Z (11 months ago)
- Language: Objective-C
- Size: 141 KB
- Stars: 15
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NSObject+Expectation
### Simple object verification that does not clutter your code.
We've all been there: you get some `NSDictionary` from some JSON deserialization, you expect it to be an `NSString`:
```
NSString *middleName = dictionaryFromJSON[@"middle_name"];
```
Everything seems fine until you discover that the middleName is optional and sometimes `null`. And suddenly your middleName contains `NSNull` instead of `NSString` and your App crashes because you tried to call `length` on your `NSNull` object. So you end up writing something like this:
```
NSString *middleName = dictionaryFromJSON[@"middle_name"];
if (![middleName isKindOfClass:[NSString class]) {
middleName = nil;
}
```
Lots of ugly code for a simple stupid check. Meet NSObject+Expectation:
```
NSString *middleName = [dictionaryFromJSON[@"middle_name"] nilUnlessIsKindOfClass:[NSString class]];
```
`[object nilUnlessIsKindOfClass:aClass]` returns `object` if it is indeed an object of `aClass`. Otherwise it returns `nil`.
But there is even a little bit more. Take this example:
```
NSInteger someIntegerValue = [dictionary[@"someIntegerValue"] integerValue];
```
We already learnt that we can make this crash proof by using our little helper:
```
NSInteger someIntegerValue = [[dictionary[@"someIntegerValue"] ifIsKindOfClass:[NSNumber class]] integerValue];
```
But actually we don't care if our object is an `NSNumber`. We are fine, as long a the object won't crash if we call `integerValue` on it. There is an selector for that:
```
NSInteger someIntegerValue = [[dictionary[@"someIntegerValue"] nilUnlessRespondsToSelector:@selector(integerValue)] integerValue];
```
Finally there is one last method so you are completely covered:
```
id aObject = [dictionary[@"fancyObject"] nilUnlessConformsToProtocol:@protocol(MyFancyProtocol)];
```
### optional Methods in Protocols
The `nilUnlessRespondsToSelector:` can become handy when calling optional methods of a protocol. How often did you write code like this:
```
if ([self.delegate respondsToSelector:@selector(shouldAbortDownloading)]) {
return [self.delegate shouldAbortDownloading];
} else {
return NO;
}
```
This might become a one-liner like this:
```
return [[self.delegate nilUnlessRespondsToSelector:@selector(shouldAbortDownloading)] shouldAbortDownloading];
```
### Default values
If you need a default value instead of nil we have something for you to:
```
NSInteger theAnswer = [[@(42) unless:alternativeAnswer respondsToSelector:@selector(integerValue)] integerValue];
```
When alternativeAnswer responds to the integerValue selector it will return alternativeAnswer otherwise @(42).
Ther methods `unless:isKindOfClass:` and `unless:conformsToProtocol` are available to.
License: BSD