如何验证String是否具有空值?

时间:2011-05-16 11:23:39

标签: iphone objective-c cocoa-touch nsstring

抱歉,但我仍在努力使这段代码正常工作。它有效,如果有一个2位数字,但它与一位数字崩溃。如何验证NSString * secDigit是否具有值或为null。我希望我的问题很明确。提前谢谢。

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"]) {
    firstDigit=@"wet";
    NSLog(@"wet");
}
if ([firstDigit  isEqualToString: @"2"]) {
    firstDigit=@"wet";
    NSLog(@"snow");

}
if ([firstDigit  isEqualToString: @"3"]) {
    firstDigit=@"wet";
    NSLog(@"ice");
    }

if ([secDigit  isEqualToString: @"1"]) {
    secDigit=@"wet";
    NSLog(@"wet");
}
if ([secDigit  isEqualToString: @"2"]) {
    secDigit=@"snow";

    NSLog(@"snow");
}
if ([secDigit  isEqualToString: @"3"]) {
    secDigit=@"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"]) {
    firstDigit=@"wet";
    NSLog(@"wet");
}
if ([firstDigit  isEqualToString: @"2"]) {
    firstDigit=@"wet";
    NSLog(@"snow");

}
if ([firstDigit  isEqualToString: @"3"]) {
    firstDigit=@"wet";
    NSLog(@"ice");
    }
if ([firstThird length] >1) {
    char secChar = [firstThird characterAtIndex:1];
    NSString *secDigit = [NSString stringWithFormat:@"%c", secChar];


if ([secDigit  isEqualToString: @"1"]) {
    secDigit=@"wet";
    NSLog(@"wet");
}
if ([secDigit  isEqualToString: @"2"]) {
    secDigit=@"snow";

    NSLog(@"snow");
}
if ([secDigit  isEqualToString: @"3"]) {
    secDigit=@"ice";
    NSLog(@"ice");
}
}

4 个答案:

答案 0 :(得分:1)

我认为这就是你要找的东西:

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

答案 1 :(得分:1)

根据这个

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

NSString“如果索引超出接收者的末尾,则引发NSRangeException”

所以,你的代码:

char secChar = [firstThird characterAtIndex:1];

问题是(您应该在调试器控制台中看到)

首先用

检查长度
  if ([firstThird length] < 2) {
       // handle the case where it is one digit
  }

答案 2 :(得分: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"]) {
        secDigit=@"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') {
        secDigit=@"wet";
        NSLog(@"wet");
    }
}

答案 3 :(得分: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;
}

你可以使用unichar而不是char。并且可以执行check in switch语句。  如果使用char,则从unichar到char进行转换,对于某些字符,您可能会失去实际值。所以使用unichar是安全的......

如果您想将unichar转换为字符串,只需编码 NSString * stringChar = [NSString StringWithFormat:@"%C",unicharVariable];

多数民众赞成......