将NSString转换为NSInteger

时间:2015-07-07 13:02:10

标签: ios objective-c

我有

NSString *strAmount = @"10.00";
NSInteger totalAmount = totalAmount + [strAmount integerValue];

输出

  

totalAmount = 80

但我希望输出为totalAmount = 10

3 个答案:

答案 0 :(得分:1)

  1. 首先totalAmount不应该是指针类型。
  2. 变量“totalAmount”在其自己的初始化
  3. 中使用时未初始化

    替换你的代码
    NSString *strAmount = @"10.00";
    NSInteger totalAmount =  0;
    totalAmount = totalAmount + [strAmount integerValue];
     NSLog(@" %ld",(long)totalAmount);
    
      

    输出= 10

答案 1 :(得分:0)

为什么你做了指向int的指针?

NSInteger totalAmount 0;
totalAmount = totalAmount + [strAmount integerValue];

答案 2 :(得分:0)

NSInterger *total替换为NSInteger total或将NSInteger转换为int(基于C)

NSString *strAmount = @"10.00";

NSInteger totalAmount = 0;
totalAmount = totalAmount + [strAmount integerValue];