1 #import <Foundation/Foundation.h>
2 #import <objc/runtime.h>
5 extern id objc_getProperty(id self, SEL _cmd, ptrdiff_t offset, BOOL atomic);
6 extern void objc_setProperty(id self, SEL _cmd, ptrdiff_t offset, id newValue, BOOL atomic, BOOL shouldCopy);
8 @interface Test : NSObject {
10 // _object is at the last optimized property offset
11 id _object __attribute__((aligned(64)));
13 @property(readonly) Class class;
14 @property(copy) NSString *value;
15 @property(assign) id object;
21 // _object is at the last optimized property offset
22 id _object __attribute__((aligned(64)));
27 // Question: why can't this code be automatically generated?
35 - (Class)class { return objc_getProperty(self, _cmd, 0, YES); }
37 - (NSString*)value { return (NSString*) objc_getProperty(self, _cmd, offsetof(TestDefs, _value), YES); }
38 - (void)setValue:(NSString*)inValue { objc_setProperty(self, _cmd, offsetof(TestDefs, _value), inValue, YES, YES); }
40 - (id)object { return objc_getProperty(self, _cmd, offsetof(TestDefs, _object), YES); }
41 - (void)setObject:(id)inObject { objc_setProperty(self, _cmd, offsetof(TestDefs, _object), inObject, YES, NO); }
43 - (NSString *)description {
44 return [NSString stringWithFormat:@"value = %@, object = %@", self.value, self.object];
50 NSAutoreleasePool *pool = [NSAutoreleasePool new];
52 NSMutableString *value = [NSMutableString stringWithUTF8String:"test"];
53 id object = [NSNumber numberWithInt:11];
54 Test *t = [[Test new] autorelease];
56 [value setString:@"yuck"]; // mutate the string.
57 testassert(t.value != value); // must copy, since it was mutable.
58 testassert([t.value isEqualToString:@"test"]);
60 Class testClass = [Test class];
62 testassert(testClass == cls);
64 testassert(testClass == cls);
69 // NSLog(@"t.object = %@, t.value = %@", t.object, t.value);
70 // NSLog(@"t.object = %@, t.value = %@", t.object, t.value); // second call will optimized getters.