存储在NSMutableArray中时,SKLabelNode文本值不会更新

时间:2020-12-01 20:56:41

If I change the text attribute on a SKLabelNode after adding it as a child, the text will update in the view. But if I add it to an NSMutableArray, retrieve it, and then change the value, the text will not update in the view.

如果我在将SKLabelNode添加为子项后更改了该文本属性,则该文本将在视图中更新。但是,如果我将它添加到NSMutableArray,检索它,然后更改该值,文本将不会在视图中更新。

My best guess would be that either -addObject: or -objectAtIndex: of NSMutableArray are copying the objects instead of referencing them, but as you can see from their documentation: (-addObject:, -objectAtIndex:), neither one says anything about objects being copied.

我最好的猜测是,NSMutableArray的-addObject:或-objectAtIndex:正在复制对象而不是引用它们,但正如您可以从他们的文档中看到的那样:( - addObject:, - objectAtIndex :),没有人对对象说什么被复制。

Note: you may need to reload the pages for the -addObject: and -objectAtIndex: documentation before you're taken to the right spot on the page.

注意:在将您带到页面上的正确位置之前,可能需要重新加载-addObject:和-objectAtIndex:文档的页面。

What's going on? Why aren't my changes propagating?

这是怎么回事?为什么我的改变不会传播?

MyScene.h:

MyScene.h:

#import <SpriteKit/SpriteKit.h>

@interface MyScene : SKScene

@property(nonatomic, strong) NSMutableArray *labels;

@end

MyScene.m:

MyScene.m:

#import "MyScene.h"

@implementation MyScene

@synthesize labels;

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {

        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

        SKLabelNode *label = [SKLabelNode labelNodeWithFontNamed:@"Consolas"];

        label.text = @"Test String";
        label.fontSize = 65;
        label.position = CGPointMake(CGRectGetMidX(self.frame),
                                     CGRectGetMidY(self.frame));
        [labels addObject:label];
        [self addChild:label];

        // If I uncomment the following line, the text won't update.
        // label = [labels objectAtIndex:0];

        label.text = @"Test String: Reloaded";
    }
    return self;
}

@end

1 个解决方案

#1


2  

It appears that you haven't created the NSMutableArray. Sending messages to nil will silently fail and nothing will happen, without any hint as to why.

您似乎还没有创建NSMutableArray。将消息发送到nil将无声地失败,没有任何事情会发生,没有任何暗示为什么。

#1


2  

It appears that you haven't created the NSMutableArray. Sending messages to nil will silently fail and nothing will happen, without any hint as to why.

您似乎还没有创建NSMutableArray。将消息发送到nil将无声地失败,没有任何事情会发生,没有任何暗示为什么。