【问题标题】:How to check what pointer is passed to a method?如何检查传递给方法的指针是什么?
【发布时间】:2012-02-19 14:33:52
【问题描述】:

希望任何人都可以帮助我。 我试图弄清楚在方法调用中传递了指向对象的指针。

所以(在 ​​cocos2d 环境中)应该是这样的:

- (void)loadCreature:(CCSprite*)creature
{
    if (/*here I want to check if the pointer is 'blue'*/) NSLog(@"the creature is blue")
    if (/*here I want to check if the pointer is 'red'*/) NSLog(@"the creature is red")
}

和实施:

CCSprite *blue;
CCSprite *red;

[self loadCreature:blue];
[self loadCreature:red];

提前致谢:)

  • 尤尔基

【问题讨论】:

    标签: objective-c pointers methods call


    【解决方案1】:

    除非有一些信息隐藏在蓝色和红色物体内部,可以区分它们,即:

    [blue setDistingusingInformation:@"This is blue"];

    [red setDistingusingInformation:@"This is red"];

    并且在调用 loadCreature 之前设置,那么在 loadCreature 方法内部没有任何方法可以告诉它传递了哪个 CCSprite 对象。

    唯一的选择是将红色和蓝色声明为全局变量,并在 loadCreature 内部比较它们的指针以确定哪个是哪个。但是这样做是非常错误的,如果我发现你这样做我会非常非常生气:D

    【讨论】:

    • 哈哈,我不希望你生气,所以我不会这样做。但我认为你为我指明了正确的方向。 CCSprite 似乎有一个标签属性,但这不起作用:blue.tag = 1; NSLog(@"blue.tag = %d", blue.tag);不知道为什么...
    • 啊,我现在明白为什么了……当 CCSprite 未初始化时,您还不能设置标签。太糟糕了,因为我需要在方法内部进行初始化。解决方法是在开始时使用空图片文件初始化精灵,然后设置标签,然后在方法内部使用新图片重新初始化。一点都不好看!但它有效......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多