對不起,但我仍然在努力使此代碼工作。它適用於使用2位數字但使用單個數字號碼崩潰的情況。如何驗證NSString * secDigit是否有值或爲空。我希望我的問題很清楚。提前致謝。如何驗證字符串是否有空值?
NSString *depositOverTotalRwy = [NSString stringWithFormat:@"%@", [deposit text]];
NSArray *components = [depositOverTotalRwy
componentsSeparatedByString:@"/"];
NSString *firstThird = [components objectAtIndex:0];
char firstChar = [firstThird characterAtIndex:0];
char secChar = [firstThird characterAtIndex:1];
NSString *firstDigit = [NSString stringWithFormat:@"%c",firstChar];
NSString *secDigit = [NSString stringWithFormat:@"%c", secChar];
NSLog(@" i'm %@", firstDigit);
NSLog(@" i'm %@", secDigit);
if ([firstDigit isEqualToString: @"1"]) {
[email protected]"wet";
NSLog(@"wet");
}
if ([firstDigit isEqualToString: @"2"]) {
[email protected]"wet";
NSLog(@"snow");
}
if ([firstDigit isEqualToString: @"3"]) {
[email protected]"wet";
NSLog(@"ice");
}
if ([secDigit isEqualToString: @"1"]) {
[email protected]"wet";
NSLog(@"wet");
}
if ([secDigit isEqualToString: @"2"]) {
[email protected]"snow";
NSLog(@"snow");
}
if ([secDigit isEqualToString: @"3"]) {
[email protected]"ice";
NSLog(@"ice");
}
感謝所有的你.....我在這裏的代碼(現在的工作):
NSString *depositOverTotalRwy = [NSString stringWithFormat:@"%@", [deposit text]];
NSArray *components = [depositOverTotalRwy
componentsSeparatedByString:@"/"];
NSString *firstThird = [components objectAtIndex:0];
char firstChar = [firstThird characterAtIndex:0];
NSString *firstDigit = [NSString stringWithFormat:@"%c",firstChar];
NSLog(@" i'm %@", firstDigit);
if ([firstDigit isEqualToString: @"1"]) {
[email protected]"wet";
NSLog(@"wet");
}
if ([firstDigit isEqualToString: @"2"]) {
[email protected]"wet";
NSLog(@"snow");
}
if ([firstDigit isEqualToString: @"3"]) {
[email protected]"wet";
NSLog(@"ice");
}
if ([firstThird length] >1) {
char secChar = [firstThird characterAtIndex:1];
NSString *secDigit = [NSString stringWithFormat:@"%c", secChar];
if ([secDigit isEqualToString: @"1"]) {
[email protected]"wet";
NSLog(@"wet");
}
if ([secDigit isEqualToString: @"2"]) {
[email protected]"snow";
NSLog(@"snow");
}
if ([secDigit isEqualToString: @"3"]) {
[email protected]"ice";
NSLog(@"ice");
}
}
確定,但後來我已經設置像一個標籤這個dep.text = [NSString stringWithFormat:@「1/3%@%@」,firstDigit,secDigit];我可以用char來做,還是我需要一個字符串? – mat 2011-05-16 11:43:26
@mattia:你也可以使用char:'dep.text = [NSString stringWithFormat:@「1/3%c%c」,firstChar,secChar];' – DarkDust 2011-05-16 12:00:33