【问题标题】:Storing data in string into an integer array将字符串中的数据存储到整数数组中
【发布时间】:2011-04-17 04:27:49
【问题描述】:

我有一些数据到一个字符串中,我希望将这些数据存储在一个整数数组中......下面是代码。

int valMines[256];

// 'b' is NSString with 256 values in it.
for(int i=0; i<[b length]; i++){
valMines[i] =  [NSString stringWithFormat:@"%d", [b characterAtIndex:i]];

NSLog(@"valMines1 is %@", valMines[i]);
}

我收到警告,因为我的应用程序没有加载: 赋值从没有强制转换的指针生成整数。

请帮忙

【问题讨论】:

    标签: objective-c arrays ios


    【解决方案1】:

    您的 valMins 是一个整数数组,您正在将 NSString 分配给它。可能你看起来像这样:

    unichar valMines[256];  // make it unichar instead of int
    
    // 'b' is NSString with 256 values in it.
    for(int i=0; i<[b length]; i++){
        valMines[i] =  [b characterAtIndex:i]; // get and store the unichar
    
        NSLog(@"valMines1 is %d", valMines[i]);  // format specifier is %d, not %@
    }
    

    【讨论】:

    • 不能有任何其他方式将它存储在 int 数组中...问题是我有一个完整的大程序编码,现在我要更改它的问题...使它成为一个 unichar 数组而不是 int 给了我很多警告和错误。
    • unichar 只是无符号短整数的 typedef。如果该数组需要为其他地方的整数数组,则可以将其保留为 int。上面的代码不会有问题。只需将其设为 int valMins[256] 而不是 unichar valMins[256]。
    • 嗯,我试过了...但它在打印时给了我一些垃圾值...确定应该是 %d??
    • 你的字符串内容是什么?你得到了什么垃圾价值?
    • 我的字符串中的内容是 {-1-2-3-40134523} 任何整数 +ve 和 -ve 在 -4 到 +4 范围内的值...但是当我在控制台上打印valMine[0] 其打印 45,valMine[1] 38,对于 valMine[2] 48 等等...
    猜你喜欢
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多