-1

For some reason when I make an NSMutableArray it won't add SKLabelNodes. I add them in my code, but when I ask for the count it gives me 0. Am I doing something wrong? Or is something that should work not working? I honestly have no idea

self.titleLabels = [[SKNode alloc]init];
self.titleLabels.position = CGPointMake(CGRectGetMidX(self.frame),  700);

self.titleLabelsArray = [[NSMutableArray alloc]initWithCapacity:5];

SKLabelNode *label1 = [SKLabelNode labelNodeWithFontNamed:@"Code-Pro-Demo"];
label1.text = @"0";
label1.fontSize = 90;
label1.fontColor = [SKColor colorWithRed:90/255.0f green:197/255.0f blue:198/255.0f alpha:1];
label1.position = CGPointMake(-150, 100);
label1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:label1.frame.size];
label1.physicsBody.dynamic = NO;

SKLabelNode *label2 = [SKLabelNode labelNodeWithFontNamed:@"Code-Pro-Demo"];
label2.text = @"1";
label2.fontSize = 90;
label2.fontColor = [SKColor colorWithRed:1 green:1 blue:1 alpha:1];
label2.position = CGPointMake(-30, 100);
label2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:label2.frame.size];
label2.physicsBody.dynamic = NO;

SKLabelNode *label3 = [SKLabelNode labelNodeWithFontNamed:@"Code-Pro-Demo"];
label3.text = @"2";
label3.fontSize = 90;
label3.fontColor = [SKColor colorWithRed:1 green:1 blue:1 alpha:1];
label3.position = CGPointMake(150, 100);
label3.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:label3.frame.size];
label3.physicsBody.dynamic = NO;

SKLabelNode *label4 = [SKLabelNode labelNodeWithFontNamed:@"Code-Pro-Demo"];
label4.text = @"3";
label4.fontSize = 90;
label4.fontColor = [SKColor colorWithRed:1 green:1 blue:1 alpha:1];
label4.position = CGPointMake(-150, 0);
label4.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:label4.frame.size];
label4.physicsBody.dynamic = NO;

SKLabelNode *label5 = [SKLabelNode labelNodeWithFontNamed:@"Code-Pro-Demo"];
label5.text = @"4";
label5.fontSize = 120;
label5.fontColor = [SKColor colorWithRed:228/255.0f green:70/255.0f blue:38/255.0f alpha:1];
label5.position = CGPointMake(10, -1);
label5.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:label5.frame.size];
label5.physicsBody.dynamic = NO;


[self.titleLabels addChild:label1];
[self.titleLabels addChild:label2];
[self.titleLabels addChild:label3];
[self.titleLabels addChild:label4];
[self.titleLabels addChild:label5];

[self addChild:self.titleLabels];

[self.titleLabelsArray addObject:label1];
[self.titleLabelsArray addObject:label2];
[self.titleLabelsArray addObject:label3];
[self.titleLabelsArray addObject:label4];
[self.titleLabelsArray addObject:label5];
NSLog(@"%i",self.titleLabelsArray.count);
Squid
  • 315
  • 1
  • 11
  • You are using "self" in your array titleLabelsArray and titleLabels. That means you have to make them class properties in your header file. Once you do that everything works fine. – sangony Mar 29 '15 at 12:59
  • Yeah, I'd done that already, my answer works :) – Squid Mar 29 '15 at 13:00

2 Answers2

0

As you noted in your own answer, in this case there is no need to add the 5 SKLabelNode(s) to a distinct array, infact you can iterate them using self.titleLabels.children.

However I tried to understand why your original code does not work as expected, infact your last line of code should indeed print the number 5.

Step 1

The compiler does not like this line

NSLog(@"%i",self.titleLabelsArray.count);

because values of type NSUInteger should not be used as format arguments. Use this instead:

NSLog(@"%lu",(unsigned long)self.titleLabelsArray.count);

Step 2

The code does work properly (and does print 5) IF the property titleLabelsArray is NOT declared as weak. E.g. this is a correct declaration:

@property NSMutableArray * titleLabelsArray;

And this is the output:

2015-03-29 16:09:31.919 MyProject[1231:695366] 5

On the other hand this is a wrong declaration for this case:

@property (weak) NSMutableArray * titleLabelsArray;

and now you get this output:

2015-03-29 16:10:59.713 MyProject[1275:699690] 0

So my question: how are you declaring the property titleLabelsArray?

Step 3

Finally I would suggest you to use an instance variable instead of a property when there is no need to make the variable public.

You can declare it this way

@implementation YourSuperCoolClass
{
    NSMutableArray * titleLabelsArray;
}

And then you access it without the self. prefix:

[titleLabelsArray addObject:label1];
[titleLabelsArray addObject:label2];
[titleLabelsArray addObject:label3];
[titleLabelsArray addObject:label4];
[titleLabelsArray addObject:label5];

NSLog(@"%lu",(unsigned long)titleLabelsArray.count);
Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
  • The advice to use ivars rather than properties needs some justification. With ARC, the difference is largely a matter of style. It's common practice to use properties declared in a class extension so that they're not part of the public interface. Why do you recommend against properties? – Caleb Mar 29 '15 at 14:42
  • Hello Caleb, you are right. You can achieve encapsulation also with a class extension inside the .m file. Personally I prefer the **@implementation { ... } block** because more essential. The topic has been discussed here http://stackoverflow.com/questions/13566862/where-to-put-ivars-in-modern-objective-c and there is a very good answer with 75 upvotes that encourages the **@implementation { ... }** approach. – Luca Angeletti Mar 29 '15 at 15:00
  • The question you linked is entirely about *where* to declare ivars, not about choosing between ivars and properties. Before ARC, properties were often preferred even for internal use because they simplified memory management. As I said above, with ARC, the choice is largely a matter of style. But if you're advising someone to use ivars instead of properties, you should provide some justification for that advice. – Caleb Mar 29 '15 at 15:16
  • Ops my fault! I though the **Option 3** was about properties as well :-) However, my answer to your question is: in this case I prefer instance variables simply because require less code and are more essential. – Luca Angeletti Mar 29 '15 at 15:32
-1

Use this:

self.titleLabels.children

It's a built in array for the childnodes

Squid
  • 315
  • 1
  • 11