应该相等的字符串比较不相等

时间:2014-05-29 18:55:04

标签: ios objective-c string cocoa-touch nsstring

我想构建一个每天都显示字符串的应用,但我不知道如何发现新的一天。

我尝试将字符串设置为initialString,记录上次打开应用的日期,以及记录当前时间的字符串nowString。如果initialString != nowString,则会在应用中的UILabel中显示新字符串,并将initialString更新为nowString。如果它们相同,则没有任何反应。

Heres是我的代码;编译器说两个字符串即使它们都不相同。

实施档案:

#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

我对这些字符串的比较有什么问题?

1 个答案:

答案 0 :(得分:1)

UIApplicationDelegate' applicationSignificantTimeChange:会在新的一天开始时通知您。

相关问题