Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to convert a NSString to a CGFloat in Objective C?
    primarykey
    data
    text
    <p>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</p> <pre><code>$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); </code></pre> <p>------------------------------------------ edit --------------------------------------------</p> <p>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:</p> <pre><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); </code></pre> <p>For future reference for those who want to know how to convert a String to a float or CG float...simply follow this:</p> <pre><code>CGFloat yourFloatVariable = [stringThatYouWantToConvert floatValue]; </code></pre> <p>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. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload