2012-12-11 40 views
0

你好是我使用加信到字符串目標C若跌破聲明

currentCardDisplayed = [NSMutableString stringWithFormat:@"%@.png", [[cardsShuffled objectAtIndex:currentCard-1] substringToIndex:[[cardsShuffled objectAtIndex:currentCard-1] length]-1]]; 

NSString *rule; 

if([[currentCardDisplayed stringByAppendingString:@"e"] isEqualToString:currentCardName]){ 
    rule = @"drink"; 
}else if([[currentCardDisplayed stringByAppendingString:@"2"]isEqualToString:currentCardName]){ 
    rule = @"drink2"; 
}else if([[currentCardDisplayed stringByAppendingString:@"3"]isEqualToString:currentCardName]){ 
    rule = @"drink3"; 
}else if([[currentCardDisplayed stringByAppendingString:@"4"]isEqualToString:currentCardName]){ 
    rule = @"drink4"; 
}else if([[currentCardDisplayed stringByAppendingString:@"5"]isEqualToString:currentCardName]){ 
    rule = @"drink5"; 
}else if([[currentCardDisplayed stringByAppendingString:@"6"]isEqualToString:currentCardName]){ 
    rule = @"drink6"; 
}else if([[currentCardDisplayed stringByAppendingString:@"7"]isEqualToString:currentCardName]){ 
    rule = @"drink7"; 
}else if([[currentCardDisplayed stringByAppendingString:@"8"]isEqualToString:currentCardName]){ 
    rule = @"drink8"; 
}else if([[currentCardDisplayed stringByAppendingString:@"9"]isEqualToString:currentCardName]){ 
    rule = @"drink9"; 
}else if([[currentCardDisplayed stringByAppendingString:@"0"]isEqualToString:currentCardName]){ 
    rule = @"drink10"; 
}else if([[currentCardDisplayed stringByAppendingString:@"k"]isEqualToString:currentCardName]){ 
    rule = @"drinkjack"; 
}else if([[currentCardDisplayed stringByAppendingString:@"n"]isEqualToString:currentCardName]){ 
    rule = @"drinkqueen"; 
}else if([[currentCardDisplayed stringByAppendingString:@"g"]isEqualToString:currentCardName]){ 
    rule = @"drinkking"; 
} 
NSLog(@" %@ ", currentCardDisplayed); 
NSLog(@" %@ ", currentCardName); 
NSLog(@"%@", rule); 
NSLog(@" %@", [cardsShuffled objectAtIndex:currentCard-1]); 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Rules" message:rule delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil]; 

[alert show]; 

我試圖添加到末尾的字母沒有到達那裏的代碼。我首先刪除了一個字母,但是然後在我的if語句中,我想檢查是否向該變量添加了另一個字母,它等於另一個字符串。

任何人都可以看到我錯了

下面是從

2012-12-11 20:06:50.853 Kings_1.03[4915:c07] club.png 
2012-12-11 20:06:50.855 Kings_1.03[4915:c07] club9.png 
2012-12-11 20:06:50.857 Kings_1.03[4915:c07] (null) 
2012-12-11 20:06:50.858 Kings_1.03[4915:c07] club9 

任何幫助表示讚賞

的nslogs輸出樣品感謝你

回答

1

您必須添加.png在你做if測試後。用你的例子,你現在的做法是將9添加到club.png中,這會讓club.png9不是club9,所以你的測試永遠不會滿足。

最簡單的方法可能會不追加在第一行巴紐,然後在你的if語句,追加兩個字母/數字和.png,所以:

}else if([[currentCardDisplayed stringByAppendingString:@"9.png"]isEqualToString:currentCardName]){ 
    rule = @"drink9"; 
+0

笏做你的意思,你能舉出一個你認爲不錯的例子嗎?或者這是你的意思,如果([[currentCardDisplayed stringByAppendingString:@「e.png」] isEqualToString:currentCardDisplayed]){0} {0}條規則@ @「drink」; } –

+0

@AlanFletcher,不,只要在第一行中定義currentCardDisplayed時不添加.png,那麼測試就可以工作。 – rdelmar

+0

@AlanFletcher,很抱歉,不會因爲您將它與具有.png的字符串進行比較而無法正常工作。 – rdelmar