ios - iPhone模拟器时差是否保留在设备上?

标签 ios xcode ios-simulator

在我的 iPhone 应用程序中,我以某种方式需要时间值并像这样获取它:

//Get the full time here
NSDate *now = [NSDate date];
//Seperate as time and date
NSArray *arr = [[[NSString alloc] initWithFormat:@"%@",now] componentsSeparatedByString:@" "];
//Get the time value
NSString *timeOnly = [arr objectAtIndex:1];
//Seperate it like hour, minute and second values
NSArray *timeOnlyArray = [timeOnly componentsSeparatedByString:@":"];
NSInteger hour = [[timeOnlyArray objectAtIndex:0] intValue];
NSInteger minute = [[timeOnlyArray objectAtIndex:1] intValue];

除一件事外一切正常:即使我的计算机和模拟器的时间设置为本地时间 (GMT +2),我在模拟器上调试时获得的值是 GMT 时间(比本地时间早 2 小时)。

没关系,我可以简单地将小时值加 2,但我不知道它在设备上是否相同。如果我以这种方式发布它,它会在设备上工作吗?或者我应该添加 2 小时,因为应用程序也会在真实设备中查看 GMT 时间?

最佳答案

您可能想查看 NSDateFormatter 类(如 AppleDocs 中所写:http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html) 完成后,请阅读此处:Convert UTC NSDate to local Timezone Objective-C

仅将 2 添加到 hours-value 不是好的做法。您将如何处理夏令时?或者其他时区的人何时会使用您的应用?

使用 NSDateFormatter 可能是最适合您的解决方案。

关于ios - iPhone模拟器时差是否保留在设备上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7900080/

相关文章:

iphone - 截屏

ios - 如何使分组的 ListView 标题在 iOS 中正确 float (不粘)?

ios - CoreML模型 undefined symbol 错误

iphone - 即使安装了 iOS 5 模拟器也无法测试

ios - Quickblox:体系结构 x86_64 的 undefined symbol :错误

swift - iOSDropDown : Type 'UIResponder' has no member 'NSNotification'

objective-c - Xcode 4.5 黑屏或异常 : 'A view can only be associated with at most one view controller at a time!'

iphone - 在 iOS 模拟器主屏幕或锁屏中设置壁纸(背景图像)

ios - 模拟器不可用错误 (Xcode 6.1)

ios - 未收到 Twilio 聊天推送通知(Ionic 和 Firebase)