Objective-C学习笔记-Category

Category可以为任何已有类添加或者替换方法,即使没有源代码,但是不能添加实例变量

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSString(TestClass)
-(int) getTestValue;
@end

NS_ASSUME_NONNULL_END
#import "TestClass.h"


@implementation NSString(TestClass)

- (int)getTestValue
{
    return 100;
}

@end
#import <Foundation/Foundation.h>
#import "TestClass.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *testString=@"hello";
        NSLog(@"test value is %d",[testString getTestValue]);
        
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Potato-Eater/p/9781426.html