user3281743
user3281743

Reputation: 295

How to format myString into an actual time

I am new to Objective-C and teaching myself. Right now I am trying to develop an interactive clock, but now I am stuck so I need your expertise. Right now it's formatted into decimals to the 2nd place. I was wondering if it was possible to format it to a colon instead of a decimal. Or am I just approaching this problem wrong. :(

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary    *)change context:(void *)context
{

if(object == _knobControl && [keyPath isEqualToString:@"value"]) {
    self.valueLabel.text = [NSString stringWithFormat:@"%.2f", _knobControl.value];
}
}

Upvotes: 1

Views: 67

Answers (2)

Hot Licks
Hot Licks

Reputation: 47699

If you have, eg, minutes, and you want to display 90 minutes as 1:30, use modulo arithmetic.

int time = 90;
int hours = time / 60;
int minutes = time % 60;
NSString* text = [NSString stringWithFormat:@"%02d:%02d", hours, minutes];

Upvotes: 1

simalone
simalone

Reputation: 2768

I have not understood your question clearly, so following code maybe help you, but I'm not suer:

NSString *text = [NSString stringWithFormat:@"%.2f", _knobControl.value];
self.valueLabel.text = [text stringByReplacingOccurrencesOfString:@"." withString:@":"];

Upvotes: 1

Related Questions