简单数据库(BAD_EXEC)

时间:2013-10-16 17:51:49

标签: ios objective-c database label

我正在做一个简单的项目,用iOS编程来证明我的技能。我正在尝试执行一个程序,您可以在其中输入一个字符串,单击“提交”,然后在下面创建一个带有您输入文本的新标签。您提交的字符串越多,您在彼此之间拥有的标签就越多。在我第一次提交之后,没有任何反应。如果我第二次提交字符串,则会收到BAD_EXEC错误。你能帮帮我吗?

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize infoLabel;
@synthesize field;
@synthesize saveButton;

NSString *labelString = @""; // this string will hold all of my strings together
NSString *separator = @"|<->|"; // this is the separator that will separate strings in labelString

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [infoLabel release];
    [field release];
    [saveButton release];
    [super dealloc];
}

- (IBAction)saveButton:(id)sender // when submit button is clicked
{
    if(!([field.text isEqualToString:@""])) // check if anything was entered as the string
    {
        if([labelString isEqualToString:@""]) labelString = field.text; // if nothing is in stored in strings, write the current input
        else // if there already are some strings in there
        {
            NSString *temp = @"";
            temp = [NSString stringWithFormat:@"%@%@%@", labelString, separator, field.text]; // prepare a new labelString to hold all my old strings + new one
            labelString = temp; // replace prepared string with old one
        }

        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; // saving to database
        [defaults setObject:labelString forKey:@"saveString"];

        NSArray *labelText = [labelString componentsSeparatedByString:separator]; // create array of separated strings
        UILabel *label[[labelText count]]; // create array of labels
        for(int i = 0; i < [labelText count]; i++) // cycle through all labels
        {
            label[i] = [[UILabel alloc] initWithFrame:CGRectMake(0, i * 20 + 20, 320, 20)]; // create a label for each string that will be on a certain position (descending)
            label[i].text = labelText[i]; // set text for labol
        }
    }
}

@end

1 个答案:

答案 0 :(得分:0)

  

我第一次提交后,没有任何反应。

您可以创建UILabel,但不要将它们添加到控制器视图中:

    UILabel *label[[labelText count]]; // create array of labels
    for(int i = 0; i < [labelText count]; i++) // cycle through all labels
    {
        label[i] = [[UILabel alloc] initWithFrame:CGRectMake(0, i * 20 + 20, 320, 20)]; // create a label for each string that will be on a certain position (descending)
        label[i].text = labelText[i]; // set text for labol
        [self.view addSubview:label[i]];
    }

另一方面,我不明白为什么使用数组来存储标签。似乎没有必要。

对于崩溃,它可能取决于访问已解除分配的某个对象。那就是:在第一次运行中,你分配一些对象并将其引用存储得很弱;当方法结束时,对象被释放;当您重新输入该方法时,您尝试访问该对象=&gt;崩溃。

该作业labelString = temp;似乎对我非常怀疑...尝试用控制器中的labelString属性替换静态全局变量strong@interface):< / p>

@property(nonatomic,strong) NSString* labelString;

编辑:

我不确定将labelString声明为属性是否会在第二次点击时修复崩溃。这只是在黑暗中拍摄的。

您应该提供有关崩溃的更多信息,以便我能够提供更多帮助。你可以:

  1. 设置一个异常断点(google for it),以便在崩溃后Xcode会告诉你崩溃发生在哪一行以及出于什么原因;

  2. 或者,只需在saveButton中设置一个断点,然后一步一步地直到崩溃发生;然后你会确切地知道你试图执行哪个陈述;

  3. 尝试在您的Xcode方案中启用僵尸检测(google),如果问题是尝试访问已经解除分配的对象,您将获得完整的报告。

  4. 希望这有帮助。