我想使用NSTimer如何在程序中初始化定时器产生2秒的延迟?
我正在开发一个从后端接收数据的应用程序.问题是,日期在我的应用程序中无法正常工作.所以我最后比较了cocoa生成的时间戳和我的java后端生成的时间戳.
是否有任何理由认为java比目标c更长?
刚比较:
java的
1318226845471
可可
1318226841
我正在生成可可时间戳:
[[NSDate date] timeIntervalSince1970]
Run Code Online (Sandbox Code Playgroud)
有什么建议?我将不胜感激任何帮助!
先谢谢你,Alex
我想将我的本地日期([NSDate日期])转换为GMT以创建JSON字符串(/ Date(1324435876019-0000)/).
当我将时钟设置为EST时区时,我的代码工作正常,当我将时区更改为PST时,我的代码仍然充当EST.你能告诉我代码是什么问题吗?
NSDate *localDate = [NSDate date];
NSTimeInterval timeZoneOffset = [[NSTimeZone systemTimeZone] secondsFromGMT]; // You could also use the systemTimeZone method
NSTimeInterval gmtTimeInterval = [localDate timeIntervalSinceReferenceDate] - timeZoneOffset;
NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];
NSString *time = [NSString stringWithFormat:@"/Date(%@-0000)/", [NSNumber numberWithLongLong:[gmtDate timeIntervalSince1970] * 1000]];
Run Code Online (Sandbox Code Playgroud) 如何将任何给定日期转换为毫秒?例如,2014-01-23到时间戳转换.
NSDate *date = [dateFormatter dateFromString:@"2014-01-23"];
NSLog(@"date=%@",date);
NSTimeInterval interval = [date timeIntervalSince1970];
NSLog(@"interval=%f",interval);
NSDate *methodStart = [NSDate dateWithTimeIntervalSince1970:interval];
[dateFormatter setDateFormat:@"yyyy/mm/dd "];
NSLog(@"result: %@", [dateFormatter stringFromDate:methodStart]);
Run Code Online (Sandbox Code Playgroud)
输出结果:1970/30/01
Swift动画的默认时间间隔是什么,比如类setNavigationBarHidden(Bool, animated: Bool)和setToolbarHidden(Bool, animated: Bool)方法UINavigationController?我想知道我的动画匹配相同的持续时间.
animation uiview uinavigationcontroller nstimeinterval swift
我需要DispatchTimeInterval从NSTimeInterval(或Double)中减去一个。
是否有一个转换的标准方式DispatchTimeInterval,以一个NSTimeInterval?
当视图加载时,我有一个 date1 和当前日期。现在我想显示从那天起已经过去了多少天。
让 timeInterval = date1.timeIntervalSince(date2)
现在我想以天为单位转换 timeInterval。任何人都可以帮忙吗?
我仍然是Objective C语法的新手,所以我可能会过度复杂,但我似乎无法弄清楚如何将NSTimeInterval传递给线程.
我想启动一个线程,它休眠从主线程发送的x秒参数,如下所示:
[NSThread detachNewThreadSelector:@selector(StartServerSynchThread) toTarget:self withObject:5];
- (void) StartServerSynchThread:(NSTimeInterval *)sleepSecondsInterval {
[NSThread sleepForTimeInterval:sleepSecondsInterval];
}
Run Code Online (Sandbox Code Playgroud)
但是编译器一直给我一个语法错误.我不确定应该怎么做.任何帮助,将不胜感激.谢谢!
我正在尝试将timeInterval与整数值进行比较,因此我尝试将timeInterval转换为整数并进行比较.但是我收到错误'无法转换为指针类型':
NSTimeInterval timeInterval = [startTime timeIntervalSinceNow];
int intSecondsElapsed = [timeInterval intValue]; // Error here !!!!
if ([counterInt != intSecondsElapsed])
{
// Do something here...
}
Run Code Online (Sandbox Code Playgroud)
怎么会这样做?
编辑:包括与执行错误相关的更多详细信息.
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;
}
}
Run Code Online (Sandbox Code Playgroud) 在一段时间过后,我不想设置一个"触发"(并做某事)的计时器.
因此,我对NSTimer课程及其方法不感兴趣.
所有我感兴趣的是计算经过的时间,例如,一些while循环正在执行.NSDate我猜可以使用如下:
NSDate currentDate = [[NSDate alloc] init];
while(someConditionIsTrue)
{
// executing..
}
timeElapsed = [self timeIntervalSinceDate:currentDate];
NSLog(@"time elapsed was: %i", timeElapsed);
Run Code Online (Sandbox Code Playgroud)
但是,如果我理解正确,timeIntervalSinceDate:只能计算秒数.
有没有办法可以计算在几毫秒内经过的时间?
换句话说,我可以计算的最小单位是什么?
nstimeinterval ×10
nsdate ×4
ios ×3
objective-c ×3
swift ×3
iphone ×2
nstimer ×2
xcode ×2
animation ×1
cocoa ×1
date ×1
nsthread ×1
nstimezone ×1
timestamp ×1
uiview ×1