Objective-c Getting variable from another classe [

2019-09-01 08:49发布

Possible Duplicate:
Unable to access object of other class

I've been working on a little project just to increase my comprehension of the objective-c syntaxe and functionalities but I'm stuck in my own project and can't find the answer.

First I've got two classes and in my main one I have declare a object of my other class so I can use the functions from my other class. This is how I did it (I just wrote the thing that I thought were necessary:

.h of my main class
import myOtherClass

@interface PkmViewController : UIViewController

    @property Pokemon * nomDePokemon;

.m of my main class
import myOtherClass

@synthesize nomDePokemon;

int nbDef = [nomDePokemon calculerUnNombrePourLaDefense];

.h of my other class

@interface Pokemon : NSObject

  @property int defencePoints;

-(int)calculerUnNombrePourLaDefense;

.m of my other class

@synthesize defencePoints;

-(int)calculerUnNombrePourLaDefense
{
    int nombrerandom = arc4random() % 45;
    return nombrerandom;
}

When I put a breakpoint in my main class to see what number is taken from the function, it always gives me 0. And when I put this section in my other class it works but I want to keep it in another class to improve my skills. Someone can explain me how to do it please?

1条回答
We Are One
2楼-- · 2019-09-01 09:20

It sounds like you haven't actually assigned an instance of Pokemon to your nomDePokemon variable. This means that instead of a Pokemon object, the variable contains nil, which returns nil or 0 in response to any message you send it. To solve the problem, create a Pokemon.

查看更多
登录 后发表回答