Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何从另一个类调用变量?_Iphone_Objective C_Class_Variables - Fatal编程技术网

Iphone 如何从另一个类调用变量?

Iphone 如何从另一个类调用变量?,iphone,objective-c,class,variables,Iphone,Objective C,Class,Variables,我有一个名为“Constants”的类,我正在其中存储一个字符串变量。此类包含我的应用程序中使用的几个全局变量 我希望能够引用这个类并在我的应用程序的其他视图中调用变量(称为profileId) 我环顾四周,发现了一些例子,但不知道如何做到这一点。目前我的设置是: 常数.h @interface Constants : UIViewController { NSString *profileId; } @property (nonatomic, retain) NSString *profil

我有一个名为“Constants”的类,我正在其中存储一个字符串变量。此类包含我的应用程序中使用的几个全局变量

我希望能够引用这个类并在我的应用程序的其他视图中调用变量(称为profileId)

我环顾四周,发现了一些例子,但不知道如何做到这一点。目前我的设置是:

常数.h

@interface Constants : UIViewController {
NSString *profileId;
}

@property (nonatomic, retain) NSString *profileId;

@end
常数.m

#import "Constants.h"

@implementation Constants

@synthesize profileId;

- (void)dealloc {
[profileId release];

[super dealloc];
}
我试图通过以下方式在新视图中调用变量profileId:

Constants *Constant;
    Constant = [[Constants alloc] init];
    Constant.profileId = userId;
NewView.h文件

@class Constants;
NSLog(@"ProfileId is:", [myConstants profileId]);
NewView.m文件

@class Constants;
NSLog(@"ProfileId is:", [myConstants profileId]);
有什么我遗漏的吗?即使我通过以下方式将值正确地存储在另一个函数中,它仍将显示为null:

Constants *Constant;
    Constant = [[Constants alloc] init];
    Constant.profileId = userId;

您缺少参数的%@:

NSLog(@“ProfileId为:%@,[myConstants ProfileId])

作为旁注,变量名应该以小写字母开头(常量,而不是常量)。您还可以在此处对属性使用点语法:myConstants.profileId


如果这不起作用,请发布用于分配值的代码(完整方法)。

我尝试了您的代码,结果值为空…要分配我的值,我会这样做:在.h文件中:#导入“Constants.h”@class Constants;在my.m文件中:NSString*responseString=[request responseString];NSString*userId=[responseString stringBetween:@''和:@'';NSLog(@“用户Id为:”);NSLog(@“%@”,userId);(userId显示正确的值)。常数*常数;常数=[[Constants alloc]init];Constant.profileId=userId;NSLog(@“常量profileId为:”);NSLog(@“%@”,常量.profileId);(Constant.profileId显示正确的值)。我通过以下代码解决了这个问题:您应该尝试查找并理解错误,因为您可能会一次又一次地执行类似的操作。但是如果没有代码(完整的方法),我们就无能为力。你可以随时编辑你的问题。