Protocol和其他一些小方法使用

1. 判断该类包含protocol协议

@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;

猜你喜欢

转载自rayln.iteye.com/blog/1933971