Using StringWithFormat generates different results using float and long

73 Views Asked by At

I created a class that holds 4 variables and 4 strings which are the same 4 vars but as strings,

in the h file I declared:

@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;

in the init method, I passed a dictionary which I used to create the variables like this:

    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];

its get wired when I placed a break point after those line and checked the string types and the first 3 are NSString and num4String is NSMutableString !


my problem actually when I try to pass this string to another variable

    NSString *copyOfNum4String = model.num4String;

and instead of a string I get

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

which is crashing the app because the next thing I try to do is to get the last 4 characters.

can someone explain what is happening, and how to solve this ? (it has to be long because the number is 13 chars long )


EDIT: this is the dict print of the value :

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

There are 0 best solutions below