Objective-c Getting variable from another classe [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-25 07:50:06

问题


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:


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.



来源:https://stackoverflow.com/questions/12539183/objective-c-getting-variable-from-another-classe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!