I've seen others ask similar questions, but none of the answers have worked for me; I get the message "Expected ';' at end of declaration list". Can someone walk me through what I'm supposed to do? (This is my first question here, so I'm sorry if I did something wrong.)
            Asked
            
        
        
            Active
            
        
            Viewed 2,908 times
        
    -1
            
            
        - 
                    2Usually a good idea to show the code and give a little bit more detail about where you think something has gone wrong. Allows people to point you in the right direction. Also, helps to show what research you have done yourself. – Ben Green May 30 '13 at 13:19
- 
                    And don't use the "xcode" tag for non-Xcode questions. – May 30 '13 at 14:06
3 Answers
11
            
            
        float yourVariable = [yourTextField.text floatValue];
Make sure you add validations to your textfield. If you have any non-numeric it will return 0.
How to convert NSString value @"3.45" into float?
I also gone through following link which says  returns 0.0 if the receiver doesn’t begin with a valid text representation of a floating-point number.
1
            
            
        float floatsample = [sampletextfield.text floatValue];
 
    
    
        Ry-
        
- 218,210
- 55
- 464
- 476
 
    
    
        iPhone developer.
        
- 2,122
- 1
- 16
- 17
1
            
            
        Do this:
NSNumberFormatter *floatFormatter = [[NSNumberFormatter alloc] init];
[floatFormatter numberFromString:textField.text];
Assuming textField is your textField. Then you can check floatFormatter for a nil NSNumber if it can't parse the string properly.
 
    
    
        swiftcode
        
- 3,039
- 9
- 39
- 64
 
     
    