与其他类共享对象?

发布于 2024-11-01 02:37:17 字数 725 浏览 4 评论 0原文

我刚刚开始学习目标c。我有一些小问题和疑问,希望大家能帮助我。

我有一个ViewController,我称之为:GameViewController。

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

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

playerModel = [[PlayerModel alloc] initWithInt:playerID];

然后,在我的 PlayerModel initWithInt 方法中,我通过从在线数据库获取数据来填充 PlayerProfile 数据。我设法填充数据,但是当我想在 gameViewController 中调用它时

// This is gameViewController.m
....
playerModel.playerProfile.name;

,但我收到此错误消息:“在 PlayerProfile 类型的对象上找不到属性名称”

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

I just started learning objective c. I have this little problems and questions, hope you guys could help me.

I have a ViewController, I called it: GameViewController.

This GameViewController, will call a model class, "PlayerModel"(NSObject) and this PlayerModel contains "PlayerProfile" object (its a NSObject).

The viewDidLoad in GameViewController will call and initiate model class:

playerModel = [[PlayerModel alloc] initWithInt:playerID];

Then, in my PlayerModel initWithInt method, I populate PlayerProfile data by fetching the data from online database. I manage to populate the data, but when I want to call it in gameViewController

// This is gameViewController.m
....
playerModel.playerProfile.name;

But i got this error message: "Property name not found on object of type PlayerProfile"

I NSLog the playerProfile in gameViewController, its "(null)", whereas when I NSLog in the playerModel, it has some values. How do I pass this values and make it universal, so that others classes can make use (set the value) of it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

定格我的天空 2024-11-08 02:37:17

关于“未找到属性名称”,我认为这是一个编译错误。您可能无法在 GameViewController#import PlayerProfile.h

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

Regarding the "Property name not found," I assume that's a compile error. You probably have failed to #import PlayerProfile.h in GameViewController.

If gameViewController is nil in GameViewController then either you have not actually initialized it, or playerModel is nil. Are you sure that you're checking it after viewDidLoad actually runs? This can be later than you think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文