Problem with using float as a value

Discussion in 'iOS Programming' started by macindev, Aug 17, 2009.

  1. macindev macrumors newbie

    Joined:
    Jul 6, 2009
    Location:
    Seattle, Washington, USA
    #1
    What I am trying to do is use the batteryLevel method to set it's float value to the float value of a ProgressView. But unfortunatley, I keep running into errors.
    I get one that says "Expected expression before 'float'.
    And one that says "Expected ':' before ']' token.

    Code:
    - ([COLOR="magenta"]void[/COLOR])setChargeProgBar:([COLOR="magenta"]id[/COLOR])sender{
    	[[COLOR="teal"]chargeProgBar[/COLOR] floatValue:[[COLOR="magenta"]float[/COLOR] [COLOR="navy"]batteryLevel[/COLOR]]];
    }
    
    This will be my first iPhone app... if it ever works...
    Any help would be great.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    floatValue: isn't a valid method (unless you defined it somewhere...). What is chargeProgBar?
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    Assuming chargeProgBar is a UIProgressView object, you probably want to look at the Class Reference for UIProgressView and look at what properties and instance methods it has.
     
  4. macindev thread starter macrumors newbie

    Joined:
    Jul 6, 2009
    Location:
    Seattle, Washington, USA
    #4
    Yes, setChargeProgBar: is the outlet to my UIProgressView. I checked out the API Documentation, but I still don't understand how I tell the progress bar to set it's float value to that of the batteryLevel's float value.
    Can anyone help me please?
    Also, I should add that I'm fairly new at C/ObjC.
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    If you don't know how to use Objective-C properties, I suggest reading up on them.

    To do what you want, do:
    Code:
    chargeProgBar.progress = someFloatValue;
     
  6. macindev thread starter macrumors newbie

    Joined:
    Jul 6, 2009
    Location:
    Seattle, Washington, USA
    #6
    I know I'm starting to get annoying, but I thought I had figured out programming in Xcode, but I guess not.
    I just don't get why I'm always having so many errors with floating-point...

    Sorry if the code looks like it came from a 3 year-old kid...:(

    Here's the code/errors:
    Code:
    - (void)showDeviceModel:(id)sender{
    	[deviceModel setString: (NSString *)model];
    }
    
    - (BOOL)isBatteryMonitoringEnabled:(id)sender{
    	return YES;
    
    }
    - (void)setChargePercentIndicator:(id)sender{
    	[chargePercentIndicator setString:[[@"Charge: " stringByAppendingString:[float batteryLevel]] stringByAppendingString:@"%"]];
    [COLOR="red"]Error: expected expression before 'float'.[/COLOR]
    }
    	
    - (id)initWithProgressViewStyle:(UIProgressViewStyle)UIProgressViewStyleDefault{
    }
    
    - (void)setChargeProgBar:(id)sender{
    	[[chargeProgBar progress] (float)batteryLevel];
    [COLOR="red"]Error: expected expression before 'float'.[/COLOR]
    }
    
    If I try to take out 'float', then it won't recognize batteryLevel.
    I've attached a screenshot of what it should look like in the end.
     

    Attached Files:

  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    I think it's the programming in Objective-C that is your issue. And did you even read kainjow's response?
     
  8. macindev thread starter macrumors newbie

    Joined:
    Jul 6, 2009
    Location:
    Seattle, Washington, USA
    #8
    Sorry about that, I was just stuck on figuring what the heck it all was. I've done more with Mac OS programming rather than iPhone OS. And to think I thought it would be exactly the same?

    I'm still trying to figure out how to convert my random assortment of methods and syntax into a neat implementation.

    Thanks for all of the help!
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Well, if your Mac OS programming was using Objective-C, they are very, very similar. Much more alike than different. Granted, not exactly the same. :)
     
  10. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #10
    Not to be rude, but most of your errors show a lack of understanding of how Objective-C programming works and have little if anything to do with the iPhone.
     
  11. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #11
    I'm pretty sure float is a C keyword and can't be a variable name so anything like:

    Code:
    [float methodName];
    
    is going to (and will always have on OSX) cause issues. I suggest you go back and learn very basic C.
     
  12. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #12
    Short answer: read up on Obj-C 2.0 properties in the Obj-C 2.0 programming guide.

    Long answer: read a book on iPhone development. That's the way to learn.
     

Share This Page