Posing - Objective C
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 method can skip this.
Objective C permits a class to wholly replace another class withing a program. The replacing class is said to "pose as" the target class.
For the versions that supported posing all message sent to the target class are instead received by the posing class.
NSObject contain the poseAsClass: method that enables us to replace the existing class is said above.
Restriction in Posing
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.
Objective C permits a class to wholly replace another class withing a program. The replacing class is said to "pose as" the target class.
For the versions that supported posing all message sent to the target class are instead received by the posing class.
NSObject contain the poseAsClass: method that enables us to replace the existing class is said above.
Restriction in Posing
- A class may only pose as one of its direct or indirect superclass.
- The posing class must not define any new instance variables that are absent from the target class (though it may define or override method)
- A posing class can call overridden method 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 *)stringByReplacingOccurencesOfString : (NSString *)targetWithString :
(NSString *)replacemant
{
NSLog(@"The target string is :%@", target);
NSLog(@"The replacement string is %@", replacement);
}
@end
int main()
{
[MyString poseAsClass : [NSString class]];
NSString *str = @"test";
[str stringByReplacingOccurencesOfString : @"a" withString :@"c"];
return 0;
}
@interface MyString : NSString
@end
@implementation MyString
-(NSString *)stringByReplacingOccurencesOfString : (NSString *)targetWithString :
(NSString *)replacemant
{
NSLog(@"The target string is :%@", target);
NSLog(@"The replacement string is %@", replacement);
}
@end
int main()
{
[MyString poseAsClass : [NSString class]];
NSString *str = @"test";
[str stringByReplacingOccurencesOfString : @"a" withString :@"c"];
return 0;
}
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.
No comments: