【问题标题】:Mac Objective-C convert HFS style paths to POSIX path [duplicate]Mac Objective-C 将 HFS 样式路径转换为 ​​POSIX 路径
【发布时间】:2019-07-09 10:15:29
【问题描述】:

在尝试建议的方法here 时,工作目录会得到前缀而不是路径转换。前导:HFS 路径没有任何区别。

    NSString * ttt = @"Macintosh HD:Users:gautam:code:Help:";
    if (CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)ttt, kCFURLPOSIXPathStyle, false))
    {
        NSString * posixPath = [(__bridge NSURL*)url path];
        // posixPath    __NSCFString *  "/Users/gautamjha/code/Macintosh HD:Users:gautam:code:Help:"
        const char * secondName = [posixPath UTF8String];
       // above does not help either working directory gets prefixed.
    }

【问题讨论】:

  • 问题在引用的问答中解决,只是你没有正确复制代码。

标签: objective-c


【解决方案1】:

发生错误是因为您传递的是 source 类型 kCFURLPOSIXPathStyle,但您想从 HFS 路径创建 URL。

正如我在链接答案中提到的,常量 kCFURLHFSPathStyle 不可用,您必须将其替换为原始值 1

if (CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)ttt, 1, false))

【讨论】:

  • kCFURLPOSIXPathStyle = 0 为原始枚举,枚举定义可用且代码代码无法用整数编译。
  • 我的错,我更新了答案。在我的机器上,代码使用整数编译,但 if 表达式无法编译。请按字面意思尝试链接答案中的代码。
  • 这不是stackoverflow.com/questions/55383444/…的复制品吗?
  • @MartinR 实际上是这样,但是由于 OP 无法根据链接的 here 解决问题,因此我写了一个答案。
猜你喜欢
  • 2019-08-18
  • 2017-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-16
  • 1970-01-01
  • 2019-11-14
相关资源
最近更新 更多