objective-c – Objective C – XCode不识别if语句之外的变量
发布时间:2020-12-14 17:16:27 所属栏目:百科 来源:网络整理
导读:尝试使用if语句设置sprite文件名,然后根据该字符串加载正确的文件.看起来我的变量范围存在问题,但我不知道它是什么. 这是我的代码: if ([[GameManager sharedGameManager] newHighScore] == TRUE) { NSString *highScoreLabelText = @"label-new-high-score
尝试使用if语句设置sprite文件名,然后根据该字符串加载正确的文件.看起来我的变量范围存在问题,但我不知道它是什么.
这是我的代码: if ([[GameManager sharedGameManager] newHighScore] == TRUE) { NSString *highScoreLabelText = @"label-new-high-score.png" } else { NSString *highScoreLabelText = @"label-high-score.png" } CCSprite *highScoreLabel = [CCSprite spriteWithSpriteFrameName:highScoreLabelText]; [highScoreLabel setAnchorPoint:ccp(0,0)]; [highScoreLabel setPosition:ccp(20,winSize.height * 0.575f)]; [self addChild:highScoreLabel]; XCode正在标记错误,说highScoreLabelText是未声明的标识符,因此不会编译应用程序.我是否需要在NSString中声明其他内容以使其余代码与变量一起使用? 解决方法
这是因为您在if的两个分支中声明了两个单独的内部范围变量.这两个变量都不在其范围之外可见,因此您收到错误.
您应该将声明移出if if,如下所示: NSString *highScoreLabelText; if ([[GameManager sharedGameManager] newHighScore] == TRUE) { highScoreLabelText = @"label-new-high-score.png" } else { highScoreLabelText = @"label-high-score.png" } 现在,在您的if语句之外可以看到highScoreLabelText. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |