0

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?

jscs
  • 63,694
  • 13
  • 151
  • 195
Henry Ngan
  • 572
  • 1
  • 4
  • 24
  • I edited this and closed it as a duplicate because the problem with your specific code is not particularly related to your larger goal of being notified of date changes. This post was really two questions in one: "What's wrong with the string comparison?" and "How can I take an action each day?" Since you've recieved an answer for the second, you are welcome to edit your question to just ask about that, but the code would then be irrelevant, so you should remove it. If you make that edit, leave me a comment and I will re-open the question. – jscs May 29 '14 at 19:11

1 Answers1

1

UIApplicationDelegate's applicationSignificantTimeChange: will inform you when a new day begins.

src
  • 205
  • 1
  • 8
  • Keep in mind that this is called for several different reasons, not just when a new day begins. – rmaddy May 29 '14 at 19:24