How can I verify if a String has a null value?
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:@"%@",开发者_C百科 [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");
}
}
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");
}
}
I think this is what you are looking for:
char secChar;
if(firstThird.length > 1)
{
secChar = [firstThird characterAtIndex:1];
}
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
}
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 ...
精彩评论