iPhone:将字符从数组提取到unichar时应用程序崩溃

iPhone:将字符从数组提取到unichar时应用程序崩溃,iphone,objective-c,ios5,Iphone,Objective C,Ios5,我有一个包含两个空白字符的数组,如下所示: shuffleArray = [[NSMutableArray alloc] initWithObjects:@"H",@"E",@"",@"",@"O", nil]; 现在我想将数组的值分配给unichar,以便进一步编码,如下所示: for(int i=0; i<[shuffleArray count]; i++) { NSString *temp = [shuffleArray objectAtIndex:i];

我有一个包含两个空白字符的数组,如下所示:

shuffleArray = [[NSMutableArray alloc] initWithObjects:@"H",@"E",@"",@"",@"O", nil];
现在我想将数组的值分配给unichar,以便进一步编码,如下所示:

for(int i=0; i<[shuffleArray count]; i++)
{
       NSString *temp = [shuffleArray objectAtIndex:i];
        NSLog(@"string:%@",temp);
        unichar c = [temp characterAtIndex:0];
}
我怎样才能解决这个问题

任何帮助都是值得的

Thanx提前..

试试这个,它会起作用的

  for( NSString *temp in shuffleArray )
  {
    NSLog(@"string:%@",temp);
    if (temp.length) {
      unichar c = [temp characterAtIndex:0];
    }
  }
试试这个,行得通

  for( NSString *temp in shuffleArray )
  {
    NSLog(@"string:%@",temp);
    if (temp.length) {
      unichar c = [temp characterAtIndex:0];
    }
  }

来自characterAtIndex:参考:

讨论

如果索引超出了 接受者

所以,在尝试访问字符串的字符之前,需要检查字符串是否为空

NSString *temp = [shuffleArray objectAtIndex:i];
NSLog(@"string:%@",temp);
unichar c = someInitialValue; // to indicate later that the string was empty may be equal 0 ?
if ([temp length] > 0) [temp characterAtIndex:0];

您的循环条件与上次迭代时一样错误(当i等于[shuffleArray count]时),您将从characterAtIndex:reference获得相同的NSRangeException异常

讨论

如果索引超出了 接受者

所以,在尝试访问字符串的字符之前,需要检查字符串是否为空

NSString *temp = [shuffleArray objectAtIndex:i];
NSLog(@"string:%@",temp);
unichar c = someInitialValue; // to indicate later that the string was empty may be equal 0 ?
if ([temp length] > 0) [temp characterAtIndex:0];

您的循环条件与上次迭代时一样错误(当i等于[Shuffarray count]时),您将得到相同的NSRangeException异常

NSMutableArray *shuffleArray = [[NSMutableArray alloc] initWithObjects:@"H",@"E",@"",@"",@"O", nil];

for(int i=0; i < [shuffleArray count]; i++) // < and not <=
{
    NSString *temp = [shuffleArray objectAtIndex:i];
    NSLog(@"string:%@",temp);
    if ([temp length] > 0)
    {
        unichar c = [temp characterAtIndex:0]; // Check if you can acces to the element before
        NSLog(@"%c", c);
    }
}
NSMutableArray*shufflarray=[[NSMutableArray alloc]initWithObjects:@“H”、“E”、“O”、“nil];
对于(int i=0;i<[shuffleArray计数];i++/<,而不是0)
{
unichar c=[temp characterAtIndex:0];//检查是否可以在之前访问该元素
NSLog(@“%c”,c);
}
}

我发现这段代码中有两个错误:

NSMutableArray *shuffleArray = [[NSMutableArray alloc] initWithObjects:@"H",@"E",@"",@"",@"O", nil];

for(int i=0; i < [shuffleArray count]; i++) // < and not <=
{
    NSString *temp = [shuffleArray objectAtIndex:i];
    NSLog(@"string:%@",temp);
    if ([temp length] > 0)
    {
        unichar c = [temp characterAtIndex:0]; // Check if you can acces to the element before
        NSLog(@"%c", c);
    }
}
NSMutableArray*shufflarray=[[NSMutableArray alloc]initWithObjects:@“H”、“E”、“O”、“nil];
对于(int i=0;i<[shuffleArray计数];i++/<,而不是0)
{
unichar c=[temp characterAtIndex:0];//检查是否可以在之前访问该元素
NSLog(@“%c”,c);
}
}

可能是因为空字符串上没有characterAtIndex 0吗?可能是因为空字符串上没有characterAtIndex 0吗?