Jens Alfke committed 319441e

Recent changes for ARC compatibility, block support, etc.

Comments (0)

Files changed (6)



 // Apply a selector to each array element, returning an array of the results:
+// (See also -[NSArray my_map:], which is more general but requires block support)
 NSArray* $apply( NSArray *src, SEL selector, id defaultValue );
 NSArray* $applyKeyPath( NSArray *src, NSString *keyPath, id defaultValue );
 #define $true   ((NSNumber*)kCFBooleanTrue)
 #define $false  ((NSNumber*)kCFBooleanFalse)
+#define $null   [NSNull null]
 @interface NSObject (MYUtils)
 - (BOOL) my_containsObjectIdenticalTo: (id)object;
 - (NSArray*) my_arrayByApplyingSelector: (SEL)selector;
 - (NSArray*) my_arrayByApplyingSelector: (SEL)selector withObject: (id)object;
+- (NSArray*) my_map: (id (^)(id obj))block;
+- (NSArray*) my_filter: (int (^)(id obj))block;
 // Internals (don't use directly)
-struct _dictpair { id key; id value; };
+struct _dictpair { __unsafe_unretained id key; __unsafe_unretained id value; };
 NSDictionary* _dictof(const struct _dictpair*, size_t count);
 NSMutableDictionary* _mdictof(const struct _dictpair*, size_t count);
 NSValue* _box(const void *value, const char *encoding);


     return result;
+- (NSArray*) my_map: (id (^)(id obj))block {
+    NSMutableArray* mapped = [[NSMutableArray alloc] initWithCapacity: self.count];
+    for (id obj in self) {
+        obj = block(obj);
+        if (obj)
+            [mapped addObject: obj];
+    }
+    NSArray* result = [[mapped copy] autorelease];
+    [mapped release];
+    return result;
+- (NSArray*) my_filter: (int (^)(id obj))block {
+    NSMutableArray* filtered = [[NSMutableArray alloc] initWithCapacity: self.count];
+    for (id obj in self) {
+        if (block(obj))
+            [filtered addObject: obj];
+    }
+    NSArray* result = [[filtered copy] autorelease];
+    [filtered release];
+    return result;
 - (NSString*) my_compactDescription
     NSMutableString *desc = [NSMutableString stringWithCapacity: 100];
     if( _gShouldLog == -1 )
-    BOOL old = _gShouldLog;
+    BOOL old = _gShouldLog != 0;
     _gShouldLog = enable;
     return old;
         NSString *prefixColor = (prefix==kWarningPrefix) ?COLOR(91) :COLOR(93);
         NSString *msgColor = (prefix==kWarningPrefix) ?@"" :COLOR(0);
         NSString *finalMsg = [[NSString alloc] initWithFormat: @"%@%@| %@%@%@%@%@\n", 
-                              COLOR(30),timestamp,
+                              COLOR(36),timestamp,
         fputs([finalMsg UTF8String], stderr);
-    // If running in Xcode, drop into the debugger on a warning.
-    // You must check the "Run > Stop On Debugger()/DebugStr()" menu item to enable this.
-    Debugger();
     Test cases are disabled if the DEBUG macro is not defined (i.e. in a release build). */
 #if DEBUG
-#define TestCase(NAME)      __attribute__ ((section ("__TEXT, Tests"))) void Test_##NAME(void); \
+#define TestCase(NAME)      void Test_##NAME(void); \
                             struct TestCaseLink linkToTest##NAME = {&Test_##NAME,#NAME}; \
                             __attribute__((constructor)) static void registerTestCase##NAME() \
                                 { = gAllTestCases; gAllTestCases=&linkToTest##NAME; } \
 #define AssertNil(VAL)          AssertEq((VAL),nil)
-#define CAssertNil(VAL)         CAssertEq((VAL),nil)
+#define CAssertNil(VAL)         CAssertEq((VAL),(id)nil)  // ARC is picky about the type of nil here
+#define CAssertNull(VAL)        CAssertEq((VAL),NULL)
 #define AssertAbstractMethod()  _AssertAbstractMethodFailed(self,_cmd);
 #import "Test.h"
-#import "ExceptionUtils.h"
 #if DEBUG
+#import "ExceptionUtils.h"
 BOOL gRunningTestCase;
 struct TestCaseLink *gAllTestCases;
     } else
         message = [NSString stringWithUTF8String: condString];
+    Log(@"*** ASSERTION FAILED: %@ ... NOT!", message);
     if( rcvr )
         [[NSAssertionHandler currentHandler] handleFailureInMethod: (SEL)selOrFn
                                                             object: rcvr 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.