从 Swift 3.0 中的主包加载 XML 文件
Load XML file from main Bundle in Swift 3.0
我有一个 .GPX 文件,其中包含徒步旅行的路线信息,我想将其加载到我的应用程序中。如果我从远程 URL (https://dl.dropboxusercontent.com/u/45741304/appsettings/Phu_si_Lung_05_01_14.gpx) 加载它,一切都很好,但我无法从应用程序包加载同一个文件(已经在 "Copy bundle resources" 中并且具有正确的目标成员资格)。
这是我从远程 URL 加载此文件的代码:
var xmlParser: XMLParser!
func startParsingFileFromURL(urlString: String) {
guard let url = URL(string: urlString) else {
print("Can't load URL: \(urlString)")
return
}
self.xmlParser = XMLParser(contentsOf: url)
self.xmlParser.delegate = self
let result = self.xmlParser.parse()
print("parse from URL result: \(result)")
if result == false {
print(xmlParser.parserError?.localizedDescription)
}
}
来自主包:
func startParsingFile(fileName: String, fileType: String) {
guard let urlPath = Bundle.main.path(forResource: fileName, ofType: fileType) else {
print("Can't load file \(fileName).\(fileType)")
return
}
guard let url:URL = URL(string: urlPath) else {
print("Error on create URL to read file")
return
}
self.xmlParser = XMLParser(contentsOf: url)
self.xmlParser.delegate = self
let result = self.xmlParser.parse()
print("parse from file result: \(result)")
if result == false {
print(xmlParser.parserError?.localizedDescription)
}
}
从应用程序包加载时出错:
parse from file result: false
Optional("The operation couldn’t be completed. (Cocoa error -1.)")
您是说:
guard let urlPath = Bundle.main.path(forResource: fileName, ofType: fileType) else {
print("Can't load file \(fileName).\(fileType)")
return
}
guard let url:URL = URL(string: urlPath) else {
print("Error on create URL to read file")
return
}
首先,将字符串路径变成URL是非常愚蠢的。你知道你想要 URL,那你为什么不先调用 url(forResource:...)
?
其次,如果你曾经做把一个字符串路径变成URL,你必须做一个file URL.
我有一个 .GPX 文件,其中包含徒步旅行的路线信息,我想将其加载到我的应用程序中。如果我从远程 URL (https://dl.dropboxusercontent.com/u/45741304/appsettings/Phu_si_Lung_05_01_14.gpx) 加载它,一切都很好,但我无法从应用程序包加载同一个文件(已经在 "Copy bundle resources" 中并且具有正确的目标成员资格)。
这是我从远程 URL 加载此文件的代码:
var xmlParser: XMLParser!
func startParsingFileFromURL(urlString: String) {
guard let url = URL(string: urlString) else {
print("Can't load URL: \(urlString)")
return
}
self.xmlParser = XMLParser(contentsOf: url)
self.xmlParser.delegate = self
let result = self.xmlParser.parse()
print("parse from URL result: \(result)")
if result == false {
print(xmlParser.parserError?.localizedDescription)
}
}
来自主包:
func startParsingFile(fileName: String, fileType: String) {
guard let urlPath = Bundle.main.path(forResource: fileName, ofType: fileType) else {
print("Can't load file \(fileName).\(fileType)")
return
}
guard let url:URL = URL(string: urlPath) else {
print("Error on create URL to read file")
return
}
self.xmlParser = XMLParser(contentsOf: url)
self.xmlParser.delegate = self
let result = self.xmlParser.parse()
print("parse from file result: \(result)")
if result == false {
print(xmlParser.parserError?.localizedDescription)
}
}
从应用程序包加载时出错:
parse from file result: false
Optional("The operation couldn’t be completed. (Cocoa error -1.)")
您是说:
guard let urlPath = Bundle.main.path(forResource: fileName, ofType: fileType) else {
print("Can't load file \(fileName).\(fileType)")
return
}
guard let url:URL = URL(string: urlPath) else {
print("Error on create URL to read file")
return
}
首先,将字符串路径变成URL是非常愚蠢的。你知道你想要 URL,那你为什么不先调用 url(forResource:...)
?
其次,如果你曾经做把一个字符串路径变成URL,你必须做一个file URL.