有人可以为我解释这个方法声明语法吗?在此函数中,返回UIPickerView(iPhone上的老虎机UI)的行数.根据我的理解,Method被称为' pickerView',并返回一个NSInteger.
它传入一个指向UIPickerview的指针,称为' pickerView'...首先,为什么该方法与参数名称相同?
接下来有一个名为component的NSInteger参数,它告诉我们计算行的哪个组件.决定哪个是在方法体内的逻辑.
什么是' numberOfRowsInComponent?它似乎描述了我们返回的值,但它位于参数的中间.
- (NSInteger) pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
if (component == kStateComponent)
return [self.states count];
return[self.zips count];
}
Run Code Online (Sandbox Code Playgroud) 我现在从几个来源(stackoverflow.com,cocoa-dev,文档,博客等)中听到,在init和dealloc方法中使用访问器和设置(foo,setFoo :)是"错误的".据我所知,如果你这样做,很可能会混淆其他正在观察财产的物体.(这里给出一个简单的例子)
但是,由于以下原因,我不得不说我不同意这种做法:
新的Objective-C运行时(iPhone上的那个和10.5中的64位运行时)允许您在不声明相应的ivar的情况下声明属性.例如,以下类将在10.5或iPhone(设备,而不是模拟器)上编译得很好:
@interface Foo : NSObject { }
@property (retain) id someObject;
@end
@implementation Foo
@synthesize someObject;
@end
Run Code Online (Sandbox Code Playgroud)
理解上面是一个完全有效的Objective-C类,假设我决定编写一个初始化程序,并且出于内存管理的目的,使用dealloc方法(因为在iPhone上没有GC).我读过有关初始化器和释放器的所有内容都会让我编写以下两种方法:
- (id) init {
if (self = [super init]) {
//initialize the value of someObject to nil
[self setSomeObject:nil];
}
return self;
}
- (void) dealloc {
//setting someObject to nil will release the previous value
[self setSomeObject:nil];
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)
但是,根据文件和流行的观点,这是"错误的".所以我的问题是这样的:
如果其中任何一个的答案是"你不能",那么在init和dealloc方法中使用访问器怎么可能不好?