0

I have an array:

SomeArray =[[NSMutableArray alloc]initWithObjects:[NSNumber numberWithFloat:50.0],[NSNumber numberWithFloat:120.0],[NSNumber numberWithFloat:200.0],nil];

When I retrieve it:

NSNumber* Target = [SomeArray objectAtIndex:0];

When I NSLog it:

NSLog(@"Target %d",Target);

it return something funky like

2013-06-26 01:47:58.940 KKK[1027:c07] Target 121016880

What is the proper way to do this?? I just need the number in the array to be used as float.

sooon
  • 4,718
  • 8
  • 63
  • 116
  • Use `%@` to log an object. `%d` is for `int`. – rmaddy Jun 25 '13 at 17:59
  • You can specify that entire array like this: `@[@50.0, @120.0, @200.0]`. It's a lot easier to read that way, and it saves us from having to scroll to see your code. – Caleb Jun 25 '13 at 18:10
  • When you typed in your call to `NSLog`, Xcode probably warned you: “Format specifies type 'int' but the argument has type 'NSNumber *'”. You should always pay close attention to compiler warnings. – rob mayoff Jun 25 '13 at 18:56

2 Answers2

4

You are retrieving a NSNumber, which is an object.

%d is for logging decimals, which is not your case.

Either you log it with

NSLog(@"Target %@", target);

or you convert it to a float and use %f

NSLog(@"Target %f", [target floatValue]);

And PLEASE don't use capitalized identifiers for variables!

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
1

You are asking for a decimal %d in the log and also a primitive type. NSNumber is an object that wraps a primitive type numbers. So you can do like that NSLog(@"target %f",[Target floatValue]) or NSLog(@"target %@",Target). With the first you are sending a message to the object to unwrap the float value with the latter you are asking for the object description that in this case is the number

Andrea
  • 26,120
  • 10
  • 85
  • 131
  • 1
    `%d` is not decimal, it is for `int`. – rmaddy Jun 25 '13 at 18:02
  • @rmaddy Wrong. It **is** decimal. `d` stands for "decimal". There are a number of ways one can print an integer. `x` is for hex, `o` is for octal, etc... Decimal means base-10, it's not necessarily a floating-point number. –  Jun 25 '13 at 18:04
  • %i is for int, %d is for decimal, but in general use you can mix them – Andrea Jun 25 '13 at 18:06
  • 1
    As far as I know there's no difference at all between `%i` and `%d`, but I'll double check just in case. – Gabriele Petronella Jun 25 '13 at 18:06
  • 1
    @Andrea When used with `printf()`, `%i` is an obsolescent synonim for `%d`, i. e. it does the same thing as `%d` does, but `%d` is preferred. –  Jun 25 '13 at 18:06
  • 5
    [Wikipedia says](http://en.wikipedia.org/wiki/Printf_format_string): *int as a signed decimal number. '%d' and '%i' are synonymous for output, but are different when used with scanf() for input (using %i will interpret a number as hexadecimal if it's preceded by 0x, and octal if it's preceded by 0.)* – Caleb Jun 25 '13 at 18:07
  • @H2CO3 Maybe `%d` stands for "decimal" but it is used for `int` variables. That is not wrong. – rmaddy Jun 25 '13 at 18:14
  • @rmaddy I think what H2CO3 means is that you can print the decimal representation of a float, using the `%d` format – Gabriele Petronella Jun 25 '13 at 18:50
  • @H2CO3 Since we're being pedantic: [C11](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) has a specific list of obsolescent features, and `%i` isn't one of them. – rob mayoff Jun 25 '13 at 18:51
  • @GabrielePetronella If you try to use a `float` value with `%d` you will likely get garbage. `%d` expects an `int` value. – rmaddy Jun 25 '13 at 18:53
  • @rmaddy I stand corrected – Gabriele Petronella Jun 25 '13 at 18:54