Saturday, January 20, 2018

#1 2010-09-30 03:22:35 pm

RIRedinPA
Member
From:: Philadelphia
Registered: 2003-11-12
Posts: 237
Website

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];
}

Offline

 

#2 2010-09-30 03:47:50 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11483
Website

Re: objective-c make a file...

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
[code]    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]
        }
    }[/code]


regards

Stefan

Offline

 

#3 2010-09-30 04:25:11 pm

RIRedinPA
Member
From:: Philadelphia
Registered: 2003-11-12
Posts: 237
Website

Re: objective-c make a file...

Thanks Stefan...that's incredibly helpful...I need to read up on NSDictionary some

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)