我是 xCode 的新手。我正在使用 xCode 4.6,但我不明白 xcode 如何完全实例化对象。我认为如果您将对象声明为 .h 文件中的属性,它会自动分配并初始化它。我可以让我的代码工作的唯一方法是在属性文件上执行分配和初始化。我在下面包含了我的示例代码,但是谁能告诉我这是否是正确的方法?
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic) int age;
@end
#import "Person.h"
@implementation Person
@end
#import <UIKit/UIKit.h>
#import "Person.h"
@interface ViewController : UIViewController
@property (strong, nonatomic) Person *person;
@property (weak, nonatomic) IBOutlet UILabel *lblDisplay;
- (IBAction)btnChangeLabel:(id)sender;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_person = [[Person alloc]init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnChangeLabel:(id)sender {
[_person setName:@"Rich"];
[_person setAge:50];
_lblDisplay.text = [NSString stringWithFormat:@"%@ is %d years old.",_person.name,_person.age];
}
@end