开发者

NSLabel: pointer arithmetic?

开发者 https://www.devze.com 2023-03-23 10:25 出处:网络
I allocated a number of labels and now I am trying to update the text in each one. However the I am not sure if the pointer arithmetic is cor开发者_StackOverflow社区rect or if I need to keep track of

I allocated a number of labels and now I am trying to update the text in each one. However the I am not sure if the pointer arithmetic is cor开发者_StackOverflow社区rect or if I need to keep track of the address in another variable array and reset label to it?? The app keeps dropping out after the NSLogs...

[lbl setFont:[UIFont systemFontOfSize:10]]; // <--- drops out here 

Code snippet:

for (int i = 0; i < 16; i++) {
    for (int j = 0; j < 12; j++) {
        NSLog(@"pointer arithmetic");
        NSLog(@"pointer: %d", lbl);

        lbl++; // <--------- pointer increment to write to next object

        NSLog(@"pointer increment: %d", lbl);

        [lbl setFont:[UIFont systemFontOfSize:10]];

        [lbl setText:[NSString stringWithFormat:@"%C", kk ]];

        kk++;
    }
}


Im not sure you can count on Objective-C to create objects with sequential memory addresses. In other words don't do this.

Just make some arrays to hold your labels and iterate through those. Then you dont have to manage memory addresses at all and the code is far easier to read and understand.

0

精彩评论

暂无评论...
验证码 换一张
取 消