gpt4 book ai didi

ios - 在特定日期显示特定文件的建议逻辑

转载 作者:行者123 更新时间:2023-12-04 08:03:20 25 4
gpt4 key购买 nike

我正在开发的应用程序将在其中包含一年中每个工作日的每日消息。我将每个工作日的 PDF 文件放在名为“星期一”、“星期二”、“星期三”、“星期四”、“星期五”的文件夹中。每个 PDF 都被命名为 0.pdf、1.pdf 等,以便我可以增加一个计数。下面是我的显示代码,但我遇到的问题是每次加载 View 时它都会增加计数。在与前一个 PDF 不同的日期之前不前进到下一个 PDF 的好方法是什么?

- (void)viewDidLoad {
NSDateFormatter* day = [[NSDateFormatter alloc] init];
[day setDateFormat: @"EEEE"];
NSLog(@"the day is: %@", [day stringFromDate:[NSDate date]]);
NSString *dayString = [day stringFromDate:[NSDate date]];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger whichWeekMonday = [defaults integerForKey:@"DevoWeekMonday"];
NSInteger whichWeekTuesday = [defaults integerForKey:@"DevoWeekTuesday"];
NSInteger whichWeekWednesday = [defaults integerForKey:@"DevoWeekWednesday"];
NSInteger whichWeekThursday = [defaults integerForKey:@"DevoWeekThursday"];
NSInteger whichWeekFriday = [defaults integerForKey:@"DevoWeekFriday"];
NSLog(@"Integer Thursday: %ld", (long)whichWeekThursday);
NSString *whichWeekMondayString = [[NSNumber numberWithInteger:whichWeekMonday] stringValue];
NSString *whichWeekTuesdayString = [[NSNumber numberWithInteger:whichWeekTuesday] stringValue];
NSString *whichWeekWednesdayString = [[NSNumber numberWithInteger:whichWeekWednesday] stringValue];
NSString *whichWeekThursdayString = [[NSNumber numberWithInteger:whichWeekThursday] stringValue];
NSLog(@"Thursday string: %@", whichWeekThursdayString);
NSString *whichWeekFridayString = [[NSNumber numberWithInteger:whichWeekFriday] stringValue];




if ([dayString isEqualToString:@"Monday"]) {
NSString *Documents = [[NSBundle mainBundle] pathForResource:whichWeekMondayString ofType:@"pdf" inDirectory:@"Mondays"];


NSURL *url = [NSURL fileURLWithPath:Documents];
NSLog(@"%@",url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[savedweb loadRequest:request];
NSInteger updatedNumberMonday = whichWeekMonday + 1;
[defaults setInteger:updatedNumberMonday forKey:@"DevoWeekMonday"];
[defaults synchronize];
}
if ([dayString isEqualToString:@"Tuesday"]) {
NSString *Documents = [[NSBundle mainBundle] pathForResource:whichWeekTuesdayString ofType:@"pdf" inDirectory:@"Tuesdays"];


NSURL *url = [NSURL fileURLWithPath:Documents];
NSLog(@"%@",url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[savedweb loadRequest:request];
NSInteger updatedNumberTuesday = whichWeekTuesday +1;
[defaults setInteger:updatedNumberTuesday forKey:@"DevoWeekTuesday"];
[defaults synchronize];

}
if ([dayString isEqualToString:@"Wednesday"]) {
NSString *Documents = [[NSBundle mainBundle] pathForResource:whichWeekWednesdayString ofType:@"pdf" inDirectory:@"Wednesdays"];


NSURL *url = [NSURL fileURLWithPath:Documents];
NSLog(@"%@",url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[savedweb loadRequest:request];
NSInteger updatedNumberWednesday = whichWeekWednesday +1;
[defaults setInteger:updatedNumberWednesday forKey:@"DevoWeekWednesday"];
[defaults synchronize];

}
if ([dayString isEqualToString:@"Thursday"]) {

// perform some action
NSString *Documents = [[NSBundle mainBundle] pathForResource:whichWeekThursdayString ofType:@"pdf" inDirectory:@"Thursdays"];

NSLog(@"File: %@", Documents);
NSURL *url = [NSURL fileURLWithPath:Documents];
NSLog(@"%@",url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[savedweb loadRequest:request];



NSInteger updatedNumberThursday = whichWeekThursday +1;
[defaults setInteger:updatedNumberThursday forKey:@"DevoWeekThursday"];
[defaults synchronize];


}
if ([dayString isEqualToString:@"Friday"]) {
NSString *Documents = [[NSBundle mainBundle] pathForResource:whichWeekFridayString ofType:@"pdf" inDirectory:@"Fridays"];


NSURL *url = [NSURL fileURLWithPath:Documents];
NSLog(@"%@",url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[savedweb loadRequest:request];
NSInteger updatedNumberFriday = whichWeekFriday +1;
[defaults setInteger:updatedNumberFriday forKey:@"DevoWeekFriday"];
[defaults synchronize];

}
if ([dayString isEqualToString:@"Saturday"]) {

}
if ([dayString isEqualToString:@"Sunday"]) {

}

// NSString *Documents = [[NSBundle mainBundle] pathForResource:selectedCountry ofType:@"pdf" inDirectory:@"thepdfpowerpoints"];

[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}

最佳答案

编辑
基于您当前的问题,即在每次加载 View 时始终不增加计数:
我相信让计数数字保持动态更好。因此,您将使用日期字符串来确定哪个文件夹,然后使用以下内容根据一年中的一周动态找出哪个编号的 pdf:

NSDateComponents weekofyearComponents = [[NSCalendar(calendarIdentifier: NSCalendarIdentifierISO8601)] components:(NSCalendarUnitWeekOfYear ) fromDate: [NSDate date]];
NSInteger whichWeekOfYear = weekofyearComponents.weekOfYear

原答案
我可以看到几种方法可以做到。
一种方法就像您建议的那样,将当天的日期提供给 PDF 的那一天。这是 getting the day of the year in iOS swift 的链接.
另一种方法是获取 week of the year number (1-52) 然后 get the day of the week number因此,您可以按一年中的一周在目录中组织 pdf,然后在其中包含一周中每一天的消息。
我的最后一条评论是,对于 iOS 应用程序中的当天消息,PDF 听起来有点沉重或笨拙。一个文本文件或一些轻量级数据库或一些缓存可能就足够了,这些缓存通过 webrequest 更新以刷新和存储当天的当前和接下来的几条消息。我绝对缺乏上下文,所以我不会否认 PDF 可能正是您所需要的这一事实。

关于ios - 在特定日期显示特定文件的建议逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66336432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com