如何验证字符串是否为空值?
抱歉,但我仍在努力使该代码正常工作。如果使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我猜您的代码在这一行中崩溃了:
这是因为您尝试访问字符串边界之外的字符。您需要首先检查字符串的长度来防止这种情况:
但我也建议不要在这里使用 NSString,因为您已经有一个
char
。只需做这样的事情:I guess you code crashes in this line:
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:
But I'd also like to recommend to not use an NSString here, as you already have a
char
. Just do something like this:我认为这就是您正在寻找的:
I think this is what you are looking for:
根据此
http:// /developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
NSString“如果索引超出接收器末尾,则引发 NSRangeException”
因此,您的代码:
是问题所在(您应该在调试器控制台中看到)
首先检查长度
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:
Is the problem (you should see that in the debugger Console)
Check the length first with
您可以使用 NSString length 检查字符串中的字符数。并修改你的代码
因为
您可以使用 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
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 ...