将NSString转换为NSNumber会给出不同的值

时间:2014-06-11 02:55:26

标签: objective-c nsstring nsnumber valueconverter

我正在尝试将NSString转换为NSNumber,但我得到的价值与我的预期不同。

for(ClassA *item in appD.classArray)
{
    NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; // Method A
    [f setNumberStyle:NSNumberFormatterDecimalStyle];
    item.number = [f numberFromString:stringA];

    item.number = [[NSDecimalNumber alloc]initWithString:stringA]; // Method B

    item.number = @([stringA intValue]); // Method C

    item.number = [NSNumber numberWithUnsignedInt:[stringA integerValue]]; // Method D
    NSLog(@"string A %@",stringA);
    NSLog(@"item.number %@",item.number);
    [ClassA updateClassInfo:item];
}

控制台上的输出是:

string A 111775
item.number -19297

使用不同的方法产生相同的结果。转换后的字符串将更改为负数。

修改

P / S:我的应用程序中的字符串A每次运行时都会增加值。

NSNumber *number = [NSNumber new];
NSLog(@"stringA %@",stringA);

item.number = [NSNumber new];

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
number = [f numberFromString:stringA];
NSLog(@"number %@",number);
item.number = number;
NSLog(@"item.number %@",item.number);

number = [[NSDecimalNumber alloc]initWithString:stringA];
NSLog(@"number %@",number);
item.number = number;
NSLog(@"item.number %@",item.number);

number = @([stringA intValue]);
NSLog(@"number %@",number);
item.number = number;
NSLog(@"item.number %@",item.number);

number = [NSNumber numberWithInteger:[stringA integerValue]];
NSLog(@"number %@",number);
item.number = number;
NSLog(@"item.number %@",item.number);

控制台上的输出是:

2014-06-11 18:06:31.262 App[751:907] stringA 111796
2014-06-11 18:06:31.266 App[751:907] number 111796
2014-06-11 18:06:31.269 App[751:907] item.number -19276
2014-06-11 18:06:31.271 App[751:907] number 111796
2014-06-11 18:06:31.274 App[751:907] item.number -19276
2014-06-11 18:06:31.276 App[751:907] number 111796
2014-06-11 18:06:31.278 App[751:907] item.number -19276
2014-06-11 18:06:31.280 App[751:907] number 111796
2014-06-11 18:06:31.287 App[751:907] item.number -19276

2 个答案:

答案 0 :(得分:0)

您确定item.number属性是NSNumber吗?

此代码有效:

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
    @autoreleasepool {

            NSString *stringA = @"111775";
            NSNumber *number;

            NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; // Method A
            [f setNumberStyle:NSNumberFormatterDecimalStyle];
            number = [f numberFromString:stringA];
            NSLog(@"string A %@",stringA);
            NSLog(@"number %@", number);

            number = [[NSDecimalNumber alloc] initWithString:stringA]; // Method B
            NSLog(@"string A %@",stringA);
            NSLog(@"number %@", number);

            number = @([stringA intValue]); // Method C
            NSLog(@"string A %@",stringA);
            NSLog(@"number %@", number);

            number = [NSNumber numberWithInteger:[stringA integerValue]]; // Method D CHANGED, because unsigned int is not equal to integer
            NSLog(@"string A %@",stringA);
            NSLog(@"number %@", number);
    }
}

输出继电器:

  

2014-06-11 10:33:48.587无题[573:507]字符串A 111775

     

2014-06-11 10:33:48.588无标题[573:507]编号111775

     

2014-06-11 10:33:48.588无题[573:507]字符串A 111775

     

2014-06-11 10:33:48.588无标题[573:507]编号111775

     

2014-06-11 10:33:48.589无题[573:507]字符串A 111775

     

2014-06-11 10:33:48.589无标题[573:507]编号111775

     

2014-06-11 10:33:48.590无题[573:507]字符串A 111775

     

2014-06-11 10:33:48.590无标题[573:507]编号111775


如您所愿,使用类和属性进行同样的测试,输出正确

class .h

#import <Foundation/Foundation.h>

@interface ClassWithNumber : NSObject

@property (nonatomic, retain) NSNumber *item;

@end

class .m

#import "ClassWithNumber.h"

@implementation ClassWithNumber

@end

#import <Foundation/Foundation.h>
#import "ClassWithNumber.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        NSString *stringA = @"111775";
        ClassWithNumber *numberClass = [ClassWithNumber new];

        NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; // Method A
        [f setNumberStyle:NSNumberFormatterDecimalStyle];
        numberClass.item = [f numberFromString:stringA];
        NSLog(@"string A %@",stringA);
        NSLog(@"number %@", numberClass.item);

        numberClass.item = [[NSDecimalNumber alloc] initWithString:stringA]; // Method B
        NSLog(@"string A %@",stringA);
        NSLog(@"number %@", numberClass.item);

        numberClass.item = @([stringA intValue]); // Method C
        NSLog(@"string A %@",stringA);
        NSLog(@"number %@", numberClass.item);

        numberClass.item = [NSNumber numberWithInteger:[stringA integerValue]]; // Method D
        NSLog(@"string A %@",stringA);
        NSLog(@"number %@", numberClass.item);

    }
    return 0;
}

答案 1 :(得分:0)

如果我理解正确,您的item.number属性是Core Data托管对象中的托管属性。

请注意,每个Core Data属性都有一个类型:

Core Data Attribute Type

例如,如果属性类型为Integer 16,则属性设置器会将数字换行为16位。我想这就是这里发生的事情。

解决方案:将您的属性类型设置为足以保存您的值,例如Integer32