Fail to read integers from C-Array

时间:2022-12-03 15:21:11

I created a NSObject-Class called "leveldesign". There I created an integer C-Array. Now I'm trying to read the numbers in my "ViewController.m" class. But if I NSLog() the integers I get numbers like 1245 or 2104.... But in my C-Array are only 0,1 and 2.. Thanks for your help!

我创建了一个名为“leveldesign”的NSObject类。在那里我创建了一个整数C数组。现在我正在尝试读取我的“ViewController.m”类中的数字。但是,如果我NSLog()整数我得到像1245或2104的数字....但在我的C数组只有0,1和2 ..感谢您的帮助!

Code "leveldesign.h"

extern int level[1][5];

@interface leveldesign : NSObject

@end

Code "leveldesign.m"

#import "leveldesign.h"

int level[1][5] = {

    {0,1,2,0,2}

};

@implementation leveldesign

@end

Code "ViewController.m"

#import "leveldesign.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
  [super viewDidLoad];

  NSLog(@"%i", level[1][1]);
  NSLog(@"%i", level[1][3]);

}

@end

2 个解决方案

#1


2  

Array indices start at 0.

数组索引从0开始。

change this:

NSLog(@"%i", level[1][1]);
NSLog(@"%i", level[1][3]);

to this:

NSLog(@"%i", level[0][0]);
NSLog(@"%i", level[0][2]);

#2


0  

Maybe you can try NSLog(@"%d", level[1][3]); with the 'd' format specifier instead of 'i'?

也许你可以尝试NSLog(@“%d”,等级[1] [3]);使用'd'格式说明符而不是'i'?

#1


2  

Array indices start at 0.

数组索引从0开始。

change this:

NSLog(@"%i", level[1][1]);
NSLog(@"%i", level[1][3]);

to this:

NSLog(@"%i", level[0][0]);
NSLog(@"%i", level[0][2]);

#2


0  

Maybe you can try NSLog(@"%d", level[1][3]); with the 'd' format specifier instead of 'i'?

也许你可以尝试NSLog(@“%d”,等级[1] [3]);使用'd'格式说明符而不是'i'?