使用StringWithFormat使用float和long

时间:2019-04-18 08:43:02

标签: objective-c ios7 stringwithformat

我创建了一个类,其中包含4个变量和4个字符串,这些变量和4个变量相同,但作为字符串,

在我声明的h文件中:

@property (assign) float num1;
@property (assign) float num2;
@property (assign) float num3;
@property (assign) long num4;

@property (nonatomic, assign) NSString *num1String;
@property (nonatomic, assign) NSString *num2String;
@property (nonatomic, assign) NSString *num3String;
@property (nonatomic, assign) NSString *num4String;

在init方法中,我传递了一个字典,该字典用于创建像这样的变量:

    num1 = [[dict objectForKey:@"num1"] floatValue];
    num2 = [[dict objectForKey:@"num2"] floatValue];
    num3 = [[dict objectForKey:@"num3"] floatValue];
    num4 = [[dict objectForKey:@"num4"] longValue];

    num1String = [NSString stringWithFormat: @"%.2f", num1];
    num2String = [NSString stringWithFormat: @"%.2f", num2];
    num3String = [NSString stringWithFormat: @"%.2f", num3];
    num4String = [NSString stringWithFormat:@"%ld",num4];

当我在这些行之后放置一个断点并检查字符串类型,并且前三个为NSString且num4String为NSMutableString时,它将连接起来!


当我尝试将此字符串传递给另一个变量时,实际上是我的问题

    NSString *copyOfNum4String = model.num4String;

而不是字符串

Printing description of copyOfNum4String:
(NSString *) copyOfNum4String = 0x0000600001cb0740

这使应用程序崩溃,因为我接下来要做的就是获取最后4个字符。

有人可以解释发生了什么,如何解决? (必须很长,因为该数字长为13个字符)


编辑: 这是值的字典打印:

[0] (null)  @"num1" : (long)2018420678770   
key NSTaggedPointerString * @"num1" 0xb79527f3826c0fa9
value   __NSCFNumber *  (long)2018420678770 0xa1c30c3a46fcec1d

0 个答案:

没有答案