与其他类共享对象?
我刚刚开始学习目标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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于“未找到属性名称”,我认为这是一个编译错误。您可能无法在
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
inGameViewController
.If
gameViewController
isnil
inGameViewController
then either you have not actually initialized it, orplayerModel
isnil
. Are you sure that you're checking it afterviewDidLoad
actually runs? This can be later than you think.