开发者

iOS Simulator Thinks Documents Directory is /var/root/Documents

I am trying to read all the files in the simulator in the Documents directory, but I get a Cocoa error 260. When I log [self applicationDocumentsDirectory], it says /var/root/Documents. Anyone know why this would happen? It only happens when I run from the command line. Wh开发者_JS百科en running in the simulator itself the code correctly outputs the right documents folder for the simulator.

UPDATE I am seeing a path of "file://localhost/Users/MyUserName/Documents" now. So weird...

- (NSString *)applicationDocumentsDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}


The fix was to chmod -R 777 "/Users/MyUserName/Documents" and then change my method to this:

- (NSString *)applicationDocumentsDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;

    if (![[NSFileManager defaultManager] createDirectoryAtPath:basePath withIntermediateDirectories:YES attributes:nil error:NULL])
    {
        NSLog(@"Error: Create documents folder failed %@", basePath);
    }

    return basePath;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜