让这段代码更加高效?
我有下面的代码,我想让它更有效率。目前它有点慢,我想让它更快。我知道我可以在这里做一些事情,但不知道该怎么做才能提高效率。 这是代码:
NSDate *now = [NSDate date];
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MM-dd-yyyy"];
NSString *dateString = [formatter stringFromDate:now];
NSDate *enddate = [formatter dateFromString:dateString];
[[NSUserDefaults standardUserDefaults] setObject:enddate forKey:@"theDate"];
谢谢!
I have the below code, and I would like to make it more efficient. Currently it is a tad slow and I want to make it faster. I know there is something I can do here but not sure what to do to make it more efficient.
Here is the code:
NSDate *now = [NSDate date];
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MM-dd-yyyy"];
NSString *dateString = [formatter stringFromDate:now];
NSDate *enddate = [formatter dateFromString:dateString];
[[NSUserDefaults standardUserDefaults] setObject:enddate forKey:@"theDate"];
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以对我来说,您似乎想在 UserDefaults 文件中的“theDate”键下存储当前日期的格式化版本(如果我错了,请纠正我)。
你能不能只使用这一行:
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"theDate"];
然后在实际显示时(或如果)实际显示时处理格式。
So to me it looks like you want to store a formatted version of the current date in the UserDefaults file under the key "theDate" (correct me if I'm wrong).
could you not just use the line:
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"theDate"];
and then deal with the formatting when (or if) it is actually displayed.