NSObject(KSTExtensions)
@interface NSObject (KSTExtensions)
/**
Set and get an arbitrary object associated with the receiver. Convenience property to associate additional objects with the receiver.
*/
@property (strong,nonatomic,nullable) id KST_representedObject;
/**
Used in place of a respondsToSelector: check before calling a method that returns void. For example:
MyClass foo = ...;
if ([foo respondsToSelector:@selector(bar)]) {
[foo bar];
}
would become:
MyClass foo = ...;
[[foo KST_performIfResponds] bar];
*/
- (instancetype)KST_performIfResponds;
/**
Used in place of a respondsToSelector: check before calling a method that returns an id value. For example:
MyClass foo = ...;
if ([foo respondsToSelector:@selector(baz)]) {
id retval = [foo baz];
}
would become:
MyClass foo = ...;
id retval = [[foo KST_performOrReturn:nil] baz];
@param value The default return value that should be used if the receiver does not respond to the method
@return Either the return value from the receiver or the default return value
*/
- (instancetype)KST_performOrReturn:(nullable id)value;
/**
Used in place of a respondsToSelector: check before calling a method that returns a non-object value (struct, primitive, etc). For example:
MyClass foo = ...;
if ([foo respondsToSelector:@selector(boop)]) {
NSUInteger retval = [foo boop];
}
would become:
MyClass foo = ...;
NSUInteger retval = [[foo KST_performOrReturnValue:@0] boop];
@param value The default value that should be used if the receiver does not respond to the method
@return Either the return value from the receiver or the default return value
*/
- (instancetype)KST_performOrReturnValue:(nullable NSValue *)value;
/**
A method for mapping the properties of an NSObject and returning the contents as an NSDictionary
@param transformer An NSValueTransformer instance for converting the property names/dictionary keys
@param dateFormatter An NSDateFormatter to format NSDate properties
@param properties A set of property names to be excluded from the return dictionary
@return The dictionary representaiton of the object
*/
- (nullable NSDictionary *)KST_dictionaryWithValueTransformer:(nullable NSValueTransformer *)transformer dateFormatter:(nullable NSDateFormatter *)dateFormatter excludingProperties:(NSSet <NSString *> *)properties;
/**
A method for populating the properties of an NSObject with a JSON dictionary
@param dictionary The dictionary representation of the object to populate
@param dateFormatter An NSDateFormatter to format NSDate properties
@param transformer An NSValueTrasformer for converting JSON keys to property names
*/
- (void)KST_setPropertiesWithJSONDictionary:(NSDictionary <NSString *, id> *)dictionary dateFormatter:(nullable NSDateFormatter *)dateFormatter valueTransformer:(nullable NSValueTransformer *)transformer;
@end
Undocumented
-
Set and get an arbitrary object associated with the receiver. Convenience property to associate additional objects with the receiver.
Declaration
Objective-C
@property (nonatomic, strong, nullable) id KST_representedObject;
-
Used in place of a respondsToSelector: check before calling a method that returns void. For example:
MyClass foo = …;
if ([foo respondsToSelector:@selector(bar)]) { [foo bar]; }
would become:
MyClass foo = …;
[[foo KST_performIfResponds] bar];
Declaration
Objective-C
- (nonnull instancetype)KST_performIfResponds;
-
Used in place of a respondsToSelector: check before calling a method that returns an id value. For example:
MyClass foo = …;
if ([foo respondsToSelector:@selector(baz)]) { id retval = [foo baz]; }
would become:
MyClass foo = …; id retval = [[foo KST_performOrReturn:nil] baz];
Declaration
Objective-C
- (nonnull instancetype)KST_performOrReturn:(nullable id)value;
Parameters
value
The default return value that should be used if the receiver does not respond to the method
Return Value
Either the return value from the receiver or the default return value
-
Used in place of a respondsToSelector: check before calling a method that returns a non-object value (struct, primitive, etc). For example:
MyClass foo = …;
if ([foo respondsToSelector:@selector(boop)]) { NSUInteger retval = [foo boop]; }
would become:
MyClass foo = …; NSUInteger retval = [[foo KST_performOrReturnValue:@0] boop];
Declaration
Objective-C
- (nonnull instancetype)KST_performOrReturnValue:(nullable NSValue *)value;
Parameters
value
The default value that should be used if the receiver does not respond to the method
Return Value
Either the return value from the receiver or the default return value
-
A method for mapping the properties of an NSObject and returning the contents as an NSDictionary
Declaration
Objective-C
- (nullable NSDictionary *) KST_dictionaryWithValueTransformer: (nullable NSValueTransformer *)transformer dateFormatter:(nullable NSDateFormatter *)dateFormatter excludingProperties:(nonnull NSSet<NSString *> *)properties;
Parameters
transformer
An NSValueTransformer instance for converting the property names/dictionary keys
dateFormatter
An NSDateFormatter to format NSDate properties
properties
A set of property names to be excluded from the return dictionary
Return Value
The dictionary representaiton of the object
-
A method for populating the properties of an NSObject with a JSON dictionary
Declaration
Objective-C
- (void)KST_setPropertiesWithJSONDictionary: (nonnull NSDictionary<NSString *, id> *)dictionary dateFormatter: (nullable NSDateFormatter *)dateFormatter valueTransformer: (nullable NSValueTransformer *)transformer;
Parameters
dictionary
The dictionary representation of the object to populate
dateFormatter
An NSDateFormatter to format NSDate properties
transformer
An NSValueTrasformer for converting JSON keys to property names