Objective c 如何访问另一个类中的实例变量

Objective c 如何访问另一个类中的实例变量,objective-c,instance-variables,Objective C,Instance Variables,我在class1中有一个实例变量,它在tableview中保存单元格的整数值,我想在另一个类中使用这个变量,例如class2,但我不知道使用了哪种语法???«如果可能,请使用成员变量 @interface MyClass : NSObject { int score; } @property (nonatomic, assign) int score; @end @implementation MyClass @synthesize score; @end //access My

我在class1中有一个实例变量,它在tableview中保存单元格的整数值,我想在另一个类中使用这个变量,例如class2,但我不知道使用了哪种语法???

«如果可能,请使用成员变量

@interface MyClass : NSObject  
{
    int score;
}
@property (nonatomic, assign) int score;
@end

@implementation MyClass
@synthesize score;
@end

//access
MyClass *object = [MyClass alloc] init];
object.score = 99;
«您也可以使用NSUserDefault

// Snippet used to save your highscore in the prefs.
int highScore  = yourGameScore;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highScore] forKey:@"HighScore"];
[[NSUserDefaults standardUserDefaults] synchronize];


// Snippet used to get your highscore from the prefs.
highScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"HighScore"] intValue ];

«如果可能,使用成员变量

@interface MyClass : NSObject  
{
    int score;
}
@property (nonatomic, assign) int score;
@end

@implementation MyClass
@synthesize score;
@end

//access
MyClass *object = [MyClass alloc] init];
object.score = 99;
«您也可以使用NSUserDefault

// Snippet used to save your highscore in the prefs.
int highScore  = yourGameScore;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highScore] forKey:@"HighScore"];
[[NSUserDefaults standardUserDefaults] synchronize];


// Snippet used to get your highscore from the prefs.
highScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"HighScore"] intValue ];

您可能想要使用属性。下面是一个例子:

类别1.h

@property int myValue
1.m班

self.myValue = 2
类别2.m

int v = instanceOfClass1.myValue

这里有一个关于使用属性的教程链接:

您可能想使用属性。下面是一个例子:

类别1.h

@property int myValue
1.m班

self.myValue = 2
类别2.m

int v = instanceOfClass1.myValue
以下是有关使用特性的教程的链接:

Google搜索了objective c实例变量并找到了本文。

Google搜索了objective c实例变量并找到了本文。

您可以这样做:

例如:您希望将字符串从Class1传递到Class2

首先在Class2中创建字符串,并将其定义为Class2.h中的属性:

在Class2.m中:

然后,例如,您希望在触摸Class1中的按钮时传递字符串。在其操作create IBAction中,将其与按钮的touchUpInside链接,您可以将其设置为获取第二个类的实例,例如:

Class2 *class2 = [[Class2 alloc] init];    
class2.classTwoString= classOneString;
我建议采用上述方法

使用NSUserDefaults-如果应用程序崩溃,可能会丢失存储的值。

您可以执行以下操作:

例如:您希望将字符串从Class1传递到Class2

首先在Class2中创建字符串,并将其定义为Class2.h中的属性:

在Class2.m中:

然后,例如,您希望在触摸Class1中的按钮时传递字符串。在其操作create IBAction中,将其与按钮的touchUpInside链接,您可以将其设置为获取第二个类的实例,例如:

Class2 *class2 = [[Class2 alloc] init];    
class2.classTwoString= classOneString;
我建议采用上述方法


使用NSUserDefaults-如果应用程序崩溃,可能会丢失存储的值。

obj->instance\u var=123@AnoopVaidya,你直接找到了你的答案,它又指向了另一条线索。我想知道什么是递归深度…@vikingosegundo:事实上,我旁边的答案是好的。就连我也联系上了,被录取了。还有一些受保护的中继站。如果一个保留周期形成:D@AnoopVaidya:您是指默认方法?这真是个糟糕的主意@AnoopVaidya,你直接找到了你的答案,它又指向了另一条线索。我想知道什么是递归深度…@vikingosegundo:事实上,我旁边的答案是好的。就连我也联系上了,被录取了。还有一些受保护的中继站。如果一个保留周期形成:D@AnoopVaidya:您是指默认方法?这真是个糟糕的主意。啊,我现在得到了-1:如果class1是第一个控制器,如果你要创建class2,那么class2的一个实例就不会或者更确切地说,不应该知道class1,因为它是父类。嗯,如果class1创建了class2,那么class1是父类,class2是子类,class1应该在class2上设置属性,而不是相反,正如您所示……问题没有指定范围或类之间的引用是如何进行的。Class2,即使它是子类,也可以通过委托模式轻松了解Class1。啊,我现在得到了-1:如果Class1是第一个控制器,如果你将创建Class2,那么Class2的实例就不会或者更确切地说,不应该知道Class1,因为它将是它的父类。IMHO,如果Class1因此创建了Class2,class1是父类,class2是子类,class1应该在class2上设置属性,而不是相反,正如您所示……问题没有指定范围或类之间的引用是如何进行的。即使是孩子,也可以使用委托模式轻松了解Class1。这是老生常谈,不再推荐/做了。。。相反,您应该在示例中创建并设置classA的属性。。。但是我没有投反对票。。。我只是回答问题,没什么了。这是老派的,不再推荐/做了。。。相反,您应该在示例中创建并设置classA的属性。。。但是我没有投反对票。。。我只是回答问题,没什么了。我在class1中拥有的家伙是UITableViewController类委托方法:此方法中的DidSelectRowatineXpath使用如下实例变量:j=indexPath。行+1,我想在另一个类中使用此值。我在class1中拥有的家伙是UITableViewController类委托方法:此方法中的DidSelectRowatineXpath使用像这样的实例变量:j=indepath.row+1,我想在另一个类中使用这个值