write to plist
I have been developing for the cocoa touch and havent had much experience developing for cocoa. With that, my question is how do I write to a file? Is it the same as I would do for the iPhone OS? I ask this because the code I am currently using doesnt seem to be writing to the file.
Here is my code I was using so far.
if (prevCommand == nil) {
prevCommand = [[NSMutableArray alloc] initWithObjects:command,nil];
NSString *docsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
commandHistoyPath = [docsDirectoryPath stringByAppendingPathComponent:@"commandhistory.plist"];
}
else {
[prevCommand addObject:command];
[command writeToFile:commandHistoyPath atomically:YES encoding:NSASCIIStringEncoding error:NULL];
}
That code looks fine to me. I've never used the iPhone or the iTouch - however the code looks about right to write to me!
I put your code into a command line program on the desktop:
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString* command ;
NSMutableArray* prevCommand = nil ;
NSString* commandHistoryPath = nil;
NSString* commandsPath;
for ( int i = 0 ; i < argc ; i++ ) {
command = [NSString stringWithFormat:@"%d %s\n",i,argv[i]] ;
if (prevCommand == nil) {
prevCommand = [[NSMutableArray alloc] initWithObjects:command,nil];
NSString* docsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
commandHistoryPath = [docsDirectoryPath stringByAppendingPathComponent:@"commandhistory.plist"];
commandsPath = [docsDirectoryPath stringByAppendingPathComponent:@"commands.plist"];
}
if ( prevCommand ) {
[prevCommand addObject:command];
[command writeToFile:commandHistoryPath atomically:YES encoding:NSASCIIStringEncoding error:NULL];
[prevCommand writeToFile:commandsPath atomically:YES];
}
}
system("/bin/ls -alt ~/Documents/c*.plist ; /bin/cat ~/Documents/commands.plist ; /bin/cat ~/Documents/commandhistory.plist") ;
[pool drain];
return 0;
}
And here's the result when I run it in terminal:
556 /Users/rmills/Projects/WriteFile/build/Debug $ WriteFile does this work
-rw-r--r--@ 1 rmills staff 7 Apr 11 07:55 /Users/rmills/Documents/commandhistory.plist
-rw-r--r-- 1 rmills staff 299 Apr 11 07:55 /Users/rmills/Documents/commands.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>0 WriteFile
</string>
<string>1 does
</string>
<string>2 this
</string>
<string>3 work
</string>
</array>
</plist>
3 work
557 /Users/rmills/Projects/WriteFile/build/Debug $
I know that isn't the answer to the question you asked, however I hope you find it useful. It could be that the ~/Documents directory (or equivalent) on the iTouch simply doesn't exist in that environment.