Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add message passing examples.

  • Loading branch information...
commit 5cc66f11944bdb614262625740d8aeded41edb1e 1 parent bfff3fe
@mudphone authored
View
5 ObjcPlayground/ObjcPlayground/PDCViewController.h
@@ -11,7 +11,12 @@
@interface PDCViewController : UIViewController
- (void)playInUhPlayground;
+
- (NSString *)nameOfClass:(Class)class;
- (void)printMethodsOfClass:(Class)class;
+static const void *sendMessage(id receiver, const char *name);
+- (void)printMessageSend;
+- (void)printMethodForSelector;
+
@end
View
46 ObjcPlayground/ObjcPlayground/PDCViewController.m
@@ -40,10 +40,17 @@ - (void)playInUhPlayground
Class theClass = [NSObject class];
// Print name of class:
- if (YES) NSLog(@"Name of class is: %@", [self nameOfClass:theClass]);
+// NSLog(@"Name of class is: %@", [self nameOfClass:theClass]);
// Print methods of class:
- if (YES) [self printMethodsOfClass:theClass];
+// [self printMethodsOfClass:theClass];
+
+ // Use home-made message passing:
+// [self printMessageSend];
+
+ // Use Cocoa helper to pass message:
+ [self printMethodForSelector];
+
}
- (NSString *)nameOfClass:(Class)class
@@ -64,4 +71,39 @@ - (void)printMethodsOfClass:(Class)class
free(methods);
}
+#pragma mark - Message Sending
+
+static const void *sendMessage(id receiver, const char *name)
+{
+ SEL selector = sel_registerName(name);
+ IMP methodIMP = class_getMethodImplementation(object_getClass(receiver), selector);
+ return (__bridge void *)methodIMP(receiver, selector);
+}
+
+- (void)printMessageSend
+{
+ id object = [NSObject alloc];
+ sendMessage(object, "init");
+
+ NSString *message = @"description";
+ id description = (__bridge id)sendMessage(object, [message UTF8String]);
+ NSLog(@"Message: %@ on NSObject results in: %@", message, description);
+
+ object = @"/tmp/filename.txt";
+ message = @"lastPathComponent";
+ id filename = (__bridge id)sendMessage(object, [message UTF8String]);
+ NSLog(@"Message: %@ on NSObject results in: %@", message, filename);
+}
+
+- (void)printMethodForSelector
+{
+ NSArray *theArray = [NSArray arrayWithObjects:@"zero", @"one", @"two", nil];
+ SEL selector = @selector(objectAtIndex:);
+ IMP objectAtIndexMethod = [theArray methodForSelector:selector];
+
+ int chosenIndex = 1;
+ id result = objectAtIndexMethod(theArray, selector, chosenIndex);
+ NSLog(@"The Array is: %@ \nObject at index %i is: %@", theArray, chosenIndex, result);
+}
+
@end
Please sign in to comment.
Something went wrong with that request. Please try again.