我有一些关于 Objective-C 的基本问题:
一、当我想访问一个对象的实例方法时,我总是必须先分配它,才能访问该方法吗?即使我已经在其他地方分配了它?
例如:
CustomPerson *person = [[CustomPerson alloc] init];
[person getName];
// can't I do something like this? (
[get_instance_of_already_somewhere_allocated_person getName];
二、作为初学者,我应该从启用 ARC 开始吗?
三、实例变量和@property-variables 有什么区别?我的意思是当我在我的方法中访问它们时,在我的实例中它们不是都是“全局的”吗?
例如:
// CustomPerson.h
@interface CustomPerson : NSObject {
UIImageView *_person;
}
@property (nonatomic, strong) UIImageView *img;
// CustomPerson.m
@implementation CustomPerson
@synthesize img = _img;
- (id)init
{
img.image = @"someimage.png";
_person.image = @"someimage.png";
[self setImageToSomeOtherImage:@"rustyimage.png"];
}
- (void)setImageToSomeOtherImage:(NSString *)img
{
// img.image before was "someimage.png"
img.image = img;
// _person.image before was "someimage.png"
_person.image = img;
}
@end