开发者

An Important Question about NSString

The problem is after i press 2 buttons the App crashes and I can't figure out why

Button1 is wired to button1 Button2 is wired to button2 ... Button5 is wired to button5

What am I doing wrong?

Basically I want to check if the sequence of #'s is punched in correctly (55235)

In my AppDelegate.H file, I've defined a variable called

NSString* my开发者_JAVA技巧EasterEgg;

In AppDelegate.M, I have 5 buttons that are correctly wired and each button has it's own method

-(IBAction)button1:(id)sender
{
if(CFStringCompare((CFStringRef)myEasterEgg, (CFStringRef)@"52235", 1) == 0)
{
myEasterEgg = @"";
} else {
myEasterEgg = [myEasterEgg stringByAppendingString:@"1"];
}
}

-(IBAction)button2:(id)sender
{
if(CFStringCompare((CFStringRef)myEasterEgg, (CFStringRef)@"52235", 1) == 0)
{
myEasterEgg = @"";
} else {
myEasterEgg = [myEasterEgg stringByAppendingString:@"2"];
}
}

-(IBAction)button3:(id)sender
{
if(CFStringCompare((CFStringRef)myEasterEgg, (CFStringRef)@"52235", 1) == 0)
{
myEasterEgg = @"";
} else {
myEasterEgg = [myEasterEgg stringByAppendingString:@"3"];
}
}

-(IBAction)button4:(id)sender
{
if(CFStringCompare((CFStringRef)myEasterEgg, (CFStringRef)@"52235", 1) == 0)
{
myEasterEgg = @"";
} else {
myEasterEgg = [myEasterEgg stringByAppendingString:@"4"];
}
}

-(IBAction)button5:(id)sender
{
if(CFStringCompare((CFStringRef)myEasterEgg, (CFStringRef)@"52235", 1) == 0)
{
myEasterEgg = @"";
} else {
myEasterEgg = [myEasterEgg stringByAppendingString:@"5"];
}
}

-(void)viewDidLoad
{
myEasterEgg = [[NSString alloc] initWithString:@""];
}


You are leaking your original string, and replacing it with new values without retaining them. The crash happens because you’re calling [myEasterEgg stringByAppendingString:…] on a dangling pointer, that is, a variable which no longer refers to any object. See the Memory Management Programming Guide for more information.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜