- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是 iPhone 开发的新手。我有一个问题。我想在 NSString 中转换一个 NSTimeInterval 值,但没有成功。请快速查看以下代码。
在 .h 中
NSTimeInterval startTime;
NSTimeInterval stopTime;
NSTimeInterval duration;
.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];
然后在一个标签上设置这个字符串----
[timeLabel setText:time];
但它不起作用,它显示错误:'stringFromDate:' 的参数 1 的类型不兼容
如果我评论该行,它会在控制台窗口中显示正确的持续时间。
感谢您的帮助。
最佳答案
来自NSDateFormatter class reference :
- (NSString *)stringFromDate:(NSDate *)date
^^^^^^^^ the type of the argument
来自您的代码:
NSTimeInterval duration;
^^^^^^^^^^^^^^ the type of what you are passing.
错误显示“类型不兼容”。你认为这意味着什么?也许 NSTimeInterval 与 NSDate* 不兼容。以下是文档中关于 NSTimeInterval 的内容:
typedef double NSTimeInterval;
NSTimeInterval is always specified in seconds...
错误告诉您编译器无法将 NSTimeInterval(实际上是 double 值)转换为指向 NSDate 的指针。这真的不足为奇。由于 NSTimeInterval 实际上是一个 double 值,您可以使用 %f
格式说明符轻松地将其转换为字符串。
foo = [NSString stringWithFormat: @"%f", timeInterval];
关于iphone - 将 NSTimeInterval 持续时间转换为 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4041481/
我试图找出从我的 iPad 到我的网站的时间差异,它们都是本地的。一切正常,直到我到达下面的行,它应该占用 iPad 时间并从中减去 serverTime。 self.serverTimeOffSet
如何将 NSTimeInterval 值转换为 NSData? 最佳答案 Xcode 8.3.1 • Swift 3.1 var nowInterval = Date().timeIntervalSi
我知道我一定把这件事搞得太复杂了,因为它 NSTimeInterval 只是一个 double ,但我似乎无法正确完成这件事,因为我对 objective-c 的接触很少。场景如下: 导入应用程序的数
我正在为我的 cocoa 类编写单元测试用例。下面是测试用例。我无法弄清楚为什么这个测试用例失败,即使断言消息中的 expect 和 diff 是相同的。我怀疑这可能与 double 或 NSTime
我正在为一个非主流流开发一个简单的 radio 闹钟应用程序,我想在其中醒来。它使用退出后台技巧在手机锁定时保持运行,类似于 Rise/Sleepcycle。 最初如果我要在午夜之前设置闹钟,第二天闹
我正在计算 startTime 和 endTime 之间的持续时间,并使用以下代码来获取两个 NSDate 之间的时间间隔 NSTimeInterval duration = [self.en
// calculate elapsed time NSTimeInterval elapsed = currentTime - startTime; int mins = (int) (elapse
我正在构建一个应用程序,而其中一部分逻辑是对大量 NSTimeIntervals 求和并将结果转换为小时和分钟。 我正在使用 for 循环来添加这样的间隔 NSTimeInterval totalIn
假设我的生日是 (mon/day/year) 10-05-1932(如 NSDate),如何将其转换为 NSTimeInterval。 同样,如何将 NSTimeInterval 转换回 NSDate
我发现日期非常不直观且令人困惑,所以这里开始。 我有一系列带日期的事件,如果任何一个事件的日期超过一个小时,我想删除它。 我使用的代码是: NSDate *now = [NSDate date]; E
我正在尝试转换 this answer 中的 Objective-C 代码, 在 this answer 中找到更正, swift : var theTimeInterval = NSTimeInte
我有一个 NSTimeInterval 和一个 JSON 值 1257808000000。 我这样做: NSTimeInterval *myTimestamp = [myJSON objectFor
如何将字符串值如 12:02:21.3213 解析为 NSTimeInterval? NSDateComponentsFormatter,从 iOS8 开始可用,只支持格式化,不支持解析。 最佳答案
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 去年关闭。 Improve this questio
我知道除以 60 得到分钟、小时等的快速但肮脏的方法 但是 API 中是否有已经实现的官方方法,并使用适当的语言来表示分钟、秒等? 感谢您的帮助。 最佳答案 使用 NSDate 的 -dateWith
我在 NSManagedObject 子类中使用 setValueForKey 设置 NSTimeInterval,该值设置正确,并且在使用 valueForKey 检索时也是正确的,但是,如果直接使
如何舍入 NSTimeInterval 对象的值? 例如,我有一个名为 duration 的 NSTimeInterval 对象,它的值为 1459(24.31667 分钟),我如何将它四舍五入为 2
刚接触 iOS SDK 和 Objective-C,有一个关于 NSTimeInterval 的简单问题。 我有一个非常简单的游戏,我想显示游戏已经玩了多长时间(以便我可以节省最佳时间)。 现在,当然
我正在开发一个应用程序,我在其中获取 2 个 NSdates 之间的差异,并获取使用 NSTimeInterval 之间的间隔 let timediff = timeDownValue?.timeIn
编辑:我是个白痴。我试图将变量放入 NSTimer()而不是 NSTimeInterval出于任何愚蠢的原因。我想我的问题是如何将所有这些都放在一个单独的类中? 理想情况下,我希望所有这些都包含在一个
我是一名优秀的程序员,十分优秀!