Estoy leyendo archivos antiguos que aún usan rutas de estilo HFS, como VolumeName:Folder:File .
Necesito convertirlos en rutas POSIX.
No me gusta hacer el reemplazo de cadenas ya que es un poco complicado, ni quiero invocar operaciones de AppleScript o Shell para esta tarea.
¿Hay una función de marco para lograr esto? La depreciación no es un problema.
Por cierto, aquí hay una solución para la operación inversa .
La operación "inversa" de CFURLCopyFileSystemPath() es CFURLCreateWithFileSystemPath() . Del mismo modo que en las preguntas y respuestas a las que se hace referencia, ha creado el estilo de ruta a partir del valor de enumeración sin formato, ya que CFURLPathStyle.cfurlhfsPathStyle está obsoleto y no está disponible. Ejemplo:
let hfsPath = "Macintosh HD:Applications:Xcode.app" if let url = CFURLCreateWithFileSystemPath(nil, hfsPath as CFString, CFURLPathStyle(rawValue: 1)!, true) as URL? { print(url.path) // /Applications/Xcode.app }Una solución en Obj-C y Swift como categoría/extensión de NSString / String . El estilo kCFURLHFSPathStyle no disponible se elude de la misma manera que en la pregunta vinculada.
C objetivo
@implementation NSString (POSIX_HFS) - (NSString *)POSIXPathFromHFSPath { NSString *posixPath = nil; CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)self, 1, [self hasSuffix:@":"]); // kCFURLHFSPathStyle if (fileURL) { posixPath = [(__bridge NSURL*)fileURL path]; CFRelease(fileURL); } return posixPath; } @endRápido
extension String { func posixPathFromHFSPath() -> String? { guard let fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, self as CFString?, CFURLPathStyle(rawValue:1)!, self.hasSuffix(":")) else { return nil } return (fileURL as URL).path } }