在开发iOS应用程序时,NSUserName()似乎不适用于Xcode 6。 我在开发过程中使用它来获取桌面目录以将核心数据种子保存到我的桌面。是否NSUserName()在xcode6中不起作用?
NSString* deskStorePath = [NSString stringWithFormat:@"/Users/%@/Desktop/newMySQL.CDBStore", NSUserName()];
我有我的代码更改为:
#define USER_NAME @"myusername"
NSString* deskStorePath02 = [NSString stringWithFormat:@"/Users/%@/Desktop/newMySQL.CDBStore", USER_NAME];
现在NSUserName()在Xcode 6没有返回值,但曾在Xcode工作正常5.
NSString* myname = NSUserName();
NSString* myfullname = NSFullUserName();
NSLog(@"name : %@ - %@",myname,myfullname);
它是一个错误? 我现在需要导入一个库吗? 或者在应用程序开发过程中是否有替代方案来获取当前的OSX用户名?
编辑:(这是我使用保存核心数据种子商店桌面,然后我添加到我的开发环境的方法)
- (void) saveSeedToDesktop
{
[self testNSUser];
NSString* myDBName = [NSString stringWithFormat:@"%@.sqlite",CD_FILE_NAME];
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent : myDBName];
NSString* deskStorePath = [NSString stringWithFormat:@"/Users/%@/Desktop/newMySQL.CDBStore", USER_NAME];
NSError*error;
if (![[NSFileManager defaultManager] copyItemAtPath:storePath toPath: deskStorePath error:&error]){
NSLog(@"error with path : %@",error);
}
else {
NSLog(@"Copied");
}
}
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];
}
这是一个测试类:
- (void) testNSUser {
NSString* myname = NSUserName();
NSString* myfullname = NSFullUserName();
NSLog(@"name : %@ - %@",myname,myfullname);
__unused NSString* deskStorePathTest01 = [NSString stringWithFormat:@"/Users/this_is_my_name/Desktop/test.txt"]; //works
__unused NSString* deskStorePathTest02 = [NSString stringWithFormat:@"~/Desktop/test.txt"];
__unused NSString* deskStorePathTest03 = [NSString stringWithFormat:@"/Users/~/Desktop/test.txt"];
__unused NSString* deskStorePathTest04 = [NSString stringWithFormat:@"/~/Desktop/test.txt"];
[self fileExistsTest:deskStorePathTest01];
[self fileExistsTest:deskStorePathTest02];
[self fileExistsTest:deskStorePathTest03];
[self fileExistsTest:deskStorePathTest04];
}
- (void) fileExistsTest : (NSString*)storePath {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *pathForFile = storePath;
if ([fileManager fileExistsAtPath:pathForFile]){
NSLog(@"exists : %@",storePath);
}
else {
NSLog(@"doesnt exist");
}
}
你不能使用'〜/ Desktop'吗? – 2014-10-06 03:35:13
Xcode 6>(lldb)po不再工作在我的最终目的上NSUserName()
这与Xcode 6有什么关系? – Droppy 2014-10-16 19:16:11