views:

57

answers:

1

Hello,

I'm stumped or just not finding info I need, I have an object that I want to save in an array when they select "saveDataround" button, however I can't seem to figure out how to populate the object with the text "Round": I'm getting an "Expected identifier" and "Expected , ;" errors on the first and second lines of code. Thanks in advance.

[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  A: 

Where are the first two lines of code implemented? What are they supposed to do?

Here's how I would modify the above code without more info:

// 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];
}
gerry3
+1 for not only fixing syntax errors, but also improving the code.
Steve Harrison
Thanks, i'm still getting an error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' tokenafter self.round.text = roundChoice;
Michael Robinson
Sorry, About the first two lines, I'm not sure where they should go...
Michael Robinson
Well, it depends on what you are trying to do. You definitely can put them inside the saveDataRound method.
gerry3
Thanks again...overall I'm having someone select a button that creates a text file that says "round", I'll later pull that out to place in an e-mail. (I have code for that and it's working fine),I went ahead an put them in the saveDataRound method, The array isn't writing though, when I had a problem like this first go around the array was empty and that why it didn't write. any ideas?
Michael Robinson
You can check if the array has objects by using NSLog: NSLog(@"%@",array);
gerry3
i can tell if it writes by looking in the Documents Directory I did "build and analyze" and it showed " Method returns an Objective-C object with a +1 retain count (owning reference) connecting the 1st line: NSString and the "NSMutableArray *array.." line.
Michael Robinson
That comment is because the code is leaking the array. I have fixed it above. This has nothing to do with any issues you are having in actually writing the file.
gerry3
Nice. You rock. I have a weird feeling my button isn't working to start the -(IBAction), I'm going to rebuild that in case I screwed that up.
Michael Robinson
It seems to be wanting to do something now, i have this error in debugger console:'NSInvalidArgumentException', reason: '*** -[NSCFArray insertObject:atIndex:]: attempt to insert nil'
Michael Robinson
If you run in debug mode, the debugger should break where the error is occurring. In any case, the error is self explanatory. You are attempting to add a nil object to an array.
gerry3