简体   繁体   English

为什么我在Swift OSX中没有得到正确的URL路径

[英]why am I not getting the correct URL path in swift OSX

this is the objective c code 这是目标C代码

dispatch_once(&onceToken, ^{
    formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYYMMDDHHmmss"];

    homeDir = [NSURL fileURLWithPath:[@"~/xyz/" stringByExpandingTildeInPath]];
  });

  NSString* date = [formatter stringFromDate:[NSDate date]];
  self.file      = [[homeDir URLByAppendingPathComponent:[@"xyz-" stringByAppendingString:date]] URLByAppendingPathExtension:@"m4a"];
 NSLog(@" self.file  = %@", self.file );

when I tried to convert this code to SWIFT 3, I am not getting the correct URL.. Can anyone help? 当我尝试将此代码转换为SWIFT 3时,我没有获得正确的URL。有人可以帮忙吗?

This is the path I get

~/xyz/xyz-201710291142410.m4a -- file:///Users/xyz/Library/Developer/Xcode/DerivedData/xyz-guhahcdeflytfhhdsxtsrucmqqnv/Build/Products/Debug/

But this is the path I want 但这是我想要的路

file:///Users/xyz/xyz/xyz-201710291142410..m4a 文件:///Users/xyz/xyz/xyz-201710291142410..m4a

my failed swift syntax 我失败的快速语法

formatter = DateFormatter() 格式化程序= DateFormatter()

    formatter?.dateFormat = "YYYYMMDDHHmmss"

    let urlS = "~/xyz/"
    // NSURL.fileURL(withPath: urlS.stringByExpandingTil)

    var homeDir = URL.init(fileURLWithPath: urlS) //URL.appendingPathComponent(URL(string: urlS))
    let date: String? = formatter?.string(from: Date())
    let file = homeDir.appendingPathComponent("xyz-" + (date!)).appendingPathExtension("m4a")

The Swift equivalent is Swift的等效项是

let formatter : DateFormatter = {
    let df = DateFormatter()
    df.dateFormat = "YYYYMMDDHHmmss"
    return df
}()

let homeDir = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("xyz")

let date = formatter.string(from: Date())
let file = homeDir.appendingPathComponent("xyz-" + date).appendingPathExtension("m4a")

If you want to use the ~ syntax you have to bridge the string to NSString because expandingTildeInPath is not available in Swift String 如果你想使用~语法,你必须弥合字符串NSString因为expandingTildeInPath不斯威夫特提供String

let homeDir = URL(fileURLWithPath:("~/xyz/" as NSString).expandingTildeInPath).appendingPathComponent("xyz")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在正确的时区输出错误的时间? - Why am I getting wrong time output with correct timezone? 一旦我登录,为什么我没有在 Swift 中将 HomeViewController 作为 Rootviewcontroller? - Once i Login why i am not getting HomeViewController as Rootviewcontroller in Swift? 当我不使用 CoreData 时,为什么我的 iOS 或 OSX 应用程序中出现 CoreData 错误? - Why am I getting a CoreData error in my iOS or OSX app when I'm not using CoreData? 在Swift 4中解析JSON后,为什么会得到空白的UITableView? - Why am I getting a blank UITableView after parse JSON in Swift 4? 为什么我在 Xcode 10.1 中出现 swift 编译器错误 - Why I am getting swift compiler error in Xcode 10.1 为什么在Swift操场上进行简单的加法操作时会出错? - Why I am getting error for simple addition operation in Swift playground? 为什么我会在表格视图中快速获得堆叠的文本标签 - Why am i getting a stacked text label in table view swift 为什么我得到“Swift Dynamic Cast失败”? :/ - Why am I getting “Swift Dynamic Cast Failed” ?? :/ 滚动后为什么UITableViewCell中出现双重内容? (迅速) - Why am I getting double content in UITableViewCell after scrolling? (Swift) 为什么我会收到NSUnkownKeyException? - Why am I getting NSUnkownKeyException?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM