1. 判断该类包含protocol协议
2. 判断一个类是否有指定方法
3. 直接访问成员变量
@autoreleasepool {
Student *stu = [[Student alloc]init];
//Study是protocol
if([stu conformsToProtocol:@protocol(Study)]){
NSLog(@"have study!");
}
[stu release];
}
2. 判断一个类是否有指定方法
if([stu respondsToSelector:@selector(test)]){
NSLog(@"has method");
}
3. 直接访问成员变量
Student *stu = [[Student alloc] init];
//访问成员变量得getter和setter方法
stu.test = 10;
//直接访问成员变量
stu->test = 10;
本文介绍了Objective-C编程中三种实用技巧:如何判断一个类是否遵循特定协议、如何检查类实例是否实现指定方法以及如何直接访问成员变量。这些技巧对于Objective-C开发者来说非常有用。
751

被折叠的 条评论
为什么被折叠?



