xcode 将日期/时间存储到核心数据中的最佳方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2520640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:13:58  来源:igfitidea点击:

Best way to store date/time into core data

iphonexcodemacoscore-data

提问by Dave

For each course I am storing the day of the week the class the user has...and the start and end time and room no. What would be the best approach in terms of data type, Like for storing time?? so that when I pull it out I can display it e.g 1:00 PM.

对于每门课程,我都会存储用户上课的星期几……以及开始和结束时间以及房间号。就数据类型而言,最好的方法是什么,比如存储时间?这样当我把它拉出来时,我就可以显示它,例如下午 1:00。

Thanks!

谢谢!

回答by Barry Wark

Use a date attribute in your managed object model for each time you want to store. See the section on "Dates and Times" in the Core Data Programming Guide.

每次要存储时,在托管对象模型中使用日期属性。请参阅核心数据编程指南中的“日期和时间”部分 。

回答by TechZen

If its an actual date e.g. Tuesday, December 15th 2010 1:00pm CST, then you can use a data attribute.

如果它是实际日期,例如 2010 年 12 月 15 日星期二 CST 下午 1 点,那么您可以使用数据属性。

However, if it's just a generic time of day e.g. any given Wednesday at 1:00pm, the you will need to create attributes to hold the day, the time and meridian as strings and or numbers.

但是,如果它只是一天中的一般时间,例如任何给定的星期三下午 1:00,您将需要创建属性以将日期、时间和子午线保存为字符串或数字。

I would recommend creating a entity in the model just for this information and then setting a relationship to it.

我建议仅针对此信息在模型中创建一个实体,然后与其建立关系。

Pseudocode:

伪代码:

DayAndTimeEntity{
    weekdayName = sting;
    timeOfDay = string or number; //use a number if you need to do calculations
    isAM = BOOL;
}

in use, "any Monday at 1:00pm" would look like:

在使用中,“任何星期一下午 1:00”看起来像:

aDayAndTimeEntityInstance{
    weekdayName = "Monday";
    timeOfDay = "1:00" 
    isAM = NO;
}

Then set a relationship to any other entities that need to store this time. You can elaborate on the basic idea as needed.

然后设置与需要存储此时间的任何其他实体的关系。您可以根据需要详细说明基本思想。

回答by Jimmy

Date Type should work

日期类型应该有效