2014-10-06 22 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 

很明顯只用於開發。

相關問題