gpt4 book ai didi

objective-c - 在 Objective C (Xcode) 中比较两个字符串时如何使用 "If"语句?

转载 作者:可可西里 更新时间:2023-11-01 05:01:02 25 4
gpt4 key购买 nike

我试图在一周中的每一天显示不同的网站。我使用 NSDateFormatter 创建了一个仅包含星期几的 NSString。然后,我为一周中的每一天创建了额外的字符串。我在“IF”语句中比较两者...所以如果字符串(天数)相等,它将执行 if 语句中的函数。如果不是,它检查下一条语句。现在它适用于星期一的第一个声明,但是当我更改 iPhone 上的日期以模拟一周中的其他日子时,它就不起作用了。我的代码在下面!

NSDateFormatter *dayofweekformatter = [[NSDateFormatter alloc] init];
[dayofweekformatter setDateFormat:@"cccc"];

NSString *DayOfWeek = [dayofweekformatter stringFromDate:[NSDate date]];


NSString *Monday = @"Monday";
NSString *Tuesday = @"Tuesday";
NSString *Wednesday = @"Wednesday";
NSString *Thursday = @"Thursday";
NSString *Friday = @"Friday";
NSString *Saturday = @"Saturday";
NSString *Sunday = @"Sunday";



if ([DayOfWeek isEqualToString:Monday])

{ // Webview code

NSString *urlAddress = @"http://www.google.com";

//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];

//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

//Load the request in the UIWebView.
[webview loadRequest:requestObj];


}

else if (dateToday == Tuesday)

{ // Webview code

NSString *urlAddress = @"http://www.cnn.com";

//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];

//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

//Load the request in the UIWebView.
[webview loadRequest:requestObj];

最佳答案

更好的解决方案如下,使用工作日的索引来确定您的 url:

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
NSInteger weekday = [components weekday];
NSString *urlString;
switch(weekday){
case 1: // sunday
urlString = @"http://google.com";
break;
case 2:
urlString = @"http://twitter.com";
break;
case 3:
urlString = @"http://facebook.com";
break;
case 4:
urlString = @"http://yahoo.com";
break;
case 5:
urlString = @"http://mashable.com";
break;
case 6:
urlString = @"http://bbc.co.uk";
break;
case 7: // saturday
urlString = @"http://stackoverflow.com";
break;
default:
urlString = @"http://google.com?q=weekday+is+never+this!";
break;
}

NSURL *url = [NSURL URLWithString:urlString];

NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

//Load the request in the UIWebView.
[webview loadRequest:requestObj];

要在您询问评论时刷新您的检查,您可以这样做:

在您的应用程序委托(delegate)文件中,将这一行添加到 applicationDidBecomeActive: 方法

- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshDateCheck" object:nil];
}

在你的类里面你正在做你的日期检查,在 init 方法中添加这一行来监听当应用程序退出后台时发送的任何刷新通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethod) name:@"refreshDateCheck" object:nil];

最后将您的日期检查代码移至此方法,只要收到通知就会调用该方法:

-(void)myMethod{
/*
Your other code goes in here
*/
}

关于objective-c - 在 Objective C (Xcode) 中比较两个字符串时如何使用 "If"语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8796927/

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