int stringWithFormat到NSString的返回值

时间:2010-11-18 09:05:09

标签: objective-c nsstring

我有一个int currentFontSize,我想与存储在C数组中的NSString进行比较,这是代码。

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    static NSString* fontSizeName[] = 
    {
        @"14",
        @"18",
        @"22",
        @"26",
        @"30",
        @"34",
    };

    int fontSizeSegmentID;
    int currentFontSize = 22;

    NSString *val = [NSString stringWithFormat: @"%i", currentFontSize];
    NSString *val2 = @"22";
    NSLog(@"the current font size is %i", currentFontSize);
    NSLog(@"the current font Value1 is %i", val);
    NSLog(@"the current font Value2 is %i",val2);
    int i;
    for (i = 0; i < 6; i++) {

        NSLog(@"fontSizeNAme:%d",fontSizeName[i]);
        NSLog(@"val:%d",val);

            if (fontSizeName[i] == val) {
                NSLog(@"They are equal");
                fontSizeSegmentID = i;
                break;
            }

            NSLog(@"the fontSizeSegmentID is: %i" , fontSizeSegmentID);
        }

    [pool drain];
    return 0;
}

存储部分是,val给我一个返回值1879919632,这使我的比较失败。奇怪的是当我将NSString硬编码为val2 = @“22”时,比较成功,其中val2为:4240,它与:

相同
NSLog(@"fontSizeNAme:%d",fontSizeName[i]);

这是我在NSLog中的回归:

2010-11-18 16:56:52.784 testINT[26034:a0f] the current font size is 22
2010-11-18 16:56:52.786 testINT[26034:a0f] the current font Value1 is 1879919632
2010-11-18 16:56:52.787 testINT[26034:a0f] the current font Value2 is 4240
2010-11-18 16:56:52.788 testINT[26034:a0f] fontSizeNAme:4176
2010-11-18 16:56:52.788 testINT[26034:a0f] val:1879919632
2010-11-18 16:56:52.788 testINT[26034:a0f] the fontSizeSegmentID is: 32767
2010-11-18 16:56:52.789 testINT[26034:a0f] fontSizeNAme:4208
2010-11-18 16:56:52.789 testINT[26034:a0f] val:1879919632
2010-11-18 16:56:52.790 testINT[26034:a0f] the fontSizeSegmentID is: 32767
2010-11-18 16:56:52.790 testINT[26034:a0f] fontSizeNAme:4240
2010-11-18 16:56:52.790 testINT[26034:a0f] val:1879919632
2010-11-18 16:56:52.791 testINT[26034:a0f] the fontSizeSegmentID is: 32767
2010-11-18 16:56:52.791 testINT[26034:a0f] fontSizeNAme:4272
2010-11-18 16:56:52.791 testINT[26034:a0f] val:1879919632
2010-11-18 16:56:52.792 testINT[26034:a0f] the fontSizeSegmentID is: 32767
2010-11-18 16:56:52.792 testINT[26034:a0f] fontSizeNAme:4304
2010-11-18 16:56:52.792 testINT[26034:a0f] val:1879919632
2010-11-18 16:56:52.793 testINT[26034:a0f] the fontSizeSegmentID is: 32767
2010-11-18 16:56:52.793 testINT[26034:a0f] fontSizeNAme:4336
2010-11-18 16:56:52.794 testINT[26034:a0f] val:1879919632
2010-11-18 16:56:52.794 testINT[26034:a0f] the fontSizeSegmentID is: 32767

有人可以向我解释原因吗?为什么我转换@“22”NString和一个表单int返回一个不同的值?????谢谢!

2 个答案:

答案 0 :(得分:6)

在Objective-C中,您不能只比较包含整数字符串表示的intNSString*。要在两者之间进行转换,您需要执行

NSString* s=@"11";
int i=[s intValue];  // i now contains 11

此外,%中的NSLog规范指定您希望如何解释对象。您需要指定与对象类型匹配的正确%事物。否则它只会显示你的垃圾。所以,两者

NSLog(@"the current font Value1 is %i", val);
NSLog(@"the current font Value2 is %i",val2);

是非法的,因为valval2NSString*。相反,你需要做

NSLog(@"the current font Value1 is %@", val);
NSLog(@"the current font Value2 is %@",val2);

NSLog(@"the current font Value1 is %i", [val intValue]);
NSLog(@"the current font Value2 is %i",[val2 intValue]);

您无法按NSString*比较两个==。您需要使用[aString isEqualTo:anotherString]

答案 1 :(得分:1)

我同意@Yuji,在目标C中你不能尝试将字符串与整数进行比较。您最终可能会在某处运行到系统缓冲区中。或者,如果您有调试方法,则可能会获得out of range exception