Objective-C从另一个类获取变量 [英] Objective-c Getting variable from another classe
问题描述
可能重复:
无法访问其他类的对象
我一直在做一个小项目,只是为了提高我对Objective-C语法和功能的理解,但我陷于自己的项目中,找不到答案。
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;
}
当我在主类中放置一个断点以查看该函数取了什么数字时,它总是给我0。当我将此节放在另一个班级时,它可以工作,但我想把它放在另一个班上以提高我的技能。有人可以向我解释如何做吗?
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?
推荐答案
听起来您实际上没有将Pokemon实例分配给您 nomDePokemon
变量。这意味着该变量包含 nil
而不是Pokemon对象,该变量返回 nil
或 0
以响应您发送的任何消息。要解决该问题,请创建一个神奇宝贝。
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.
这篇关于Objective-C从另一个类获取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!