Frage

I'm a newbie in XCode programming. I have following code:

CGSize firstSize = CGSizeMake(1.0,1.0);

NSLog(@"[height] %@", firstSize.height);
//...

At runtime, I got the result:

height (null)

Could someone help me to explain while that code print null for firstSize.height?

Many thanks

War es hilfreich?

Lösung

Use %f instead of %@ :

CGSize firstSize = CGSizeMake(1.0,1.0);
NSLog(@"[height] %f", firstSize.height);

%@ is used for all objects, here you are dealing with Core its float type, you need to use %f for float.

If you want decimal precision as well, use %.2f for 2 decimal digits.

Andere Tipps

You can also use this nice function

 NSString * NSStringFromCGSize (
   CGSize size
);
defined by UIKit in UIGeometry.h (together with many other useful functions):


NSLog(@"Size: %@",NSStringFromCGSize(firstSize));
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top