Rob
Rob

Reputation: 2329

How to convert a NSString to a CGFloat in Objective C?

So I have been stuck for a couple of hours trying to get this to work. In my application I receive XML data, parse through it and create an image with buttons programmatically. However, when I try and convert Strings to CGFloats so that I can use them for the button coordinates it doesn't work. I have tried a few different tutorials and methods and nothing seems to work. You can see in the code below that I am trying it multiple ways. I know that the strings have the correct numbers in them, I just can't get it to convert. It comes back as Null. Help! Thanks in advance

$CGFloat buttonViewXvalue = [tempXCorrVariable floatValue];
   CGFloat buttonViewYvalue = [tempYCorrVariable floatValue];
    buttonViewWidth = [tempWidthCorrVariable floatValue];
    buttonViewLength = [tempLengthCorrVariable floatValue];

    //The "buttonView" variables are Float Variables and the "temp" Variables are Strings.

    NSLog(@"Float X = %@", buttonViewXvalue); 
    NSLog(@"Float Y = %@", buttonViewYvalue);
    NSLog(@"Float Width = %@", buttonViewWidth);
    NSLog(@"Float Length = %@", buttonViewLength);

------------------------------------------ edit --------------------------------------------

Thanks to good responses I got it to work. Turns out it was simply a formatting issue with the NSLog. Here is the new code:

$CGFloat buttonViewXvalue = [tempXCorrVariable floatValue];
   CGFloat buttonViewYvalue = [tempYCorrVariable floatValue];
    buttonViewWidth = [tempWidthCorrVariable floatValue];
    buttonViewLength = [tempLengthCorrVariable floatValue]; 

    NSLog(@"Float X = %f", buttonViewXvalue);
    NSLog(@"Float Y = %f", buttonViewYvalue);
    NSLog(@"Float Width = %f", buttonViewWidth);
    NSLog(@"Float Length = %f", buttonViewLength);

For future reference for those who want to know how to convert a String to a float or CG float...simply follow this:

CGFloat yourFloatVariable = [stringThatYouWantToConvert floatValue];

Make sure that you use the exact word on the last part "floatValue." That isn't another variable or a value or anything, just put that in your code.

Upvotes: 1

Views: 8094

Answers (2)

PengOne
PengOne

Reputation: 48406

I think you should take a moment to review the string format specifiers:

%@     Object
%d, %i signed int
%u     unsigned int
%f     float/double

%x, %X hexadecimal int
%o     octal int
%zu    size_t
%p     pointer
%e     float/double (in scientific notation)
%g     float/double (as %f or %e, depending on value)
%s     C string (bytes)
%S     C string (unichar)
%.*s   Pascal string (requires two arguments, pass pstr[0] as the first, pstr+1 as the second)
%c     character
%C     unichar

%lld   long long
%llu   unsigned long long
%Lf    long double

When you use %@ is send the message address to the NSObject. If you're using a float, then it isn't an NSObject, so errors will ensue.

To make your NSLog calls work, you have two options, of which I prefer the former:

NSLog(@"Float X = %f", myFloat); 
NSLog(@"Float X = %@", [NSNumber numberWithFloat:myFloat]); 

The second is silly, but emphasizes the difference, I hope.

Upvotes: 3

AliSoftware
AliSoftware

Reputation: 32681

You use the wrong format specifies: maybe your float values are right after all, but displaing it using the NSLog fails.

Use NSLog(@"Float X = %f",buttonViewXvalue); instead of %@. Use only %@ for NSObjects.

Upvotes: 3

Related Questions