-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNSObject+Dejal.m
361 lines (264 loc) · 12.1 KB
/
NSObject+Dejal.m
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//
// NSObject+Dejal.m
// Dejal Open Source Categories
//
// Created by David Sinclair on Tue Sep 24 2002.
// Copyright (c) 2002-2015 Dejal Systems, LLC. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#import "NSObject+Dejal.h"
@implementation NSObject (Dejal)
// ----------------------------------------------------------------------------------------
#pragma mark - KEY VALUE CODING METHODS
// ----------------------------------------------------------------------------------------
/**
Returns the value for the key path, or the default value if that key path is invalid (instead of throwing an exception).
@param keyPath Path to the value.
@param defaultValue Value to use if the path is invalid or the value is NSNull; may be nil.
@return The value from the path, or the defaultValue.
@author DJS 2013-12.
@version DJS 2014-01: to return the default value if NSNull.
*/
- (id)dejal_valueForKeyPath:(NSString *)keyPath defaultValue:(id)defaultValue;
{
id result = nil;
@try
{
result = [self valueForKeyPath:keyPath];
if (!result || [result isEqual:[NSNull null]])
result = defaultValue;
}
@catch (NSException *exception)
{
result = defaultValue;
}
return result;
}
/**
Tries to get a value from the dictionary via the key. If a value is found, it is set in the receiver, using the same key, otherwise no change is made. Useful for setting properties from a dictionary.
@author DJS 2011-02.
*/
- (void)dejal_setValueForKey:(NSString *)key withDictionary:(NSDictionary *)dict;
{
id value = dict[key];
if (value)
[self setValue:value forKey:key];
}
/**
Tries to get a value from the dictionary via the first key. If there isn't one, tries the alternative key. If a value is found, it is set in the receiver, using the first key, otherwise no change is made. Useful for setting properties from a dictionary.
@author DJS 2011-02.
*/
- (void)dejal_setValueForKey:(NSString *)key orDictKey:(NSString *)altKey withDictionary:(NSDictionary *)dict;
{
id value = dict[key];
if (!value && altKey)
value = dict[altKey];
if (value)
[self setValue:value forKey:key];
}
/**
Tries to get a value from the dictionary via the first key. If there isn't one, tries the alternative keys until a value is found. If a value is found, it is set in the receiver, using the first key, otherwise no change is made. Useful for setting properties from a dictionary.
@author DJS 2011-02.
*/
- (void)dejal_setValueForKey:(NSString *)key orDictKeys:(NSArray *)altKeys withDictionary:(NSDictionary *)dict;
{
id value = dict[key];
if (!value && altKeys)
{
for (NSString *altKey in altKeys)
if (!value)
value = dict[altKey];
}
if (value)
[self setValue:value forKey:key];
}
/**
If there is a value for the specified key in the receiver, it is set in the dictionary, otherwise this does nothing.
@author DJS 2011-02.
*/
- (void)dejal_setValueInDictionary:(NSMutableDictionary *)dict forKey:(NSString *)key;
{
[self dejal_setValueInDictionary:dict forKey:key removeIfNil:NO];
}
/**
If there is a value for the specified key in the receiver, it is set in the dictionary. If there isn't a value, the key is optionally removed from the dictionary. Passing YES is equivalent to calling [dict setValue:[self valueForKey:key] forKey:key].
@author DJS 2011-02.
*/
- (void)dejal_setValueInDictionary:(NSMutableDictionary *)dict forKey:(NSString *)key removeIfNil:(BOOL)removeIfNil;
{
id value = [self valueForKey:key];
if (value)
dict[key] = value;
else if (removeIfNil)
[dict removeObjectForKey:key];
}
// ----------------------------------------------------------------------------------------
#pragma mark - IDENTITY METHODS
// ----------------------------------------------------------------------------------------
/**
Returns YES if the receiver is an instance of a subclass of aClass, but not an instance of aClass itself.
@author DJS 2004-01.
*/
- (BOOL)dejal_isReallySubclassOfClass:(Class)aClass
{
return [self isKindOfClass:aClass] && ![self isMemberOfClass:aClass];
}
/**
Like -isEqual, but returns YES if the receiver and the other object are equal when both interpreted as case-insensitive strings. So, for example, @"THIS" and @"This" are equivalent, and @"123" and [NSNumber numberWithInteger:123] are equivalent. If the other object is nil, returns NO; i.e. a non-nil receiver is not equivalent to a nil value (but, of course, sending this to a nil object will result in 0, which would be incorrect if the other object were also nil).
@author DJS 2005-03.
*/
- (BOOL)dejal_isEquivalent:(id)anObject
{
if (anObject)
return ([[self description] caseInsensitiveCompare:[anObject description]] == NSOrderedSame);
else
return NO;
}
/**
An alias of -isEquivalent:, for when the "To" makes more sense. For efficiency, the logic is duplicated.
@author DJS 2005-05.
*/
- (BOOL)dejal_isEquivalentTo:(id)anObject
{
if (anObject)
return ([[self description] caseInsensitiveCompare:[anObject description]] == NSOrderedSame);
else
return NO;
}
// ----------------------------------------------------------------------------------------
#pragma mark - PERFORM SELECTOR METHODS
// ----------------------------------------------------------------------------------------
/**
Similar to -performSelector:, but calls a method that returns a boolean value. Returns NO if the receiever doesn't respond to that selector.
@author DJS 2006-06.
*/
- (BOOL)dejal_performBoolSelector:(SEL)selector;
{
return [self dejal_performIntegerSelector:selector withObject:[NSNull null] withObject:[NSNull null]];
}
/**
Similar to -performSelector:withObject:, but calls a method that returns a boolean value. Returns NO if the receiever doesn't respond to that selector.
@author DJS 2006-06.
*/
- (BOOL)dejal_performBoolSelector:(SEL)selector withObject:(__unsafe_unretained id)object;
{
return [self dejal_performIntegerSelector:selector withObject:object withObject:[NSNull null]];
}
/**
Similar to -performSelector:withObject:withObject:, but calls a method that returns a boolean value. Returns NO if the receiever doesn't respond to that selector.
@author DJS 2006-06.
*/
- (BOOL)dejal_performBoolSelector:(SEL)selector withObject:(__unsafe_unretained id)object1 withObject:(__unsafe_unretained id)object2;
{
return [self dejal_performIntegerSelector:selector withObject:object1 withObject:object2];
}
/**
Similar to -performSelector:, but calls a method that returns an integer value. Returns zero if the receiever doesn't respond to that selector.
@author DJS 2004-04.
*/
- (NSInteger)dejal_performIntegerSelector:(SEL)selector
{
return [self dejal_performIntegerSelector:selector withObject:[NSNull null] withObject:[NSNull null]];
}
/**
Similar to -performSelector:withObject:, but calls a method that returns an integer value. Returns zero if the receiever doesn't respond to that selector.
@author DJS 2004-04.
*/
- (NSInteger)dejal_performIntegerSelector:(SEL)selector withObject:(__unsafe_unretained id)object
{
return [self dejal_performIntegerSelector:selector withObject:object withObject:[NSNull null]];
}
/**
Similar to -performSelector:withObject:withObject:, but calls a method that returns an integer value. Does nothing (and returns zero) if selector is nil or the receiver doesn't respond to that selector.
@author DJS 2004-04.
*/
- (NSInteger)dejal_performIntegerSelector:(SEL)selector withObject:(__unsafe_unretained id)object1 withObject:(__unsafe_unretained id)object2
{
if (!selector || ![self respondsToSelector:selector])
return 0;
NSMethodSignature *signature = [self methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
NSInteger result = 0;
[invocation setSelector:selector];
if (![object1 isKindOfClass:[NSNull class]])
[invocation setArgument:&object1 atIndex:2];
if (![object2 isKindOfClass:[NSNull class]])
[invocation setArgument:&object2 atIndex:3];
[invocation invokeWithTarget:self];
[invocation getReturnValue:&result];
return result;
}
/**
Similar to -performSelector:withObject:, but allows any number of arguments. The arguments are taken from the array, in order. The selector should expect the same number of arguments. It's okay for arguments to be nil, if the selector doesn't take any. Does nothing if selector is nil, or the number of arguments of the selector and array are different.
@author DJS 2007-03.
*/
- (void)dejal_performSelector:(SEL)selector withArguments:(NSArray *)arguments;
{
if (!selector)
return;
NSMethodSignature *signature = [self methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
// Exclude the hidden self and _cmd arguments:
if (([signature numberOfArguments] - 2) != [arguments count])
{
NSLog(@"performSelector:%@ withObjects:%@ has different number of arguments", NSStringFromSelector(selector), arguments); // log
return;
}
[invocation setSelector:selector];
for (NSUInteger i = 0; i < [arguments count]; i++)
{
__unsafe_unretained id object = arguments[i];
[invocation setArgument:&object atIndex:i + 2];
}
[invocation invokeWithTarget:self];
}
/**
Invokes the selector, passing each object in the array as a parameter. Not the same as -[NSArray makeObjectsPerformSelector:], as that makes the objects in the array perform the selector, instead of making the receiver object perform the selector. The selector must take a single argument.
@author DJS 2004-10.
@version DJS 2014-12: changed to add a workaround for a compiler warning.
*/
- (void)dejal_performSelector:(SEL)selector withEachObjectInArray:(NSArray *)array
{
id object;
for (object in array)
{
[self performSelector:selector withObject:object];
}
}
/**
Invokes the selector, passing each object in the dictionary as a parameter. The order invoked is not defined. See -performSelector:withEachObjectInArray: for more information.
@author DJS 2004-10.
*/
- (void)dejal_performSelector:(SEL)selector withEachObjectInDictionary:(NSDictionary *)dict
{
[self dejal_performSelector:selector withEachObjectInArray:[dict allValues]];
}
/**
Invokes the selector, passing each object in the set as a parameter. The order invoked is not defined. See -performSelector:withEachObjectInArray: for more information.
@author DJS 2004-10.
*/
- (void)dejal_performSelector:(SEL)selector withEachObjectInSet:(NSSet *)set
{
[self dejal_performSelector:selector withEachObjectInArray:[set allObjects]];
}
@end