Objective c NSUserName()在xcode6中不工作吗?

Objective c NSUserName()在xcode6中不工作吗?,objective-c,xcode6,Objective C,Xcode6,在开发iOS应用程序时,NSUserName()似乎不适合我在Xcode 6中使用。 我用它来获取桌面目录,以便在开发期间将核心数据种子保存到我的桌面 NSString* deskStorePath = [NSString stringWithFormat:@"/Users/%@/Desktop/newMySQL.CDBStore", NSUserName()]; 我必须将代码更改为: #define USER_NAME @"myusername" NSString* deskStoreP

在开发iOS应用程序时,NSUserName()似乎不适合我在Xcode 6中使用。 我用它来获取桌面目录,以便在开发期间将核心数据种子保存到我的桌面

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) 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");
    }
}

不管怎样,它在设备上对我有效。它在模拟器上似乎不起作用

-(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之外操作核心数据存储文件。

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

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

显然只用于开发。

你不能使用
~/Desktop
?在Xcode 6>(lldb)po NSUserName()上也不再在我这端工作了这与Xcode 6有什么关系?在设备上我得到了输出,但输出是错误的:2014-10-16 11:25:31.183 APPNAME[1287:324349]name:mobile-mobile User
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