如何在对象Objective-C中调用变量

时间:2022-09-07 08:26:26

i have 2 class: main.m second.m.

我有2个课:main.m second.m。

If i have in main.m

如果我在main.m

- (void)ok
{    
NSString *myString = @"OK!";
return myString;
}

How to call myString or "-(void) ok" function from second.m? Thanks.

如何从second.m调用myString或“ - (void)ok”函数?谢谢。

2 个解决方案

#1


7  

somewhere in second.m and assuming main.h/m defines a "Main" class

在second.m中的某处,假设main.h / m定义了一个“Main”类

Main *m = [[Main alloc] init];
NSString *mString = [m ok];
//Do Stuff
[m release];

However, based on the fact that your example returns void and not NSString, I think you have some basic knowledge to gain first. I'm guessing you don't understand Interfaces and Implementations yet, etc.

但是,基于您的示例返回void而不是NSString的事实,我认为您首先获得了一些基本知识。我猜你还不了解接口和实现等等。

I would suggest starting with a good basic tutorial.

我建议从一个很好的基础教程开始。

#2


0  

create an instance of main.m in second.m, then then using the created instance you can easily access its own variable. i support MaskPowell's answer.

在second.m中创建main.m的实例,然后使用创建的实例,您可以轻松访问自己的变量。我支持MaskPowell的答案。

#1


7  

somewhere in second.m and assuming main.h/m defines a "Main" class

在second.m中的某处,假设main.h / m定义了一个“Main”类

Main *m = [[Main alloc] init];
NSString *mString = [m ok];
//Do Stuff
[m release];

However, based on the fact that your example returns void and not NSString, I think you have some basic knowledge to gain first. I'm guessing you don't understand Interfaces and Implementations yet, etc.

但是,基于您的示例返回void而不是NSString的事实,我认为您首先获得了一些基本知识。我猜你还不了解接口和实现等等。

I would suggest starting with a good basic tutorial.

我建议从一个很好的基础教程开始。

#2


0  

create an instance of main.m in second.m, then then using the created instance you can easily access its own variable. i support MaskPowell's answer.

在second.m中创建main.m的实例,然后使用创建的实例,您可以轻松访问自己的变量。我支持MaskPowell的答案。