Objective-C Basic class related question, retaining the value of a specific object using a class fil

Posted by von steiner on Stack Overflow See other posts from Stack Overflow or by von steiner
Published on 2010-05-04T18:23:29Z Indexed on 2010/05/04 18:28 UTC
Read the original article Hit count: 252

Filed under:
|
|
|

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.

I have left a comment with the text "Here is the issue" below, referring to my question.

//  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];
}

Many thanks in advance, Kindly remember that my background is more related to Salmons breeding than to computer code, try and lower your answer to my level if it's all the same to you.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone-sdk