注意一点:Foundation框架的类, 但凡是通过类工厂方法创建的对象都是加上autorelease了
Person.m
#import "Person.h"
@implementation Person
+ (instancetype)person
{
return [[[self alloc] init] autorelease];
}
- (instancetype)initWithAge:(int)age{
if (self = [super init]) {
_age = age;
}
return self;
}
+ (instancetype)personWithAge:(int)age
{
/*
Person *p = [[self alloc] init];
p.age = age;
return [p autorelease];
*/
return [[[self alloc] initWithAge:age] autorelease];
}
- (void)dealloc
{
NSLog(@"%s", __func__);
[super dealloc];
}
@end
main.m
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
/*
Person *p = [[[Person alloc] init] autorelease];
p.age = 10;
NSLog(@"age = %i", p.age);
Person *p1 = [[[Person alloc] init] autorelease];
Person *p2 = [[[Person alloc] init] autorelease];
*/
/*
Person *p = [Person person];
p.age = 10;
NSLog(@"age = %i", p.age);
// 注意: Foundation框架的类, 但凡是通过类工厂方法创建的对象都是autorelease的
[[NSString alloc] init];
[NSString string];
// [NSString alloc] initWithString:<#(NSString *)#>
// [NSString stringWithString:<#(NSString *)#>];
*/
/*
// Person *p = [[[Person alloc] initWithAge:10] autorelease];
// NSLog(@"age = %i", p.age);
Person *p = [Person personWithAge:10];
NSLog(@"age = %i", p.age);
*/
}
return 0;
}