添加带有“文本”的对象

时间:2010-01-25 23:30:04

标签: iphone nsstring nsobject

我很难过或者只是找不到我需要的信息,我有一个对象,当我们选择“saveDataround”按钮时我想要保存在数组中,但是我似乎无法弄清楚如何填充对象文字“Round”:我得到一个“预期的标识符”和“预期的,”;第一行和第二行代码的错误。提前致谢。

[NSString *roundChoice = [NSString stringWithFormat:@"Round"];
self.round.text = roundChoice;]

- (IBAction)saveDataround {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *recipient = [NSString stringWithFormat:@"%@/arrayChoiceRound", documentsDirectory];
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [array addObject:round.text];
    [array writeToFile:recipient atomically:NO];
}

1 个答案:

答案 0 :(得分:1)

前两行代码在哪里实现?他们应该做什么?

以下是我在没有更多信息的情况下修改上述代码的方法:

// remove "[" from start of line & no need to use stringWithFormat here
NSString *roundChoice = @"Round";

// remove "]" from end of line
self.round.text = roundChoice;

- (IBAction)saveDataround {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    // use stringByAppendingPathComponent here
    NSString *recipient = [documentsDirectory stringByAppendingPathComponent:@"arrayChoiceRound"];
    NSMutableArray *array = [[NSMutableArray alloc] init];

    // use self here (not required)
    [array addObject:self.round.text];

    [array writeToFile:recipient atomically:NO];

    // release the array
    [array release];
}