0
NSString * str = @"ABCDEFGHILMN";
NSDictionary *dictOdd = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt:0], @"A",
[NSNumber numberWithInt:1], @"B",
[NSNumber numberWithInt:2], @"C",
[NSNumber numberWithInt:3], @"D",
...............................
nil];
NSDictionary *dictEven = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt:1], @"A",
[NSNumber numberWithInt:0], @"B",
[NSNumber numberWithInt:5], @"C",
[NSNumber numberWithInt:7], @"D",
...............................
nil];
int sum = 0;
for (int i=1; i < [str length]; i++){
if(i % 2){
sum += [dictPair objectForKey:str[i]];
}else{
sum += [dictEven objectForKey:str[i]];
}
}
我已經創建了2個字典存儲int數字/字母,每個字母都有一個int值。NSDictionary和循環
我會使int數與我的str值相關的總和,但我不知道如何分配objectForKey:str [i]];
從技術上講,應該是'%C',因爲'characterAtIndex:'返回一個unichar。 – Chuck
謝謝。對於字符串的字符格式,我有點生疏 – utahwithak