How to get Darwin user cache dir via swift?

I'm trying to get the same path you'd get by running getconf DARWIN_USER_CACHE_DIR in the terminal, but via

FileManager.default.urls(for: , in:) , but can't really find out how is there a way to do that other than running the shell script via swift?

Answered by DTS Engineer in 787967022

If you want exactly that value, it’s not hard to call confstr from Swift:

func darwinUserCacheDir() -> URL? {
    var buf = [CChar](repeating: 0, count: 1024)
    let success = confstr(_CS_DARWIN_USER_CACHE_DIR, &buf, buf.count) >= 0
    guard success else { return nil }
    return URL(fileURLWithFileSystemRepresentation: &buf, isDirectory: true, relativeTo: nil)
}

Keep in mind that these values can diverge based on the context in which the code is running. For example, if you run this code in a sandboxed app you’ll get a very different result.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

It's done like this:

let cachesDirectory = Filemanager.default.urls(for: .cachesDirectory, in: .userDomainMask).first

let darwinUserCacheDir = cachesDirectory!.appendingPathComponent("com.apple.darwin.user-cache", isDirectory: true).absoluteString
Accepted Answer

If you want exactly that value, it’s not hard to call confstr from Swift:

func darwinUserCacheDir() -> URL? {
    var buf = [CChar](repeating: 0, count: 1024)
    let success = confstr(_CS_DARWIN_USER_CACHE_DIR, &buf, buf.count) >= 0
    guard success else { return nil }
    return URL(fileURLWithFileSystemRepresentation: &buf, isDirectory: true, relativeTo: nil)
}

Keep in mind that these values can diverge based on the context in which the code is running. For example, if you run this code in a sandboxed app you’ll get a very different result.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

How to get Darwin user cache dir via swift?
 
 
Q