gpt4 book ai didi

objective-c - 当全局对象传递给函数时,在 Objective C 中实例化它们。关于对象范围的困惑

转载 作者:行者123 更新时间:2023-11-30 15:55:31 25 4
gpt4 key购买 nike

我对 Objective-C 和 C 总体来说是新手。我一直在四处寻找,但找不到解决这个问题的方法。任何帮助将不胜感激。

我有以下全局变量

CCSprite* 背景图像;
CCSprite* 背景幽灵;
CCSprite* 地球仪;
CCSprite* Logo ;

在我的 init 中,我调用一个函数并将全局变量作为参数传递。

if(_ourDevice == iPad)
{

[self CustomCodeSetAssetForIpad:BackgroundImage ghost:BackgroundGhost TheGlobe:Globe AndTheLogo:Logo];


}

这是 CustomCodeSetAssetForIpad 的代码:

-(void) CustomCodeSetAssetForIpad:(CCSprite*) _Background ghost:(CCSprite*) _BackgroundGhosts TheGlobe:(CCSprite*)_Globes AndTheLogo:(CCSprite*) _Logos
{
_Background = [CCSprite spriteWithFile:@"1028-768-sunray.png"];
_BackgroundGhosts = [CCSprite spriteWithFile:@"1028-768-sunray.png"];
_Globes = [CCSprite spriteWithFile:@"BigGlobe.png"];
_Logos = [CCSprite spriteWithFile:@"DefaultLogo.png"];

[_BackgroundGhosts setAnchorPoint:CGPointMake(0.5, 0)];
[_BackgroundGhosts setScale:2];
[_BackgroundGhosts setOpacity:120];
//[BackgroundGhost setPosition: CGPointMake(BackgroundGhost.position.x, BackgroundGhost.position.y-500)];

[_BackgroundGhosts setPosition:CGPointMake([[CCDirector sharedDirector]winSize].width/2, -100)];

[_Globes setAnchorPoint:CGPointMake(0.5, 0.5)];
[_Globes setScale:0.7];
[_Globes setPosition:CGPointMake([[CCDirector sharedDirector]winSize].width/2, -260)];



[_Logos setPosition:CGPointMake([self CenterOfTheScreen].x, [[CCDirector sharedDirector]winSize].height-[[CCDirector sharedDirector]winSize].height*0.2)];
[_Logos setScale:0.05];


}

前几行实例化传递的全局变量。然而,当函数完成时,对这些对象的引用就会丢失。我认为当您将指针传递给函数时,当对象被实例化时,它将保留对实例化对象的引用。我在这里遗漏了什么吗?

最佳答案

啊... <b>classname</b> * 类型的变量是对该类实例的有效引用。所以在你的情况下,_Background是作为参数传递给函数的实例引用。如果您尝试从函数返回多个结果(通过指针),您的参数实际上应该是 <b>classname</b> ** 类型。 ,这是一个指向引用的指针。

因此调用代码将如下所示:

CCSprite * background = nil ;
CCSprite * ghosts = nil ;
CCSprite * globes = nil ;
CCSprite * logos = nil ;

[ self customCodeSetAssetForIpad:<b>&</b>background ghosts:<b>&</b>ghosts globes:<b>&</b>globes logos:<b>&</b>logos ] ;

你的方法如下所示:

-(void)customCodeSetAssetForIPad:(CCSprite**)background ghosts:(CCSprite**)backgroundhosts globe:globes logos:(CCSprite**)logos
{
*background = [CCSprite spriteWithFile:@"1028-768-sunray.png"];
// ... the rest of your code ...
}

另外,我冒昧地让你的方法名称和变量名称更像 Objective-C(方法和变量以小写字母开头)

编辑:我个人会这样构造它:

//
// World... global things go in here
//

@interface World

@property ( nonatomic, readonly, strong ) CCSprite * background ;

+(id)theWorld // accessor to get the global world object

@end

@implementation World
@synthesize CCSprite * background = _background ;

static World * __theWorld = nil ; // global variable to hold our shared global World instance

+(void)load
{
// when this class is loaded, create our global world object
__theWorld = [ [ [ self class ] alloc ] init ] ;
}

+(id)theWorld
{
return __theWorld ;
}

// return the background sprite, creating it if it hasn't be created yet
-(CCSprite*)background
{
if ( !_background) { _background = [ CCSprite spriteWithFile:[CCSprite spriteWithFile:@"1028-768-sunray.png"] ; }
return _background ;
}

@end

关于objective-c - 当全局对象传递给函数时,在 Objective C 中实例化它们。关于对象范围的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12086148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com