访问多维数组中的对象,Obj-c

发布于 2024-12-01 19:32:47 字数 601 浏览 0 评论 0原文

我有一个包含其他数组的数组,但无法让它工作!我试图从子数组中提取字符串值,我可以首先访问第一个子数组,没有问题,但是当我尝试将标签更改为第二个数组中的对象时,我的程序崩溃了,我知道我应该如何处理这个问题吗?

int count = 0; // variable to access the required sub array 
NSArray* myArray; // array holding other arrays 
UILabel* mylabel; // label to display my string values from the array s

-(void) setLabel 
{

    NSArray* subArray = [myArray objectAtIndex: count];
    [myLabel setText:[subArray objectAtIndex:1]]; // this works fine
}

-(void) changeLabelToNextArray
{
    count ++
    [self setLabel]; //program crashes here when try to load label from next array 
} 

I have an array holding other arrays, but cant get this to work!! IM trying to pull string values from subArrays I can access first the first subarray no problem but then when i try changing the label to the object in second array my prog crashes anyidea on how i should approach this?

int count = 0; // variable to access the required sub array 
NSArray* myArray; // array holding other arrays 
UILabel* mylabel; // label to display my string values from the array s

-(void) setLabel 
{

    NSArray* subArray = [myArray objectAtIndex: count];
    [myLabel setText:[subArray objectAtIndex:1]]; // this works fine
}

-(void) changeLabelToNextArray
{
    count ++
    [self setLabel]; //program crashes here when try to load label from next array 
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千寻… 2024-12-08 19:32:47

你为什么要这样做?

[myLabel setText:[subArray objectAtIndex:1]];

则这将崩溃

  1. 如果subArray 在索引 1 处没有对象,
  2. 索引 1 处的对象无法设置为标签文本(即不能成为字符串)

我认为有关数组结构的更多信息将有助于给出更好的答案问题是什么。

编辑(基于下面的评论)
试试这个:

NSArray* myArray;   // Contains 5 subarrays, each containing 5 strings

UILabel* myLabel1;
UILabel* myLabel2;
UILabel* myLabel3;
UILabel* myLabel4;
UILabel* myLabel5;

int count = 0;      // Keeps track of which subarray we are on

- (void)setLabel
{
    NSArray* subArray = [myArray objectAtIndex:count];
    [myLabel1 setText: [subArray objectAtIndex:0]]
    [myLabel2 setText: [subArray objectAtIndex:1]]
    [myLabel3 setText: [subArray objectAtIndex:2]]
    [myLabel4 setText: [subArray objectAtIndex:3]]
    [myLabel5 setText: [subArray objectAtIndex:4]]
    count = (count + 1) % 5;    // Ensures that count is always 0 to 4
}

现在,每当您调用 setLabel 时,只要 5 个子数组中的每个子数组中确实有 5 个字符串,所有 5 个标签上的文本都应该发生变化。

Why are you doing this?

[myLabel setText:[subArray objectAtIndex:1]];

This will crash if

  1. subArray does not have an object at index 1
  2. the object at index 1 cannot be set as the label text (i.e cannot be made a string)

I think some more information on how your arrays are structured would help give a better answer to what the problem is.

EDIT (Based on comments below)
Try this:

NSArray* myArray;   // Contains 5 subarrays, each containing 5 strings

UILabel* myLabel1;
UILabel* myLabel2;
UILabel* myLabel3;
UILabel* myLabel4;
UILabel* myLabel5;

int count = 0;      // Keeps track of which subarray we are on

- (void)setLabel
{
    NSArray* subArray = [myArray objectAtIndex:count];
    [myLabel1 setText: [subArray objectAtIndex:0]]
    [myLabel2 setText: [subArray objectAtIndex:1]]
    [myLabel3 setText: [subArray objectAtIndex:2]]
    [myLabel4 setText: [subArray objectAtIndex:3]]
    [myLabel5 setText: [subArray objectAtIndex:4]]
    count = (count + 1) % 5;    // Ensures that count is always 0 to 4
}

Now whenever you call setLabel, the text should change on all 5 of your labels provided you do indeed have 5 strings in each of the 5 subarrays.

何其悲哀 2024-12-08 19:32:47

也许你可以尝试使用 C 数组:

id myArray[iMax][jMax];
subArray[i][j] = myArray[a][b];

Maybe you can try to use C array :

id myArray[iMax][jMax];
subArray[i][j] = myArray[a][b];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文