2015-08-05 20 views
6

में नहीं, मैं स्विफ्ट सीख रहा हूं और यह पता लगाने की कोशिश कर रहा हूं कि मैं फ़ाइलों को लोड करने में असमर्थ हूं। यह पता चला है कि कोड एक्सकोड में काम करता है लेकिन एक खेल के मैदान में काम नहीं करता है। इसका कारण क्या है?स्विफ्ट 2.0 कोड एक्सकोड में काम करता है लेकिन प्लेग्राउंड

कोड यह रहा:

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

Xcode में एक परीक्षण मॉड्यूल में चल रहा है, यह ठीक से काम करता है और प्रिंट क्या मैं कंसोल के लिए के लिए आशा करता हूं।

enter image description here

क्या मैं गलत कर रहा हूँ यहाँ:

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 

एक खेल का मैदान में, मैं यह मिलता है? क्या मैं खेल के मैदान का अनुचित तरीके से उपयोग कर रहा हूं?

उत्तर

7

आप मेनू

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

में जाते हैं तो आप पूर्ण त्रुटि दिखाई देगी

वर्कअराउंड में प्लेग्राउंड में फ़ाइल प्रोजेक्ट नेविगेटर का उपयोग करके फ़ाइल शामिल है।

मेनू

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

तो खींचें और "संसाधन" फ़ोल्डर में अपनी फ़ाइल ड्रॉप करने जाओ।

फिर पथ प्राप्त करने के लिए एनएसबंडल का उपयोग करें।

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

* वास्तव में आप अपने खेल का मैदान साझा डेटा युक्त /var/ फ़ोल्डर में केवल उपयोग किया है, और खेल का मैदान बस एक शॉर्टकट पेशकश कर रहा है। Playground के नेविगेटर में यह फ़ोल्डर वास्तव में /var/ फ़ोल्डर का प्रतिनिधित्व करता है, और प्रत्येक Playground के लिए अद्वितीय है। आप NSBundle के साथ अपने पते के देख सकते हैं:

NSBundle.mainBundle().resourcePath 
+0

में 'var' फ़ोल्डर है खेल का मैदान फ़ाइल का बंडल? – Clay

+1

मैंने इस फ़ोल्डर के बारे में अतिरिक्त विवरण के साथ अपना उत्तर अपडेट कर दिया है। – Moritz

2

एक अनुमति समस्या हो सकती है। शायद आपके एक्सकोड प्रोजेक्ट में आपके पास विशेषाधिकारों का एक सेट है जो खेल के मैदान में उपलब्ध नहीं हैं। "* यदि आप खेल का मैदान से फाइल सिस्टम का उपयोग करने की अनुमति नहीं है।":

+2

केवल निर्देशिका है कि आप एक खेल के मैदान में की पहुंच है 'NSSearchPathForDirectoriesInDomain (.DocumentDirectory, .UserDomainMask, सच) के साथ मिल किया जा सकता है' –

संबंधित मुद्दे