开发者

Cocoa - Debugging Help

开发者 https://www.devze.com 2023-01-06 01:29 出处:网络
Ok for some reason my code wont play nice and I\'m too new to cocoa to figure this out on my own.. when the send button is pressed it is meant to run the createEmail method. But it says

Ok for some reason my code wont play nice and I'm too new to cocoa to figure this out on my own..

when the send button is pressed it is meant to run the createEmail method. But it says

GDB: Program received signal: "EXC_BAD_ACCESS". when the button is pressed.

#import "Controller.h"

@implementation Controller
-(IBAction)send:(id)sender{
 [self createEmail];
}

-(void)createEmail{
 NSString *number  = [numfield stringValue]; 
 NSString *carrier = [carrierfield stringValue];
 NSString *carrierTag;

 [carrier lowercaseString]; //make all lowercase 

 //set carrierTag based on what carrier it is
 if ([carrier isEqualToString:@"verizon"]) {
  carrierTag = @"@vtext.com";
 }
 if ([carrier isEqualToString:@"at&t"]) {
  carrierTag = @"@txt.att.net";
 }
 if ([carrier isEqualToString:@"nextel"]) {
  carrierTag = @"@messaging.nextel.com";
 }
 if ([carrier isEqualToString:@"sprint"]) {
  carrierTag = @"@messaging.sprintpcs.com";
 }
 if ([carrier isEqualToString:@"cingular"]) {
  carrierTag = @"@cingularme.com";
 }
 if ([carrier isEqualToString:@"cingular"]) {
  carrierTag = @"@cingularme.com";
 }
 if ([carrier isEqualToString:@"virgin"]) {
  carrierTag = @"@vmobl.com";
 }
 if ([carrier isEqualToString:@"t-mobile"]) {
  carrierTag = @"@tmomail.ne开发者_JAVA技巧t";
 }

 //Concatenate number and carrierTag to create an email address
 email = [number stringByAppendingString:carrierTag];
}
@end


Well, I see two possibilities. Either carrier is not set correctly or it's not equal to any of those strings in which case carrierTag is left uninitialised.

In the former case, the exception is probably on the line:

[carrier lowercaseString]

In the latter, it will probably be at:

email = [number stringByAppendingString:carrierTag];

The debugger should provide you with that information, and you should provide it to us as well :-)

In addition, lowercaseString returns another string, it doesn't operate in-place, so you need:

carrier = [carrier lowercaseString];

What you may want to do is set carrierTag to an initially empty string so that nothing is appended if there is no match:

carrierTag = @"";
if ([carrier isEqualToString:@"verizon"]) {
    carrierTag = @"@vtext.com";
}
:
:


In addition to the problems pointed out in the other answers, email appears to be a member variable, and you are assigning it an autoreleased value--meaning it will become garbage as soon as the event loop finishes, and you'll almost certainly crash the next time you access it. You need to retain the value when you assign it.

If you are new to Cocoa, you should absolutely read Apple's introduction to memory management.


If carrier is none of the handled strings, carrierTag is left uninitialized - you should at least set it to nil and test for that before working with it:

NSString *carrierTag = nil;
// ...
if (carrierTag) { 
    // ...

There is also the problem that you expect lowercaseString to mutate the string - but it just returns a new string that is the lowercase version of the string you called it on. Use e.g. the following instead:

carrier = [carrier lowercaseString];

Note that you could greatly simplify the lookup of the carrier-tags by using a dictionary, e.g.:

NSDictionary *carrierTags = [NSDictionary dictionaryWithObjectsAndKeys:
                               @"@vtext.com", @"verizon",
                               // ...
                               nil];
NSString *carrierTag = [carrierTags objectForKey:carrier ? carrier : @""];
0

精彩评论

暂无评论...
验证码 换一张
取 消