PDA

View Full Version : Problem with using float as a value




macindev
Aug 17, 2009, 05:47 PM
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.


- (void)setChargeProgBar:(id)sender{
[chargeProgBar floatValue:[float batteryLevel]];
}


This will be my first iPhone app... if it ever works...
Any help would be great.



kainjow
Aug 17, 2009, 06:32 PM
floatValue: isn't a valid method (unless you defined it somewhere...). What is chargeProgBar?

dejo
Aug 17, 2009, 07:53 PM
Assuming chargeProgBar is a UIProgressView object, you probably want to look at the Class Reference (http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIProgressView_Class/Reference/Reference.html) for UIProgressView and look at what properties and instance methods it has.

macindev
Aug 17, 2009, 10:21 PM
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.

kainjow
Aug 17, 2009, 10:30 PM
If you don't know how to use Objective-C properties, I suggest reading up on them.

To do what you want, do:
chargeProgBar.progress = someFloatValue;

macindev
Aug 18, 2009, 12:05 AM
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:

- (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:@"%"]];
Error: expected expression before 'float'.
}

- (id)initWithProgressViewStyle:(UIProgressViewStyle)UIProgressViewStyleDefault{
}

- (void)setChargeProgBar:(id)sender{
[[chargeProgBar progress] (float)batteryLevel];
Error: expected expression before 'float'.
}


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.

dejo
Aug 18, 2009, 12:10 AM
I know I'm starting to get annoying, but I thought I had figured out programming in Xcode, but I guess not.
I think it's the programming in Objective-C that is your issue. And did you even read kainjow's response?

macindev
Aug 18, 2009, 12:24 AM
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!

dejo
Aug 18, 2009, 12:28 AM
I've done more with Mac OS programming rather than iPhone OS. And to think I thought it would be exactly the same?
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. :)

admanimal
Aug 18, 2009, 12:39 AM
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?


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.

robbieduncan
Aug 18, 2009, 04:36 AM
I'm pretty sure float is a C keyword and can't be a variable name so anything like:


[float methodName];


is going to (and will always have on OSX) cause issues. I suggest you go back and learn very basic C.

PhoneyDeveloper
Aug 18, 2009, 09:51 AM
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.