如何验证字符串是否为空值?

发布于 2024-11-07 19:47:22 字数 2347 浏览 2 评论 0原文

抱歉,但我仍在努力使该代码正常工作。如果使用 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"]) {
    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");
}
}

sorry but i'm still struggling to make this code working. It works if with a 2 digits number but it crashes with a single digit number. How can I verify if the NSString *secDigit has a value or is null. I hope my question is clear. Thanks in advance.

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");
}

thanks to all of you..... here my code (working now):

   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");
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

裂开嘴轻声笑有多痛 2024-11-14 19:47:22

我猜您的代码在这一行中崩溃了:

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");
    }
}

I guess you code crashes in this line:

char secChar = [firstThird characterAtIndex:1];

This is because you try to access a character outside of the string bounds. You need to guard against this by checking the length of the string first:

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");
    }
}

But I'd also like to recommend to not use an NSString here, as you already have a char. Just do something like this:

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");
    }
}
懷念過去 2024-11-14 19:47:22

我认为这就是您正在寻找的:

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

I think this is what you are looking for:

char secChar;
if(firstThird.length > 1)
{
    secChar = [firstThird characterAtIndex:1];
}
何以畏孤独 2024-11-14 19:47:22

根据此

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
  }

According to this

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

NSString "Raises an NSRangeException if index lies beyond the end of the receiver"

So, your code:

char secChar = [firstThird characterAtIndex:1];

Is the problem (you should see that in the debugger Console)

Check the length first with

  if ([firstThird length] < 2) {
       // handle the case where it is one digit
  }
柳絮泡泡 2024-11-14 19:47:22

您可以使用 NSString length 检查字符串中的字符数。并修改你的代码
因为

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。并且可以在 switch 语句中执行检查。
如果您使用 char,则会从 unichar 到 char 进行转换,对于某些字符,您可能会丢失实际值。所以使用 unichar 是安全的...

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

就是这样...

You can check number of characters in a string using NSString length. and modify your code
as

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;
}

you can use unichar instead of char. And can perform check in switch statements.
If you use char, a casting is done from unichar to char and for some characters you may lose actual value. So it is safe to use unichar...

If you want to convert unichar to string simply code
NSString * stringChar = [NSString StringWithFormat:@"%C",unicharVariable];

Thats it ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文