<分区>
<分区>
我想构建一个每天显示一个字符串的应用程序,但我不知道如何检测新的一天已经到来。
我尝试将字符串设置为 initialString
来记录您上次打开应用程序的日期,并将字符串设置为 nowString
来记录当前时间。如果 initialString != nowString
,它会在应用程序的 UILabel
中显示一个新字符串,并将 initialString
更新为与 相同现在字符串
。如果它们相同,则什么也不会发生。
这是我的代码;编译器说两个字符串不一样,即使它们是一样的。
执行文件:
#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
我比较这些字符串有什么问题?
最佳答案
UIApplicationDelegate 的 applicationSignificantTimeChange:新的一天开始时会通知您。
关于ios - 应该相等的字符串比较不相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23940868/
相关文章:
objective-c - 填充 map View 时如何设置两种不同的图钉颜色
c# - HTMLagilitypack 没有删除所有 html 标签 我怎样才能有效地解决这个问题?
ios - NSWidgetExtensionContext openURL Swift
java - 如何在 Java 服务器中验证 Twitter OAuth 凭据(包括电子邮件)
ios - 如何在 swift 中使用文本字段文本 "Amount"向表格 View 添加额外的行
c - 是否有一个标准的 C 函数来计算字符串长度达到某个限制?