将 TimeInterval 与 Integer 进行比较时遇到问题

发布于 2024-09-24 14:38:08 字数 909 浏览 1 评论 0原文

我试图将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

っ〆星空下的拥抱 2024-10-01 14:38:08

NSTimeInterval 定义为 double,因此如果您想将其转换为 int,您可以进行简单的类型转换

NSTimeInterval timeInterval = [startTime timeIntervalSinceNow];
int intSecondsElapsed = (int)timeInterval;

或简单地

int intSecondsElapsed = (int)[startTime timeIntervalSinceNow];

编辑: 如果您在不同的函数中初始化并使用 startTime 变量,您可以那么需要保留它。 [NSDate date] 返回自动释放的对象,因此它在创建的范围之外变得无效。为了让事情变得更容易,声明一个带有 startTime 属性的属性并使用创建它,

self.startTime = [NSDate date];

并且不要忘记在类 dealloc 方法中释放它。

NSTimeInterval is defined as double so if you want to convert it to int you can make simple type cast

NSTimeInterval timeInterval = [startTime timeIntervalSinceNow];
int intSecondsElapsed = (int)timeInterval;

or simply

int intSecondsElapsed = (int)[startTime timeIntervalSinceNow];

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 for startTime and create it using

self.startTime = [NSDate date];

And don't forget to release it in your class dealloc method.

执手闯天涯 2024-10-01 14:38:08

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 to timeInterval, 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 treat timeInterval as a pointer, but failed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文