English 中文(简体)
目标-C 基本类相关问题,保留使用类别档案的特定物体的价值
原标题:Objective-C Basic class related question, retaining the value of a specific object using a class file

Members, scholars, code gurus. My background is far from any computer programming thus my question may seems basic and somewhat trivial to you. Nevertheless it seems that I can t put my head around it. I have googled and searched for the answer, just to get myself confused even more. With that, I would kindly ask for a simple explanation suitable for a non technical person such as myself and for other alike arriving to this thread.

我在谈到我的问题时,对以下案文发表了意见。

//  character.h
#import <Foundation/Foundation.h>

@interface character : NSObject {
    NSString *name;
    int hitPoints;
    int armorClass;
}

@property (nonatomic,retain) NSString *name;
@property int hitPoints,armorClass;

-(void)giveCharacterInfo;

@end


//  character.m
#import "character.h"


@implementation character
@synthesize name,hitPoints,armorClass;

-(void)giveCharacterInfo{
    NSLog(@"name:%@ HP:%i AC:%i",name,hitPoints,armorClass);
}
@end



//  ClassAtLastViewController.h
#import <UIKit/UIKit.h>

@interface ClassAtLastViewController : UIViewController {

}

-(void)callAgain;

@end



//  ClassAtLastViewController.m
#import "ClassAtLastViewController.h"
#import "character.h"

@implementation ClassAtLastViewController


- (void)viewDidLoad {
    //[super viewDidLoad];
    character *player = [[character alloc]init];
    player.name = @"Minsc";
    player.hitPoints = 140;
    player.armorClass = 10;
    [player giveCharacterInfo];
    [player release];
        // Up until here, All peachy!
    [self performSelector:@selector(callAgain) withObject:nil afterDelay:2.0];
}

-(void)callAgain{
    // Here is the issue, I assume that since I init the player again I loss everything
    // Q1. I loss all the data I set above, where is it than? 
    // Q2. What is the proper way to implement this

    character *player = [[character alloc]init];
    [player giveCharacterInfo];
}

很多人预先感谢,但愿记住,我的背景与 Sal鱼的繁殖有关,而不是与计算机法典有关,如果你与你一样,试图把你的答复降低到我的水平。

最佳回答

我失去了上文所列的所有数据,数据在哪里?

释放。 通知:

character *player = [[character alloc]init];    // You create the character here...
// And then you initialize it...
// Then later...
[player release];                               // You release it.

即便是你没有释放,你仍然无法从<密码>进入。 ∗ UNEP/POPS/POPRC.2/1。 如果您希望可在all<>m>上查阅中的>方法。 页: 1

采取什么适当方式来执行这一规定

我不知道你的具体用法,但我想象你们会想像:

@interface ClassAtLastViewController : UIViewController {
    character *player;    // Make player an instance variable
}

-(void)callAgain;

@end

@implementation ClassAtLastViewController


- (void)viewDidLoad {
    //[super viewDidLoad];
    player = [[character alloc]init];    // Initialize the instance variable
    player.name = @"Minsc";
    player.hitPoints = 140;
    player.armorClass = 10;
    [player giveCharacterInfo];
    [self performSelector:@selector(callAgain) withObject:nil afterDelay:2.0];
}

-(void)callAgain{
    [player giveCharacterInfo];
}
问题回答

当你以你叫Again的职称为特点时,你实际上正在创造第二大特征。 尽管以Again为名创建的特性与你为DidLoad创造的特性相同,但它是一个完全不同的变量。 您认为DidLoad的特性,只在观察者Load的职能范围内存在。

而且,当你打电话[游戏者释放],看德·洛德时,你正在从记忆中删除这一品格。

我认为,你想要做的是,创造一种可以用于你整个班子的事例变量:

//  ClassAtLastViewController.h
#import <UIKit/UIKit.h>

@interface ClassAtLastViewController : UIViewController {
    character *player;
}

-(void)callAgain;

@end



//  ClassAtLastViewController.m
#import "ClassAtLastViewController.h"
#import "character.h"

@implementation ClassAtLastViewController


- (void)viewDidLoad {
    //[super viewDidLoad];
    player = [[character alloc]init];
    player.name = @"Minsc";
    player.hitPoints = 140;
    player.armorClass = 10;
    [player giveCharacterInfo];

    [self performSelector:@selector(callAgain) withObject:nil afterDelay:2.0];
}

-(void)callAgain{
    // Here is the issue, I assume that since I init the player again I loss everything
    // Q1. I loss all the data I set above, where is it than? 
    // Q2. What is the proper way to implement this

    [player giveCharacterInfo];
}




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。