Convert NSTimeInterval Duration to NSString

I am new to iphone development. I have a problem. I want to convert the value of NSTimeInterval to NSString, but not success in this. Please quickly review the code below.

in .h

NSTimeInterval startTime;
NSTimeInterval stopTime;
NSTimeInterval duration;

in .m

startTime = [NSDate timeIntervalSinceReferenceDate];
stopTime = [NSDate timeIntervalSinceReferenceDate];

duration = stopTime-startTime;

NSLog(@"Duration is %@",[NSDate dateWithTimeIntervalSinceReferenceDate: duration]);
NSLog(@"Duration is %@", [NSString stringWithFormat:@"%f", duration]);

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];

NSString *time = [dateFormatter stringFromDate:duration];----> Error
[dateFormatter release];

And on one label set this line ----

[timeLabel setText:time];

but it does not work, it shows an error: incompatible type for argument 1 'stringFromDate:'

and if I comment on this line, it will show the correct duration in the console window.

Thanks for any help.

+3
source share
3 answers

From the NSDateFormatter class reference :

- (NSString *)stringFromDate:(NSDate *)date
                              ^^^^^^^^ the type of the argument

From your code:

NSTimeInterval duration; 
^^^^^^^^^^^^^^ the type of what you are passing.

" ". , ? , NSTimeInterval NSDate *. NSTimeInterval:

typedef double NSTimeInterval;

NSTimeInterval ...

, NSTimeInterval ( ) NSDate. . NSTimeInterval , %f.

foo = [NSString stringWithFormat: @"%f", timeInterval];
+8

NSTimeInterval , , ,

NSString *time = [NSString stringWithFormat:@"%f", duration];
+3
NSString *time = [dateFormatter stringFromDate:duration];----> Error

NSDate, stringFromDate. , NSTimeInterval , NSString.

0

Source: https://habr.com/ru/post/1771897/


All Articles