0
我是xcode的新手。我使用xCode 4.6,我不明白xcode如何完全實例化對象。 我認爲,如果您聲明對象作爲.h文件中的屬性,它會自動分配並初始化它。唯一可以讓我的代碼工作的方法是在屬性文件上執行alloc和init。我在下面列出了我的示例代碼,但是誰能告訴我這是否是正確的方法?xCode 4.6對象分配和初始化
#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
非常感謝! – Rich 2013-04-28 20:13:05