objective-c make a file...

I’m trying to create a file to store some simple data in the document directory of my app…I’m not getting any error messages but it also doesn’t seem to be working…any idea what I am doing wrong?

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

  • (void)viewDidLoad {

    //set path to document directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSFileManager *manager = [NSFileManager defaultManager];
    NSArray *fileList = [manager directoryContentsAtPath:documentsDirectory];

    //set file count
    int itemcount = [fileList count];
    NSLog(@“%d”, itemcount);

    if (itemcount > 0) {
    //set some vars
    BOOL hasmaster = FALSE;
    //loop through looking for master.plist
    for (NSString *s in fileList){
    NSLog(@“%@”, s);
    }
    } else {
    NSLog(@“Need to make master.plist”);
    [[paths objectAtIndex:0] stringByAppendingPathComponent:@“master.plist”];
    }

    [super viewDidLoad];
    }

Hi,

your code doesn’t include any methods to create a file.
You can easily check the existence of a file with fileExistsAtPath: of NSFileManager
Try this

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *masterFilePath = [documentsDirectory stringByAppendingPathComponent:@"master.plist"]; NSDictionary *plistDict = [NSDictionary dictionary]; NSFileManager *manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath:masterFilePath]) ; // do something; else { NSString *errorDesc; NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc]; if (!errorDesc) { if (![manager createFileAtPath:masterFilePath contents:plistData attributes:nil]) NSLog(@"Can't create file at %@", masterFilePath); } else { NSLog(@"Can't create plist data"); [errorDesc release] } }

Thanks Stefan…that’s incredibly helpful…I need to read up on NSDictionary some