2010-02-26 90 views
0

在我的tableView的numberOfRowInSection中,我尝试将self.date中的myDate与self.allDates中的dateInFiche进行比较。比较值不起作用(Objective-C)

我的约会是这样的:1986年12月5日
,for语句dateinFiche将这些值:
1986年12月5日
1986年12月5日
13-05- 1986年
18-05-1986

当如果发生语句的第一个日期是相同的,所以它会递增numberofRows,二是也同样的,但问题是如果不想在这一点上执行。
我使用的断点和值是相同的,但如果不工作。任何想法?


(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

NSString *myDate = [self.date objectAtIndex:section]; int numberOfRows = 0; for (int i = 0; i [self.allDates count]; i++) { Date *dateFiche = (Date *)[self.allDates objectAtIndex:i]; NSString *dateInFiche = [[dateFiche sDate] substringWithRange: NSMakeRange(0,10)]; if (dateInFiche == myDate) { numberOfRows = numberOfRows+1; } } return numberOfRows; }

回答

4

嗯,这是行不通的,因为你是一个指针直接与指针比较的NSString对象到另一个NSString对象。这类似于:

void *someBuf = calloc (100, 1); 
void *anotherBuf = calloc (100, 1); 

memcpy (someBuf, "test", 4); 
memcpy (anotherBuf, "test", 4); 

if (someBuf == anotherBuf) 
{ 
    // won't branch even though their contents are identical 
    ... 

你不能比较的指针本身,你要比较它们的内容。你可以用NSString的isEqualToString:来做到这一点。

if ([firstString isEqualToString:secondString]) 
{ 
    // will branch only if the strings have the same content 
    ... 
2

if语句使用==两个字符串直接比较。这只会比较指针的值,而不是字符串的实际内容。试试这个:

if ([dateInFiche isEqualToString:myDate]) { 
    .... 
+0

感谢多数民众赞成我正在寻找〜 – ludo 2010-02-26 05:54:55