2014-07-11 5 views
51

मैं समझने की कोशिश कर रहा हूं कि स्विफ्ट के साथ fileExistsAtPath:isDirectory: फ़ंक्शन का उपयोग कैसे करें, लेकिन मैं पूरी तरह से खो गया हूं।NSFileManager fileExistsAtPath: isDirectory और swift

यह मेरा कोड उदाहरण है:

var b:CMutablePointer<ObjCBool>? 

if (fileManager.fileExistsAtPath(fullPath, isDirectory:b!)){ 
    // how can I use the "b" variable?! 
    fileManager.createDirectoryAtURL(dirURL, withIntermediateDirectories: false, attributes: nil, error: nil) 
} 

मैं नहीं समझ सकता है कि कैसे मैं b MutablePointer के लिए मूल्य पहुँच सकते हैं। क्या होगा यदि मैं जानना चाहता हूं कि यह YES या NO पर सेट है या नहीं?

उत्तर

131

दूसरा पैरामीटर प्रकार UnsafeMutablePointer<ObjCBool>, जिसका मतलब है कि आप एक ObjCBool चर के पता पारित करने के लिए है कि है। उदाहरण:

var isDir : ObjCBool = false 
if fileManager.fileExistsAtPath(fullPath, isDirectory:&isDir) { 
    if isDir { 
     // file exists and is a directory 
    } else { 
     // file exists and is not a directory 
    } 
} else { 
    // file does not exist 
} 

अद्यतन स्विफ्ट 3 और स्विफ्ट 4 के लिए:

let fileManager = FileManager.default 
var isDir : ObjCBool = false 
if fileManager.fileExists(atPath: fullPath, isDirectory:&isDir) { 
    if isDir.boolValue { 
     // file exists and is a directory 
    } else { 
     // file exists and is not a directory 
    } 
} else { 
    // file does not exist 
} 
+0

यहाँ दस्तावेज़ के लिए लिंक है, https://developer.apple.com/documentation/foundation/filemanager/1410277 -फाइल मौजूद है# –

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