Xcode 如何在OS X 10.10应用程序中访问任意文件?

Xcode 如何在OS X 10.10应用程序中访问任意文件?,xcode,swift,file-io,osx-yosemite,Xcode,Swift,File Io,Osx Yosemite,我正在自学使用Swift OS X 10.10应用程序开发苹果。我想向NSXMLParser传递一个文件URL。该文件很大,位于与我的应用程序分离的磁盘上。我创建了NSURL,但当我使用CheckPromisedItemMisablableandReturnError()检查它时,我总是得到一个“没有这样的文件或目录错误” 看起来,当我在Xcode中运行我的应用程序时,有什么东西在将应用程序的开发目录预先添加到我的文件路径中,因此,“/Volumes/bigdrive/data.xml”变成了“

我正在自学使用Swift OS X 10.10应用程序开发苹果。我想向NSXMLParser传递一个文件URL。该文件很大,位于与我的应用程序分离的磁盘上。我创建了NSURL,但当我使用CheckPromisedItemMisablableandReturnError()检查它时,我总是得到一个“没有这样的文件或目录错误”

看起来,当我在Xcode中运行我的应用程序时,有什么东西在将应用程序的开发目录预先添加到我的文件路径中,因此,
“/Volumes/bigdrive/data.xml”
变成了
“/Users/charlweed/Library/Developer/Xcode/DerivedData/dataskunk ghkiumvdkopxarhavynetidlqxio/Build/Products/Debug/file:/volums/bigdrive/data.xml”

创建项目时,我没有启用沙盒或iCloud。我想我可能需要使用NSURL.startAccessingSecurityScopedResource(),但它总是返回true。我做错了什么?这是Swift中的一个测试函数,我不知道objective-c,但我希望我能找到objective-c的答案:

func accessFile() {
    /**I belive this URI is correct, becuase everything after the file:// works in the OS X bash*/
    let xmlInFilePath = "file:///Volumes/Seagate_1tib/projects/dataskunk/wasteproduct.xml"
    if let xmlInFileURL = NSURL.fileURLWithPath(xmlInFilePath)
    {
        println("Unwrapped \(xmlInFileURL)")
        var securityURLBS = xmlInFileURL.startAccessingSecurityScopedResource()
        if securityURLBS
        {
            var xmlFileError: NSError?
            if xmlInFileURL.checkPromisedItemIsReachableAndReturnError(&xmlFileError)
            {
                println("Can access file. huray!")
                /** Use the file URL**/
            }
            else
            {
                /** This Always happens with a "No such file or directory " :( **/
                println("\(xmlFileError)")
            }
        }
        else
        {
            println("Could not get Security Scoped Resource")
        }
        xmlInFileURL.stopAccessingSecurityScopedResource()
    }
    else
    {
        log(" NSURL.fileURLWithPath() returned nil for \(xmlInFilePath)")
    }
}
以下是错误的转储:

展开文件:/Volumes/segate_1tib/projects/dataskunk/apple_rss.xml--file:///Users/charlweed/Library/Developer/Xcode/DerivedData/Keepass2Keyring-ghkiumvdkopxarhavynetidlqxio/Build/Products/Debug/

可选(错误域=NSCOCAERRORDOMAIN Code=260)无法打开文件“wasteproduct.xml”,因为没有这样的文件

UserInfo=0x6100006F9C0 NSURL=file:/Volumes/segate_1tib/projects/dataskunk/wasteproduct.xml--file:///Users/charlweed/Library/Developer/Xcode/DerivedData/Keepass2Keyring-ghkiumvdkopxarhavynetidlqxio/Build/Products/Debug/,

NSFilePath=/Users/charlweed/Library/Developer/Xcode/DerivedData/Keepass2Keyring ghkiumvdkopxarhavynetidlqxio/Build/Products/Debug/file:/Volumes/segate_1tib/projects/dataskunk/wasteproduct.xml

NSUnderlyingError=0x61000044A10“操作无法完成。没有此类文件或目录”})


答案是
NSURL.fileURLWithPath()
不将URL路径作为参数,而只将文件系统路径作为参数。所以
”file:///Volumes/disk/file.xml“
错误,
”/Volumes/disk/file.xml“
正确


损坏是NSURL将当前目录作为前缀添加到它认为是相对文件系统路径字符串上。

只是为了让您知道该字符串是否具有前缀文件://您需要使用NSURL方法filePathForURL()创建url,然后您可以使用路径扩展名从该NSURL提取路径。您的具体意思是什么?NSURL类上没有“filePathForURL”方法。至少目前没有相关文档,编译器在我尝试引用它时抱怨它不存在。很抱歉,它被称为fileURLWithPath:file://是url方案。路径没有它。无法在查找程序的“转到文件夹”命令shift-G中将文件://添加到path.Ttry