2017-10-18 1 views
0

c'est le code objectif cpourquoi je ne reçois pas le chemin d'URL correct dans OSX rapide

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

quand j'ai essayé de convertir ce code SWIFT 3, je ne reçois pas l'URL correcte .. Can quelqu'un vous aide?

This is the path I get 

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

Mais ceci est le chemin que je veux

fichier

: ///Users/xyz/xyz/xyz-201710291142410..m4a

ma syntaxe rapide a échoué

formatter = 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") 
+0

est l'application sandbox? Si oui, vous n'avez pas accès au répertoire personnel. – vadian

+0

pouvez-vous me dire comment le savoir? Je suis assez nouveau à OSX. Je reçois le chemin lorsque j'utilise le code c objectif .. Mais je veux utiliser le code rapide. – solArise

+0

ok pas d'importance J'ai corrigé mon problème – solArise

Répondre

0

L'équivalent Swift est

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

Si vous souhaitez utiliser la syntaxe ~ vous devez relier la chaîne à NSString parce expandingTildeInPath n'est pas disponible dans Swift String

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