Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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
Objective c 与其他类共享对象?_Objective C_Oop_Class_Share - Fatal编程技术网

Objective c 与其他类共享对象?

Objective c 与其他类共享对象?,objective-c,oop,class,share,Objective C,Oop,Class,Share,我刚开始学习目标c。我有一些小问题,希望你们能帮助我 我有一个ViewController,我叫它:GameViewController 此GameViewController将调用一个模型类“PlayerModel”(NSObject),此PlayerModel包含“PlayerProfile”对象(它是一个NSObject) GameViewController中的viewDidLoad将调用并启动模型类: playerModel = [[PlayerModel alloc] initWit

我刚开始学习目标c。我有一些小问题,希望你们能帮助我

我有一个ViewController,我叫它:GameViewController

此GameViewController将调用一个模型类“PlayerModel”(NSObject),此PlayerModel包含“PlayerProfile”对象(它是一个NSObject)

GameViewController中的viewDidLoad将调用并启动模型类:

playerModel = [[PlayerModel alloc] initWithInt:playerID];
然后,在我的PlayerModelInitWithInt方法中,我通过从在线数据库获取数据来填充PlayerProfile数据。我设法填充数据,但当我想在gameViewController中调用它时

// This is gameViewController.m
....
playerModel.playerProfile.name;
但我收到了错误消息:“在PlayerProfile类型的对象上找不到属性名”

我在gameViewController中NSLog playerProfile,它的“(null)”,而当我在playerModel中NSLog时,它有一些值。如何传递此值并使其通用,以便其他类可以使用(设置值)?

关于“未找到属性名”,我假定这是一个编译错误。您可能未能在
GameViewController
中导入PlayerProfile.h


如果
gameViewController
中的
gameViewController
nil
,则您可能尚未实际初始化它,或者
playerModel
nil
。确实要在
viewDidLoad
实际运行后检查它吗?这可能比您想象的要晚。

您是对的,我没有在gameviewcontroller中导入PlayerProfile.h。Bcuz我认为我导入了PlayerModel,PlayerModel导入了PlayerProfile,所以我认为我不需要导入PlayerProfile。谢谢,我今天学到了一些东西。无论如何,在我导入之后,没有编译错误,但仍然为零。我不确定我的方法是否正确,也许我的程序流程不正确。你通常做什么,或者这种情况下的常见做法是什么?我找到了答案。要共享该类,可以使用singleton。例如: