oc 点语法
时间:2015-8-7 15:15 热度:2358° 评论:0 条

main.m
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Person *person = [[Person alloc] init];
person.age = 10; // 等效于[person setAge:10];
int age = person.age; // 等效于int age = [person age];
NSLog(@"age is %i", age);
[person release];
}
return 0;
}
person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject {
int _age;
}
- (void)setAge:(int)age; // 方法名是setAge:
- (int)age; // 方法名是age
// 方法名是setAge:andNo:
// - (void)setAge:(int)newAge andNo:(int)no;
@end
person.m
#import "Person.h"
@implementation Person
- (void)setAge:(int)age {
NSLog(@"调用了setAge方法:%i", age);
_age = age;
// 这是错误的写法,会导致死循环,无限调用set方法
// self.age = newAge;// [self setAge:newAge];
}
- (int)age {
NSLog(@"调用了age方法:%i", _age);
return _age;
}
@end


捐赠支持:如果觉得这篇文章对您有帮助,请“扫一扫”鼓励作者!
相关文章本文作者:沁雨寒 文章标题: oc 点语法
本文地址:https://blog.sxx1314.com/sdk-ios/oc-2.html
版权声明:若无注明,本文皆为“unix 软硬件 技术宅 ”原创,转载请保留文章出处。百度已收录
本文地址:https://blog.sxx1314.com/sdk-ios/oc-2.html
版权声明:若无注明,本文皆为“unix 软硬件 技术宅 ”原创,转载请保留文章出处。百度已收录















