2014-10-06 63 views
1

在开发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"); 
    } 
} 
+0

你不能使用'〜/ Desktop'吗? – 2014-10-06 03:35:13

+0

Xcode 6>(lldb)po不再工作在我的最终目的上NSUserName() Kalle 2014-10-16 12:53:45

+0

这与Xcode 6有什么关系? – Droppy 2014-10-16 19:16:11

回答

0

无论如何,它在我的设备上工作。它似乎不适用于模拟器。

-(id) init 
{ 
    self = [super initWithStyle:UITableViewStyleGrouped]; 

    if(self) 
    { 
     //Custom initialization. 
     NSString *u = NSUserName(); 

     NSLog(@"%@", u); 
    } 

    return self; 
} 

输出是:

2014-10-05 20:45:38.451 [my app business] mobile 

如果我理解你想要什么,在这里完成,不过,我想初始化默认的核心数据,店里的规定方式,可能是以编程方式添加它。至少,这是我从文档中拿走的东西。我非常确定他们会说某处不应该在API之外操作Core Data存储文件。

+0

在设备上我得到的输出,但它的输出错误: \t 2014-10-16 11:25:31.183 APPNAME [1287:324349] name:mobile - Mobile User – MGM 2014-10-16 18:32:55

2

我们在模拟器中运行应用程序产生的资产有相同的问题。我意识到该应用程序本身存储在我的主目录下,所以我可以从中访问我的用户名:

NSString *userName = NSUserName(); 

#if TARGET_IPHONE_SIMULATOR 
if (userName.length == 0) 
{ 
    NSArray *bundlePathComponents = [NSBundle.mainBundle.bundlePath pathComponents]; 

    if (bundlePathComponents.count >= 3 
     && [bundlePathComponents[0] isEqualToString:@"/"] 
     && [bundlePathComponents[1] isEqualToString:@"Users"]) 
    { 
     userName = bundlePathComponents[2]; 
    } 
} 
#endif 

很明显只用于开发。

相关问题