2015-06-20 11 views

risposta

22

Possiamo usare la Bundle.main

Quindi, se avete un test.json nel vostro parco giochi come

enter image description here

È possibile accedere e stampare il contenuto del genere:

// get the file path for the file "test.json" in the playground bundle 
let filePath = Bundle.main.path(forResource:"test", ofType: "json") 

// get the contentData 
let contentData = FileManager.default.contents(atPath: filePath!) 

// get the string 
let content = String(data:contentData!, encoding:String.Encoding.utf8) 

// print 
print("filepath: \(filePath!)") 

if let c = content { 
    print("content: \n\(c)") 
} 

Stampa

filepath: /var/folders/dm/zg6yp6yj7f58khhtmt8ttfq00000gn/T/com.apple.dt.Xcode.pg/applications/Json-7800-6.app/Contents/Resources/test.json 
content: 
{ 
    "name":"jc", 
    "company": { 
     "name": "Netscape", 
     "city": "Mountain View" 
    } 
} 
+0

Qui è bene ricordare. Per ottenere le risorse il modo migliore è creare il parco giochi all'interno di ** WorkSpace **. Verrò visualizzato per impostazione predefinita e nel punto corretto. – jasmo2

8

Jeremy Chone's risposta, aggiornato per Swift 3, Xcode 8:

// get the file path for the file "test.json" in the playground bundle 
let filePath = Bundle.main.path(forResource: "test", ofType: "json") 

// get the contentData 
let contentData = FileManager.default.contents(atPath: filePath!) 

// get the string 
let content = String(data: contentData!, encoding: .utf8) 


// print 
print("filepath: \(filePath!)") 

if let c = content { 
    print("content: \n\(c)") 
} 
+0

filePath deve essere utilizzato in tutta sicurezza. Si prega di verificare se il file File non è nullo, ad esempio utilizzando let. Lo stesso dovrebbe essere fatto con contentData. Il codice dovrebbe essere scritto in modo che possa gestire questi casi problematici. – Heitara

3

È possibile utilizzare String direttamente con un URL. Esempio a Swift 3:

let url = Bundle.main.url(forResource: "test", withExtension: "json")! 
let text = String(contentsOf: url) 
0

Un altro breve tratto (Swift 3):

let filePath = Bundle.main.path(forResource: "test", ofType: "json") 
let content: String = String(contentsOfFile: filePath!, encoding: .utf8) 
0

Aggiunto provare per swift3.1:

let url = Bundle.main.url(forResource: "test", withExtension: "json")! 
// let text = String(contentsOf: url) 
do { 
    let text = try String(contentsOf: url) 
    print("text: \n\(text)") 
} 
catch _ { 
    // Error handling 
} 

// -------------------------------------------------------------------- 
let filePath2 = Bundle.main.path(forResource: "test", ofType: "json") 
do { 
    let content2: String = try String(contentsOfFile: filePath2!, encoding: .utf8) 
    print("content2: \n\(content2)") 

} 
catch _ { 
    // Error handling 
} 
+0

È molto difficile ignorare gli errori nel blocco catch. Non si dovrebbe usare l'operatore '_': basta prendere 'catch' e genererà correttamente la variabile' error' nel blocco. – Moritz

Problemi correlati