Objective-C Posing

Before starting about Posing in Objective-C, I would like to bring to your notice that Posing was declared deprecated in Mac OS X 10.5 and it's not available for use thereafter. So for those who are not concerned about these deprecated methods can skip this chapter.

Objective-C permits a class to wholly replace another class within a program. The replacing class is said to "pose as" the target class.

For the versions that supported posing, all messages sent to the target class are instead received by the posing class.

NSObject contains the poseAsClass: method that enables us to replace the existing class as said above.

Restrictions in Posing

  • A class may only pose as one of its direct or indirect superclasses.

  • The posing class must not define any new instance variables that are absent from the target class (though it may define or override methods).

  • The target class may not have received any messages prior to the posing.

  • A posing class can call overridden methods through super, thus incorporating the implementation of the target class.

  • A posing class can override methods defined in categories.

#import <Foundation/Foundation.h>

@interface MyString : NSString

@end

@implementation MyString

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
withString:(NSString *)replacement
{
   NSLog(@"The Target string is %@",target);
   NSLog(@"The Replacement string is %@",replacement);
}

@end

int main()
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   [MyString poseAsClass:[NSString class]];
   NSString *string = @"Test";
   [string stringByReplacingOccurrencesOfString:@"a" withString:@"c"];
   [pool drain];
   return 0;
}

Now when we compile and run the program in a older Mac OS X (V_10.5 or earlier), we will get the following result.

2013-09-22 21:23:46.829 Posing[372:303] The Target string is a
2013-09-22 21:23:46.830 Posing[372:303] The Replacement string is c

In the above example, we just polluted the original method with our implementation and this will get affected throughout all the NSString operations with the above method.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值