2011-05-16 42 views
1

對不起,但我仍然在努力使此代碼工作。它適用於使用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"); 
} 
} 

回答

1

我猜你的代碼崩潰在這一行:

char secChar = [firstThird characterAtIndex:1]; 

這是因爲您嘗試訪問字符串邊界之外的字符。你需要首先檢查字符串的長度,以防止這種情況:

if ([firstThird count] > 1) { 
    // String has 2 or more characters, do all the stuff that involves 
    // a second character. 
    char secChar = [firstThird characterAtIndex:1]; 
    NSString *secDigit = [NSString stringWithFormat:@"%c", secChar]; 

    if ([secDigit isEqualToString: @"1"]) { 
     [email protected]"wet"; 
     NSLog(@"wet"); 
    } 
} 

但我也想推薦到這裏不是使用一個NSString,因爲你已經有一個char。只要做這樣的事情:

if ([firstThird count] > 1) { 
    // String has 2 or more characters, do all the stuff that involves 
    // a second character. 
    char secChar = [firstThird characterAtIndex:1]; 

    if (secChar == '1') { 
     [email protected]"wet"; 
     NSLog(@"wet"); 
    } 
} 
+0

確定,但後來我已經設置像一個標籤這個dep.text = [NSString stringWithFormat:@「1/3%@%@」,firstDigit,secDigit];我可以用char來做,還是我需要一個字符串? – mat 2011-05-16 11:43:26

+0

@mattia:你也可以使用char:'dep.text = [NSString stringWithFormat:@「1/3%c%c」,firstChar,secChar];' – DarkDust 2011-05-16 12:00:33

1

我認爲這是你在找什麼:

char secChar; 
if(firstThird.length > 1) 
{ 
    secChar = [firstThird characterAtIndex:1]; 
} 
1

您可以使用NSString長度檢查字符串中的字符數。並修改代碼 作爲

NSString *depositOverTotalRwy = [NSString stringWithFormat:@"%@", @"23"]; 
NSArray *components = [depositOverTotalRwy 
         componentsSeparatedByString:@"/"]; 
NSString *firstThird = [components objectAtIndex:0]; 


unichar firstChar; 
unichar secChar; 

if([firstThird length]>1){ 
    firstChar = [firstThird characterAtIndex:0]; 
    secChar = [firstThird characterAtIndex:1]; 
} else { 
    firstChar = [firstThird characterAtIndex:0]; 
    secChar = 0; 
} 

switch (firstChar) { 
    case '1': /* Do your stuff*/break; 
    case '2': /* Do your stuff*/break; 
    case '3': /* Do your stuff*/break;   
    default: 
     break; 
} 

switch (secChar) { 
    case '1': /* Do your stuff*/break; 
    case '2': /* Do your stuff*/break; 
    case '3': /* Do your stuff*/break;   
    default: 
     break; 
} 

,您可以改用字符的單字符。並且可以執行檢入切換語句。 如果您使用char,則會從unichar轉換爲字符,對於某些字符,您可能會失去實際價值。因此它是安全的使用單字符...

如果你想單字符 NSString * stringChar = [NSString StringWithFormat:@"%C",unicharVariable];

那轉換爲字符串簡單的代碼吧...

+0

+1注意到'characterAtIndex:'實際返回一個字符'unichar'並提供一個很好的例子。 Minor nit-pick:'stringWithFormat:'以小寫字母開始。 – DarkDust 2011-05-17 06:26:53

相關問題