Skip to content

Instantly share code, notes, and snippets.

@lanvige
Last active December 10, 2015 20:48
Show Gist options
  • Save lanvige/4490789 to your computer and use it in GitHub Desktop.
Save lanvige/4490789 to your computer and use it in GitHub Desktop.
#import <UIKit/UIKit.h>
@interface TestViewController : UIViewController <
UITableViewDelegate,
UITableViewDataSource>
{
// instance variable 实例变量,Refrence Count,在实现类里赋值,引用值怎么计算?因为它没有指定strong类型。
NSMutableArray *_listData;
UITableView *_tableView;
}
// 这里的属性只能用self.listData来使用,通过setters操作来控制其Ref Count。
// LLVM还是自动生成了一个_listData的,可以通过_listData直接访问赋值,但这样就不会操作其Ref Count。
// 但若是再显式定义Instance Var会有什么问题?(感觉应该是没问题的)属性会直接使用那个实例变量。也就是说多余,但没错?
@property (nonatomic, strong) NSMutableArray *listData;
@property (nonatomic, strong) IBOutlet UITableView *tableView;
@end
#import "TestViewController.h"
@interface TestViewController ()
@end
@implementation TestViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 水果官方文档里有句话:Don’t Use Accessor Methods in Initializer Methods and dealloc
// 因为 getter/setter 是类里的一种 "方法" 而 init, delloc 时,这个类的实例是 unknown state,所以不建议access 类中的任何 方法
// ARC后还适用吗?下面哪种方法推荐?有何区别?
// ARC后能直接用setter赋值,因为setter方法是在编译时期就生成好的方法。
// 因为属性声明为strong后,setters方法里会对Ref Count进入+1操作,但直接赋值也会Ref Count吗?
// _listData = '';时Ref count是不会发生变化的。
_listData = @[@"item1", @"item2"];
self.listData = @[@"item1", @"tem2"];
}
- (void)viewDidUnload
{
self.listData = nil;
}
- (void)dealloc
{
// ARC支持下的dealloc还是必须的吗?
self.listData = nil;
[super dealloc];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment