I want to build an app that shows a string every day, but I don't know how to detect a new day has come.
I tried setting a string as initialString
which records the day of the last time you opened the app, and a string as nowString
which records the current time. If initialString != nowString
, it would show a new string in the UILabel
in the app and update the initialString
to the same as nowString
. If they're the same, then nothing happens.
Heres is my code; the compiler says both strings are not the same even when they are.
Implementation file:
#import "QuoteViewController.h"
#import "Quotes.h"
@interface QuoteViewController ()
@end
@implementation QuoteViewController
@synthesize View1;
@synthesize View2;
//Will be used in the future.
//@synthesize View3;
- (void)viewDidLoad
{
// Do any additional setup after loading the view, typically from a nib.
[super viewDidLoad];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View1"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"View2"]];
self.quotes = [[Quotes alloc] init];
self.now = [[NSDate alloc] init];
self.initialString = @"1";
[self timer];
}
#pragma mark - timer
- (void) timer {
//This repasts the method checkDate every one second
[NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(checkDate)
userInfo:nil
repeats:YES];
}
#pragma mark - checkDate
- (void) checkDate {
//Date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd"];
NSString *nowString = [dateFormatter stringFromDate:self.now];
NSLog(@"self.initialString is: %@", self.initialString);
NSLog(@"now is: %@", nowString);
//both Strings are the same but the compiler still says they're not the same and keep calling the method [showQuote]
if (self.initialString != nowString) {
self.initialString = nowString;
[self showQuote];
NSLog(@"AFTER THE METHOD THE STRING IS %@", self.initialString);
}
else { NSLog(@"IT'S THE SAME");}
}
#pragma mark - showQuote
- (void) showQuote {
self.quoteLabel.text = [self.quotes randomQuote];
}
@end
What is the problem with my comparison of these strings?