2015-08-05 3 views
6

Sto imparando Swift e sono stato frustrato nel tentativo di capire come non riesco a caricare i file. Si scopre che il codice funziona in Xcode ma non funziona in un parco giochi. Qual è la ragione di questo?Il codice Swift 2.0 funziona in Xcode ma non in Playground

Ecco il codice:

func testFileLoad(){ 
    let myFilePath: String = "/Users/clay/Desktop/test.txt" 
    let t: Bool = NSFileManager.defaultManager().fileExistsAtPath(myFilePath) 
    print(t) 

    let s: String = try! String(contentsOfFile: myFilePath, encoding: NSUTF8StringEncoding) 
    print(s) 

    do { 
     let p: String = try String(contentsOfFile: myFilePath, encoding: NSUTF8StringEncoding) 
     print(p) 
    } catch { 
     print("nope") 
    } 
} 

esecuzione in un modulo di test in Xcode, funziona correttamente e stampa quello che vorrei sperare alla console.

Test Suite 'Selected tests' started at 2015-08-05 14:24:15.977 
Test Suite 'swiftgraphTests' started at 2015-08-05 14:24:15.978 
Test Case '-[swiftgraphTests.swiftgraphTests testFileLoad]' started. 
true 
this is a test 
this is a test 
Test Case '-[swiftgraphTests.swiftgraphTests testFileLoad]' passed (0.001 seconds). 
Test Suite 'swiftgraphTests' passed at 2015-08-05 14:24:15.979.  
    Executed 1 test, with 0 failures (0 unexpected) in 0.001 (0.001) seconds 
Test Suite 'Selected tests' passed at 2015-08-05 14:24:15.979. 
    Executed 1 test, with 0 failures (0 unexpected) in 0.001 (0.002) seconds 

in un parco giochi, ottengo questo:

enter image description here

Che cosa sto facendo male qui? Sto usando il campo giochi in modo improprio?

risposta

7

Se si va al menu

"View" -> "Debug Area" -> "Show Debug Area"

vedrete l'errore completo: "non si dispone di autorizzazioni per accedere al file system dal parco giochi *."

La soluzione alternativa è includere il file nel Playground utilizzando il suo Navigatore progetto.

Vai al menu

"View" -> "Navigators" -> "Show Project Navigator"

quindi trascinare e rilasciare il file nella cartella "Risorse".

Quindi utilizzare NSBundle per ottenere il percorso.

func testFileLoad() { 

    // get the file path for the file from the Playground's Resources folder 
    guard let path = NSBundle.mainBundle().pathForResource("test", ofType: "txt") else { 
      print("Oops, the file is not in the Playground") 
      return 
    } 

    // keeping the examples from your question 
    let s: String = try! String(contentsOfFile: path, encoding: NSUTF8StringEncoding) 
    print(s) 

    do { 
     let p: String = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding) 
     print(p) 
    } catch { 
     print("nope") 
    } 

} 

testFileLoad() 

* In realtà si hanno solo l'accesso alla cartella che contiene i giochi /var/ condiviso dei dati, e il parco giochi è solo offrendo una scorciatoia. Questa cartella nel navigatore del Playground rappresenta in realtà la cartella /var/ ed è univoca per ciascun campo di gioco. Si può vedere il suo indirizzo con NSBundle:

NSBundle.mainBundle().resourcePath 
+0

è la cartella 'var' nel fascio di file del parco giochi? – Clay

+1

Ho aggiornato la mia risposta con ulteriori dettagli su questa cartella. – Moritz

2

Potrebbe essere un problema di autorizzazione. Probabilmente nel tuo progetto XCode hai una serie di privilegi che non sono disponibili nel parco giochi.

+2

L'unica directory che si ha accesso a in un parco giochi può essere ottenuto con 'NSSearchPathForDirectoriesInDomain (.DocumentDirectory, .UserDomainMask, true)' –