How to use "If" statements when comparing two strings in Objective-C (Xcode)?

I try to display a different site on every day of the week. I created an NSString that contains only the current day of the week using NSDateFormatter. Then I created additional lines for each day of the week. I compare them in the expression "IF" ... so if the lines (days) are equal, it will execute the function in the if statement. if not, he checks the following statement. Now this will work for the first application on Monday, but when I change the date on my iPhone to simulate other days of the week, it will not work. My code is below!

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]; 
+4
source share
6 answers

The best solution would be: using the day of the week index to determine your 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]; 

To update your checks, as you requested in the comment, you can do this:

In the application delegation file, add this line to applicationDidBecomeActive: method

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

In your class, you are checking the date; in the init method, add this line to listen for any update notifications sent when the application exits the background:

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

Finally, follow the date verification code to this method, which is called whenever a notification is received:

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

You used isEqualToString for the first check, which is good, but then do the following comparison using == .
Use else if ([dateToday isEqualToString:tuesday])

In addition, as an additional note, your variable name should begin with a lowercase letter.

+3
source

You can use the date format @ "E" to get the numeric day of the week. Now you are not tied to a particular line of the language.

 NSDateFormatter *dayofweekformatter = [[NSDateFormatter alloc] init]; [dayofweekformatter setDateFormat:@"E"]; NSString *DayOfWeek = [dayofweekformatter stringFromDate:[NSDate date]]; NSInteger weekDay = [DayOfWeek integerValue]; switch (weekDay) { case 1: // Sunday break; case 2: // Monday break; default: break; } 
+3
source

I am disappointed with everyone who answered so far.

Yes - you correctly indicate the difference between pointer equality and equality.

But you do not indicate that the questionnaire goes wrong with testing on weekdays - that is what question he asked.

Another solution to the real problem:

You have a date - you can turn this into an NSDateComponents - which has a weekday method that returns an NSInteger, which in the case of a Gregorian returns 1 on Sunday, 2 on Monday, etc.

For example, this is taken directly from the calendar section of the cost estimate for Apple documents.

 NSDate *today = [NSDate date]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today]; NSInteger day = [weekdayComponents day]; NSInteger weekday = [weekdayComponents weekday]; 

Now you can simply use the switch statement.

+3
source

Spot the difference

 if ([DayOfWeek isEqualToString:Monday]) // 1 if (dateToday == Tuesday) // 2 
  • In the first case, you call the isEqualToString: method, so NSString compares the contents of the string for equality.

  • In the second case, you use == , this is a pointer comparison. The Tuesday pointer points to another object returned by [dayofweekformatter stringFromDate:[NSDate date]];

Therefore, make sure that you use the correct comparison methods for the type you are dealing with.

Also, do not compare different variables. In the first if you are comparing DayOfWeek in the second if , which you are comparing dateToday .

Refresh

It looks like you might have come to Objective-C from a different language, so it might be worth compiling Apple documents for the Coding Guide, it just gives some quick examples of how things are usually called in Objective-C

+2
source

You do not handle any other case than Monday. You need to add the same code as before (with isEqualToString instead of == ):

 if ([DayOfWeek isEqualToString:Monday]) { /* code here */ } else if ([DayOfWeek isEqualToString:Tuesday]) { /* code here */ } else if ([DayOfWeek isEqualToString:Wednesday]) { /* code here */ } else if (...) 
+1
source

Source: https://habr.com/ru/post/1390141/


All Articles