int stringWithFormat到NSString的返回值

Bil*_*ary 1 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;
}
Run Code Online (Sandbox Code Playgroud)

strage部分是,val给我一个返回值1879919632,这使我的比较失败.奇怪的是当我将NSString硬编码为val2 = @"22"时,比较成功,其中val2为:4240,它与以下相同:

NSLog(@"fontSizeNAme:%d",fontSizeName[i]);
Run Code Online (Sandbox Code Playgroud)

这是我在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
Run Code Online (Sandbox Code Playgroud)

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

Yuj*_*uji 6

在Objective-C,你不能只是一个比较int以及NSString*其中包含一个整数的字符串表示.要在两者之间进行转换,您需要这样做

NSString* s=@"11";
int i=[s intValue];  // i now contains 11
Run Code Online (Sandbox Code Playgroud)

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

NSLog(@"the current font Value1 is %i", val);
NSLog(@"the current font Value2 is %i",val2);
Run Code Online (Sandbox Code Playgroud)

是非法的,因为val而且val2NSString*.相反,你需要这样做

NSLog(@"the current font Value1 is %@", val);
NSLog(@"the current font Value2 is %@",val2);
Run Code Online (Sandbox Code Playgroud)

要么

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

你不能比较两个NSString*s ==.你需要使用[aString isEqualTo:anotherString].