Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone &引用;语句需要整数类型的表达式;switch语句和字符串数组出错_Iphone_Objective C - Fatal编程技术网

Iphone &引用;语句需要整数类型的表达式;switch语句和字符串数组出错

Iphone &引用;语句需要整数类型的表达式;switch语句和字符串数组出错,iphone,objective-c,Iphone,Objective C,试图使用数组,但它在switch语句的右边给出了“语句需要整数类型的表达式('id'无效)”。怎么了 NSArray count = [NSArray arrayWithObjects: @"1", @"2", @"3", @"4", @"5", @"6", @"7", nil]; switch ([count objectAtIndex: myIndex]) { case 1: NSLog(@"One"); break; case 2:

试图使用数组,但它在switch语句的右边给出了“语句需要整数类型的表达式('id'无效)”。怎么了

NSArray count = [NSArray arrayWithObjects: @"1", @"2", @"3", @"4", @"5", @"6", @"7", nil];    

switch ([count objectAtIndex: myIndex]) {
    case 1:
        NSLog(@"One");
        break;
    case 2:
        NSLog(@"Two");
        break;
    case 3:
        NSLog(@"Three");
        break;
    default:
        break;
}

您正在创建一个文本字符串数组,并在int上执行case语句。只能打开整数类型

问题是arrayWithObjects创建了一个NSObject派生对象的数组,您无法在对象(id)上进行切换

如果您想存储一个数字数组,那么一个选项是存储NSNumber对象,而不是依赖于存储字符串的脆弱性,您希望这些字符串是数字。这项工作:

NSArray *arr = [NSArray arrayWithObjects: [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil];

switch ([[arr objectAtIndex:1] intValue]) {
    case 1:
        NSLog(@"1");
        break;

    case 2:
        NSLog(@"2");
        break;

    default:
        break;
}
它输出:

2012-03-05 23:23:46.798 Craplet[82357:707] 2

[count objectAtIndex:][/code>返回一个Id(aka object),在您的特定情况下,它将是一个NSString,在任何一种情况下,它都不是您的case表达式所期望的int。您需要
[[count objectAtIndex:myIndex]intValue]
将NSString转换为整数。

您的数组对象是NSString,而不是int。你想要实现的更大的目标是什么

你可以:

NSString *str = [count objectAtIndex:myIndex];
if ([str isEqualToString:@"1"]) NSLog(@"One");
else if ... // etc
更好的是:

static NSString *one = @"1";
static NSString *two = @"2";
// etc

NSArray *count = [NSArray arrayWithObjects:one, two, ... nil];

NSString *str = [count objectAtIndex:myIndex];

if (str == one) NSLog(@"One"); // this works because now 'str' and 'one' are the same object
else if ... // etc

switch语句仅适用于整型。您的数组包含
NSString
对象。将从数组中获取的
NSString
转换为如下所示的整数:

NSArray count = [NSArray arrayWithObjects: @"1", @"2", @"3", @"4", @"5", @"6", @"7", nil];

NSString *obj = [count objectAtIndex: myIndex];
switch ([obj intValue]) {
    case 1:
        NSLog(@"One");
        break;
    case 2:
        NSLog(@"Two");
        break;
    case 3:
        NSLog(@"Three");
        break;
    default:
        break;
}

为什么不在阵列中存储NSNUMBER?将数字存储为字符串有点脆弱…@bryanmac这会更有意义…但NSNumber仍然是一个对象,因此您仍然必须提取整型值以在switch语句中使用。快速问题:如何创建全局数组,以便可以从每个方法访问它?“如何创建全局数组,以便可以从每个方法访问它?“这个问题充其量只能说明你真的需要退后一步,读一本关于objC开发的好书,最坏的情况是会导致你编写一些带有全局值的可怕代码。我认为你缺少基本的编程知识。。。我的建议是,从零开始,不要试图在一周内学会所有东西。另外,不要使用教程来学习、获取书籍或上课(如果可能的话)。我意识到另一个问题是我在-(void)viewdiload方法中启动了数组,但我无法从其他方法访问它。如何创建全局数组(通常是全局变量)?签出单例模式。您可以做的另一件事是委托(让一个视图为另一个视图提供一个委托以回调并获取数据)。取决于它是共享数据的视图还是您真的希望它成为单个全局对象。。。