Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 比较两个字符串,如date_Iphone_Ios_Nsdate_Nsdateformatter_Nsdatepicker - Fatal编程技术网

Iphone 比较两个字符串,如date

Iphone 比较两个字符串,如date,iphone,ios,nsdate,nsdateformatter,nsdatepicker,Iphone,Ios,Nsdate,Nsdateformatter,Nsdatepicker,需要比较NSDate类型的两个变量。 一个是当前日期,另一个是用户选择的日期 用户选择日期: UIDatePicker *datePicker; // declared in h file -(void)dateChange:(id)sender{ NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"dd-MM-yyy"]; dateLabel.text = [NSString stringWi

需要比较NSDate类型的两个变量。 一个是当前日期,另一个是用户选择的日期

用户选择日期:

UIDatePicker *datePicker;   // declared in h file

-(void)dateChange:(id)sender{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd-MM-yyy"];
dateLabel.text = [NSString stringWithFormat:@"%@", [df stringFromDate:datePicker.date]];

userDebtInfo.duration = dateLabel.text;}
验证方法:

-(IBAction) validation:(id)sender{
NSDate *today = [NSDate date];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSString *dateString = [dateFormat stringFromDate:today];

[dateFormat release];

 if ((userDebtInfo.debt_code == userTmp.debt_code) && ([userDebtInfo.duration compare:dateString]== NSOrderedDescending)))
            [bl updateUserMoneyValidation:userDebtInfo];  
 else
      [bl saveUserMoneyValidation:userDebtInfo];}
我已经测试并正在比较两个字符串。有人能帮我提供一些关于这种情况的建议吗? 我想比较所选日期是否在当前日期之后,以便插入或更新数据库


谢谢大家!

如果已经有两个NSDate类型的对象,可以使用NSDate比较方法之一。例如,
[NSDate compare:]
返回一个
NSComparisonResult
(这是由
[NSString compare:]
返回的相同枚举)。

如果已经有两个NSDate类型的对象,可以使用其中一个NSDate比较方法。例如,
[NSDate compare:]
返回一个
NSComparisonResult
(这与
[NSString compare:]
返回的枚举相同)。

您可以使用NSDatecompare函数

if([firstDate compare:secondUpdate] == NSOrderedAscending){
       //This will return YES if secondUpdate is the recent date between the two dates
}
传感器检测是比较结果的常数 以下是您可以比较的其他常数:

enum {
 NSOrderedAscending = -1,
 NSOrderedSame,
 NSOrderedDescending
};
typedef NSInteger NSComparisonResult;

您可以使用NSDate比较功能

if([firstDate compare:secondUpdate] == NSOrderedAscending){
       //This will return YES if secondUpdate is the recent date between the two dates
}
传感器检测是比较结果的常数 以下是您可以比较的其他常数:

enum {
 NSOrderedAscending = -1,
 NSOrderedSame,
 NSOrderedDescending
};
typedef NSInteger NSComparisonResult;

为什么人们总是写
[nsstringwithformat:@“%@”,otherString]
[nsstringwithstring:otherString]
和类似的多余的、浪费时间的调用?@H2CO3很好,伙计。我刚刚发现我自己也有一些这样的问题,为什么人们总是写
[nsstringwithformat:@“%@”,otherString]
[nsstringwithstring:otherString]
和类似的多余的、浪费时间的调用?@H2CO3很好,伙计。我刚发现我自己也有一些