2010-02-26 123 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