我有一个if例程,该例程将一个值与其他两个值进行比较(使用||运算符),如果该语句的结果为YES,则值firstActivity.stopTime应该从null更改为当前时间。但是,即使我的[NSDate date]读数(和我自己的观察结果)似乎确认必须将其评估为NO,它似乎仍可以评估为NSLog,因此没有任何变化。这是代码:-(void) compareActivity{ NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread]; TimedActivity *firstActivity = [TimedActivity MR_findFirstOrderedByAttribute:@"startTime" ascending:NO]; NSLog(@"1 Currently timed activity is %@",firstActivity.name); NSLog(@"2 thisSpec.activityOfInterest is %@",self.thisSpec.activityOfInterest); NSLog(@"3 Currently timed activity stopTime is %@",firstActivity.stopTime); NSLog(@"4 thisSpec.benchmarkActivity is %@",self.thisSpec.benchmarkActivity); if (firstActivity.name == self.thisSpec.activityOfInterest || firstActivity.name == self.thisSpec.benchmarkActivity) { firstActivity.stopTime = [NSDate date]; [localContext MR_saveToPersistentStoreAndWait]; NSLog(@"5 Currently timed activity stopTime is %@",firstActivity.stopTime); } NSLog(@"6 Currently timed activity stopTime is %@",firstActivity.stopTime);}这是从控制台读取的信息:2014-04-28 22:15:06.588 WMDGx[5683:a0b] 1 Currently timed activity is Test 12014-04-28 22:15:06.588 WMDGx[5683:a0b] 2 thisSpec.activityOfInterest is Test 12014-04-28 22:15:06.589 WMDGx[5683:a0b] 3 Currently timed activity stopTime is (null)2014-04-28 22:15:06.589 WMDGx[5683:a0b] 4 thisSpec.benchmarkActivity is Test 22014-04-28 22:15:06.589 WMDGx[5683:a0b] 6 Currently timed activity stopTime is (null)有什么想法或建议吗?谢谢!更新:我因此更新了代码:if ([firstActivity.name isEqualToString:self.thisSpec.activityOfInterest] || [firstActivity.name isEqualToString:self.thisSpec.benchmarkActivity]){ firstActivity.stopTime = [NSDate date]; [localContext MR_saveToPersistentStoreAndWait]; NSLog(@"5 Currently timed activity stopTime is %@",firstActivity.stopTime);}现在,它可以完美运行了!有趣的是,我在前一段时间遇到过同样的问题,实际上尝试过更改代码,但是这次我显然做错了,导致我认为这是错误的方法。非常感谢! (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 您不能使用==比较对象指针,而需要使用isEqual:比较对象[firstActivity.name isEqual: self.thisSpec.activityOfInterest] || [firstActivity.name isEqual: self.thisSpec.benchmarkActivity]如果它们是NSString,则可以使用isEqualToString: (adsbygoogle = window.adsbygoogle || []).push({});
10-08 05:42