2016-07-27 26 views
1

Ich habe NSMutableArray von void (^)() blockiert und ich möchte Debuggen, was in dieser Sammlung vor sich geht. Gerade jetzt, wenn ich versuche, es zu drucken, erhalte ich:Obj C: Wie fügt man einem Blockobjekt eine Beschreibung hinzu?

(lldb) po self.blockArray 
<__NSArrayM 0x1712f090>(
<__NSMallocBlock__: 0x19d64e30>, 
<__NSMallocBlock__: 0x19d60b50>, 
<__NSMallocBlock__: 0x19cbb2b0>, 
<__NSMallocBlock__: 0x19cbaa30>, 
<__NSMallocBlock__: 0x19c83100>, 
<__NSMallocBlock__: 0x170cbef0> 
) 

ich eine Beschreibung String in einen jeden Block hinzufügen möchten, und sehen es statt Adresse (Reihenfolge der Blöcke ist wichtig). Da Objektblöcke auch Objekte sind, habe ich eine Möglichkeit zu fällen. Kann jemand eine Idee teilen, wie es geht?

Antwort

0

Idee 1: Protokollieren Sie die Blöcke und Beschreibung, wenn sie erstellt und manuell identifiziert werden.

Idee 2: Nur zum Debuggen und Experimentieren verwenden Sie auf eigene Gefahr. Fügen Sie für jeden Block eine description-Methode zu NSBlock und ein zugehöriges Beschreibungsobjekt hinzu. Meine Test App:

@implementation AppDelegate 

static char kAssociatedObjectKey; 

typedef void (^MyBlockType)(void); 

- (NSString *)myDescription { 
    NSString *description = [super description]; 
    id object = objc_getAssociatedObject(self, &kAssociatedObjectKey); 
    if (object) 
     description = [description stringByAppendingFormat:@" %@", object]; 
    return description; 
} 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 
    Class blockClass = NSClassFromString(@"NSBlock"); 
    Method descriptionMethod = class_getInstanceMethod([self class], @selector(myDescription)); 
    BOOL didAddMethod = class_addMethod(blockClass, @selector(description), 
     method_getImplementation(descriptionMethod), method_getTypeEncoding(descriptionMethod)); 

    MyBlockType a = ^{}; 
    objc_setAssociatedObject(a, &kAssociatedObjectKey, @"block a", OBJC_ASSOCIATION_RETAIN_NONATOMIC); 
    MyBlockType b = ^{}; 
    objc_setAssociatedObject(b, &kAssociatedObjectKey, @"block b", OBJC_ASSOCIATION_RETAIN_NONATOMIC); 
    MyBlockType c = ^{}; 
    objc_setAssociatedObject(c, &kAssociatedObjectKey, @"block c", OBJC_ASSOCIATION_RETAIN_NONATOMIC); 
    NSArray *array = @[a, b, c]; 
    NSLog(@"%@", array); 
} 

@end 

PS. Vielleicht ist Idee 2 keine gute Idee. Ich kenne die Objective-C-Laufzeit nicht, aber ich glaube, ich verstehe, was ich mache.