标签: nscalendar

如何在 UIPageViewController 中重用同一个 ViewController?

我正在创建一个日历应用程序,我想将其布局为 UIPageViewController 类型的样式。所以每个页面都显示一天。每个页面将完全相同(如 UI 组件中),但仅包含不同的数据。我想使用相同的 ViewController 来执行此操作,因为它显然会更整洁。但我只是想知道这是否可能?如果是这样我该怎么做呢?多谢你们。

scrollview uiviewcontroller nscalendar uipageviewcontroller swift

3
推荐指数
1
解决办法
1437
查看次数

Swift - Date from components is incorrect when printed. Date has 2 values?

I'm trying to store a time of day in a Date:

let calendar = NSCalendar.init(identifier: .gregorian)
let components = NSDateComponents()
components.hour = 7
components.minute = 0

var newDate = calendar?.date(from: components as DateComponents)!

print(newDate!)
Run Code Online (Sandbox Code Playgroud)

However, this yields some bizarre results when I try to print or otherwise use the value. Here's a Swift Playground of the results:

奇怪的日期结果

How can newDate be both 7:00AM and 11:56AM at the same time?

nscalendar nsdatecomponents swift

3
推荐指数
1
解决办法
1076
查看次数

如何检查几年前用户选择的NSDate是否发生过?

在我的应用程序中,我需要检查用户是否已满18岁.我为他提供了UIDatePickerView,他选择了他的生日日期.我将它存储在NSDate字段中.如何检查自生日以来已经过去18年了?

iphone nsdate nscalendar

2
推荐指数
1
解决办法
1100
查看次数

NSCalendar dateFromComponents返回错误的日期

我正在NSDate上编写一个类别来从ISO 8601字符串表示法(YYYYMMDD)创建一个NSDate.

即使我通过20010226,我也会回到2001-02-25 23:00:00 +0000.我究竟做错了什么?

这是代码:

-(id) initWithISO8601Date: (NSString *) iso8601Date{
    // Takes a date in the YYYYMMDD form


    int year = [[iso8601Date substringWithRange:NSMakeRange(0, 4)] integerValue];
    int month = [[iso8601Date substringWithRange:NSMakeRange(4, 2)] integerValue];
    int day = [[iso8601Date substringWithRange:NSMakeRange(6,2)] integerValue];

    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setYear:year];
    [comps setMonth:month];
    [comps setDay:day];


    self = [[NSCalendar currentCalendar] dateFromComponents:comps];
    NSLog(@"%@", self);

    [comps release];

    return self;

}
Run Code Online (Sandbox Code Playgroud)

cocoa cocoa-touch nsdate nscalendar nsdatecomponents

2
推荐指数
1
解决办法
3209
查看次数

发送NSCFCalendar nil NSDate Sarcastic错误

这里有几个主题讨论在向日历方法发送nil日期时在控制台中弹出的新iOS 6 Xcode错误:

-[__NSCFCalendar components:fromDate:toDate:options:]: fromDate cannot be nil
I mean really, what do you think that operation is supposed to mean with a nil fromDate?
An exception has been avoided for now.
A few of these errors are going to be reported with this complaint, then further 
violations will simply silently do whatever random thing results from the nil.
Run Code Online (Sandbox Code Playgroud)

这个错误让我笑了一下,但让我思考......

我编写的导致此错误的代码从大量来源(从日期格式化程序,字符串,用户输入等)获取日期信息,我真的不希望每个提供的日期都有效 - 我知道完全有些人将是零,我很高兴处理这些对象的结果.

当我第一次开始使用Objective C编程时,我读到的一个功能就是向nil对象发送消息.这很棒,因为这意味着我不必担心传递零对象.

现在我收到了来自Xcode的一个脾气暴躁的错误消息告诉我,我已经向一个零对象发送了一条消息.

现在推断我必须在方法中使用它之前检查每个对象是否为零?我是否在做与nil对象完全令人发指的事情?

null xcode objective-c nsdate nscalendar

2
推荐指数
1
解决办法
3457
查看次数

在两个NSDates之间发生的特定工作日的计数

我怎样才能找到两个特定工作日的计数NSDates

我已经搜索了很长一段时间,但只提出了一个解决方案,其中计算了整个工作日的数量,而不仅仅是一个特定的工作日.

cocoa cocoa-touch date objective-c nscalendar

2
推荐指数
1
解决办法
590
查看次数

如何让NSCalendar周开始于星期一而不是星期日

当我试图让当前的星期日期出现在日志中时(星期一到星期日),这就像一周从星期日开始.例如:本周是38岁.昨天是9月20日.我的代码确实显示了周一到周日的日期.但是今天(9月21日)我的日志显示了下周的日期(第39周),当时它仍然是第38周.

我的代码:

NSDate *currentDate = [NSDate date];
NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *currentComps = [myCalendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekOfYearCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:currentDate];
int thisWeeksNumber = currentComps.weekOfYear;
NSLog(@"1  %d", thisWeeksNumber);

[currentComps setWeekday:2];

NSDate *firstDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
NSDate *secondDayOfTheWeek = [self dateByAddingDays:1 toDate:firstDayOfTheWeek];
NSDate *thirdDayOfTheWeek = [self dateByAddingDays:2 toDate:firstDayOfTheWeek];
NSDate *fourthDayOfTheWeek = [self dateByAddingDays:3 toDate:firstDayOfTheWeek];
NSDate *fifthDayOfTheWeek = [self dateByAddingDays:4 toDate:firstDayOfTheWeek];
NSDate *sixthDayOfTheWeek = [self dateByAddingDays:5 toDate:firstDayOfTheWeek];
NSDate *seventhDayOfTheWeek = [self dateByAddingDays:6 toDate:firstDayOfTheWeek]; …
Run Code Online (Sandbox Code Playgroud)

date objective-c nsdate nscalendar ios

2
推荐指数
1
解决办法
1728
查看次数

在Swift 2.0中将NSCalendarUnit与OR(管道)相结合时出错

我有一些在Swift 2.0中破解的代码:

let formatter = NSDateComponentsFormatter()
formatter.allowedUnits = NSCalendarUnit.Year
formatter.allowedUnits |= .Month
formatter.allowedUnits |= .WeekOfMonth 
formatter.allowedUnits |= .Day
formatter.allowedUnits |= .Hour
formatter.allowedUnits |= .Minute
Run Code Online (Sandbox Code Playgroud)

我收到了错误Binary operator '|=' cannot be applied to 'NSCalenderUnit' operands.

做这种事的新方法是什么?

nsdateformatter nscalendar nsdatecomponents swift swift2

2
推荐指数
1
解决办法
342
查看次数

检查日期何时过去 - 斯威夫特

好吧,标题几乎说明了一切.我想要做的是检查日期何时过去.因此,举例来说,让我们说用户正在使用我的应用程序然后他们上午睡觉并在早上检查我的应用程序.当我的应用程序打开时,我需要检查一天是否发生了变化.

此外,我不需要在应用程序终止或在后台或任何内容时知道此信息.我只需要知道应用程序运行时用户是否正在与之交互时日期是否已更改.

注意:我已经查看了与此问题有关的其他Stack Overflow帖子,但没有一个帮助过我.

time nsdate nscalendar swift

2
推荐指数
2
解决办法
1144
查看次数

如何在每天开始时重置变量?

我有一个变量来跟踪我想在每天开始时重置的用户统计信息.我怎样才能做到这一点?由于不允许应用程序在后台运行,似乎每次应用程序处于活动状态时我都必须进行检查,但我不知道如何重置我只有一次的变量.这是我想要使用的功能:

    let beginingOfDay = NSCalendar.currentCalendar().startOfDayForDate(NSDate())
    func resetCurrentTime(){

    // Date comparision to compare current date and begining of the day.
    let dateComparisionResult:NSComparisonResult = NSDate().compare(beginingOfDay)

    if dateComparisionResult == NSComparisonResult.OrderedDescending || dateComparisionResult == NSComparisonResult.OrderedSame {
        // Current date is greater or equal to end date.
        currentTime = 0 //reset the time tracker
    }
}
Run Code Online (Sandbox Code Playgroud)

我想使用此函数来检查应用程序何时启动,但问题是应用程序可能每天多次启动.如果用户正在使用该应用程序,或者当应用程序变为活动状态或当天第一次启动时,如何在一天开始时仅重置一次变量?谢谢

nsdate nscalendar swift

2
推荐指数
1
解决办法
780
查看次数