问题是在我按下2个按钮后,应用程序崩溃,我无法找出原因
Button1连接到button1 Button2连接到button2 ... Button5连接到button5
我做错了什么?
基本上我想检查#的序列是否正确打孔(55235)
在我的AppDelegate.H文件中,我定义了一个名为
的变量NSString * myEasterEgg;
在AppDelegate.M中,我有5个正确连线的按钮,每个按钮都有自己的方法
-(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:@""];
}
答案 0 :(得分:1)
您正在泄漏原始字符串,并将其替换为新值而不保留它们。发生崩溃的原因是你在悬空指针上调用[myEasterEgg stringByAppendingString:…]
,即一个不再引用任何对象的变量。有关详细信息,请参阅Memory Management Programming Guide。