将 TimeInterval 与 Integer 进行比较时遇到问题
我试图将 timeInterval 与整数值进行比较,因此我尝试将 timeInterval 转换为整数并进行比较。但我收到错误“无法转换为指针类型”:
NSTimeInterval timeInterval = [startTime timeIntervalSinceNow];
int intSecondsElapsed = [timeInterval intValue]; // Error here !!!!
if ([counterInt != intSecondsElapsed])
{
// Do something here...
}
如何执行此操作?
编辑:包括与执行错误相关的更多详细信息。
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//[formatter setDateFormat:@"HH:mm:ss"];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
startTime = [NSDate date];
[formatter release];
if ([deviceType isEqualToString:@"iPhone 4"]||[deviceType isEqualToString:@"iPhone Simulator"])
{
int intSecondsElapsed = (int)[startTime timeIntervalSinceNow];
if (counterInt != intSecondsElapsed)
{
counterInt = intSecondsElapsed;
}
}
I'm trying to compare the timeInterval to an integer value, so I try and convert the timeInterval to an integer and compare. But I'm getting an error 'Cannot convert to a pointer type':
NSTimeInterval timeInterval = [startTime timeIntervalSinceNow];
int intSecondsElapsed = [timeInterval intValue]; // Error here !!!!
if ([counterInt != intSecondsElapsed])
{
// Do something here...
}
How would go about doing this ?
EDIT: Include more detail relating to Execute error.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//[formatter setDateFormat:@"HH:mm:ss"];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
startTime = [NSDate date];
[formatter release];
if ([deviceType isEqualToString:@"iPhone 4"]||[deviceType isEqualToString:@"iPhone Simulator"])
{
int intSecondsElapsed = (int)[startTime timeIntervalSinceNow];
if (counterInt != intSecondsElapsed)
{
counterInt = intSecondsElapsed;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSTimeInterval 定义为 double,因此如果您想将其转换为 int,您可以进行简单的类型转换
或简单地
编辑: 如果您在不同的函数中初始化并使用
startTime
变量,您可以那么需要保留它。[NSDate date]
返回自动释放的对象,因此它在创建的范围之外变得无效。为了让事情变得更容易,声明一个带有startTime
属性的属性并使用创建它,并且不要忘记在类 dealloc 方法中释放它。
NSTimeInterval is defined as double so if you want to convert it to int you can make simple type cast
or simply
Edit: If you initialize and use your
startTime
variable in different functions you need to retain it then.[NSDate date]
returns autoreleased object so it becomes invalid outside the scope it was created. To make things easier declare a property with retain attribute forstartTime
and create it usingAnd don't forget to release it in your class dealloc method.
Vladimir 所说的完全正确,我只是想补充一点,您看到错误“无法转换为指针类型”的原因是因为您尝试将消息
intValue
发送到timeInterval
,只有当您发送消息的目标是指向将响应该消息的对象的指针时,您才能执行此操作。编译器尝试将timeInterval
视为指针,但失败了。What Vladimir said is exactly right, I just wanted to add that the reason that you were seeing the error "Cannot convert to a pointer type" is because you tried to send the message
intValue
totimeInterval
, which you can only do if what you're sending the message to is a pointer to an object that will respond to it. The compiler tried to treattimeInterval
as a pointer, but failed.