changecurcurrentdirectorypath 在 objective c 中将当前路径更改为 null

changecurrentdirectorypath changes the current path to null in objective c

我已经多次尝试将当前目录更改为 /User/{username} 所以我可以在 /User/{username}/ 目录中创建新文件夹。 我使用的代码是:

filemgr = [NSFileManager defaultManager];

currentpath = [filemgr currentDirectoryPath];

NSLog (@"from changing directory Current directory is %@", currentpath);

if ([filemgr changeCurrentDirectoryPath: @"/Users/"] == NO)
    NSLog (@"from changing directory Cannot change directory.");
else
    NSLog(@"directory changed is %@",currentpath);

currentpath = [filemgr currentDirectoryPath];

NSLog (@"directory after changed Current directory is %@", currentpath);

并得到如下输出:

2015-12-05 11:47:43.450 test2_Finder[970:33618] from changing directory Current directory is /Users/macbookpro/Library/Containers/com.eebs.Test2.test2-Finder/Data 2015-12-05 11:47:43.450 test2_Finder

[970:33618] directory changed is /Users/macbookpro/Library/Containers/com.eebs.Test2.test2-Finder/Data 2015-12-05 11:47:43.458 test2_Finder[970:33618] directory after changed Current directory is (null)

2015-12-05 11:47:43.458 test2_Finder[970:33618] -[FinderSync init] launched from /Users/macbookpro/Library/Developer/Xcode/DerivedData/Test2-hbxwqkjtnzaewlcisyxybyfbzefb/Build/Products/Debug/Test2.app/Contents/PlugIns/test2_Finder.appex ; compiled at 11:47:39

终于找到了创建文件夹到用户目录的解决方案比如 “/Users/macbookpro/folder” NSString *userPath=[NSOpenStepRootDirectory() stringByAppendingString:@"Users/"]; NSString *userPath1=[userPath stringByAppendingString:NSUserName()]; NSString *userPath2=[userPath1 stringByAppendingString:@"/folder/"]; if(![fileManager fileExistsAtPath:userPath2 isDirectory:&isDir]) if(![fileManager createDirectoryAtPath:userPath2 withIntermediateDirectories:YES attributes:nil error:NULL]) NSLog(@"Error: Create folder failed %@",userPath2);