Swift 2.0 中未调用 NSXMLParser(无法打开数据流)
NSXMLParser is not being called in Swift 2.0 (Could not open data stream)
我正在尝试让 NSXMLParser 在 Swift 2.0 中工作。我在 Swift 1 中有一个可用的解析器。我迁移到 Swift 2.0,现在解析器无法正常启动。
init(urlToUse: String)
{
self.eventFeedURL = urlToUse
super.init()
}
func beginParsing() {
// this should kick off the parsing functionality
// lastDate starts off as the current date/time and will be used later to update available articles
var lastDate = NSDate()
let feedURL:NSURL = NSURL(string: self.eventFeedURL)!
let feedParser:NSXMLParser? = NSXMLParser(contentsOfURL: feedURL)
if let actualFeedParser = feedParser {
// Download feed and parse out
actualFeedParser.delegate = self
actualFeedParser.parse()
}
}
func getAvailableEvents() -> [Event] {
return self.availableEvents
}
func updateAvailableEvents(newEvents:[Event]) {
// add the new day's events to the avaiable events
self.availableEvents += newEvents
// notify that new events are available from this parser
notificationCenter.postNotificationName("CityOfBoston_EventsUpdated", object: self)
}
// XML parsing functions specific to this feed
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
// add any other XML tags you care about to the if statement below (via || (or) logic)
if elementName == "item" || elementName == "title" || elementName == "link" || elementName == "description" || elementName == "category" {
self.currentElement = elementName
self.attributes = attributeDict
}
if elementName == "item" {
// Start a new event
self.currentlyConstructingEvent = Event()
}
}
func parser(parser: NSXMLParser, foundCharacters string: String) {
// add any other XML tags you care about to the if statement below (via || (or) logic)
if self.currentElement == "item" ||
self.currentElement == "title" ||
self.currentElement == "description" ||
self.currentElement == "link" ||
self.currentElement == "category"{
self.foundCharacters += string
}
}
func parserDidEndDocument(parser: NSXMLParser) {
notificationCenter.postNotificationName("CityOfBoston_ParserFinished", object: self)
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if elementName == "title" {
// Parsing of the title element is complete, save the data
// update with appropriate title formatting
foundCharacters = foundCharacters.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
foundCharacters = foundCharacters.stringByReplacingOccurrencesOfString("'", withString: "'", options: [], range: nil)
let title:String = foundCharacters.stringByReplacingOccurrencesOfString(""", withString: "\"", options: [], range: nil)
self.currentlyConstructingEvent.setEventTitle(title)
此问题的问题是最新版本 here 中的传输安全性。
所以您应该将 NSAppTransportSecurity 字典添加到您的 info.plist。然后将 NSAllowsArbitraryLoads 键添加到该字典并将布尔值设置为 true。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>
我正在尝试让 NSXMLParser 在 Swift 2.0 中工作。我在 Swift 1 中有一个可用的解析器。我迁移到 Swift 2.0,现在解析器无法正常启动。
init(urlToUse: String)
{
self.eventFeedURL = urlToUse
super.init()
}
func beginParsing() {
// this should kick off the parsing functionality
// lastDate starts off as the current date/time and will be used later to update available articles
var lastDate = NSDate()
let feedURL:NSURL = NSURL(string: self.eventFeedURL)!
let feedParser:NSXMLParser? = NSXMLParser(contentsOfURL: feedURL)
if let actualFeedParser = feedParser {
// Download feed and parse out
actualFeedParser.delegate = self
actualFeedParser.parse()
}
}
func getAvailableEvents() -> [Event] {
return self.availableEvents
}
func updateAvailableEvents(newEvents:[Event]) {
// add the new day's events to the avaiable events
self.availableEvents += newEvents
// notify that new events are available from this parser
notificationCenter.postNotificationName("CityOfBoston_EventsUpdated", object: self)
}
// XML parsing functions specific to this feed
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
// add any other XML tags you care about to the if statement below (via || (or) logic)
if elementName == "item" || elementName == "title" || elementName == "link" || elementName == "description" || elementName == "category" {
self.currentElement = elementName
self.attributes = attributeDict
}
if elementName == "item" {
// Start a new event
self.currentlyConstructingEvent = Event()
}
}
func parser(parser: NSXMLParser, foundCharacters string: String) {
// add any other XML tags you care about to the if statement below (via || (or) logic)
if self.currentElement == "item" ||
self.currentElement == "title" ||
self.currentElement == "description" ||
self.currentElement == "link" ||
self.currentElement == "category"{
self.foundCharacters += string
}
}
func parserDidEndDocument(parser: NSXMLParser) {
notificationCenter.postNotificationName("CityOfBoston_ParserFinished", object: self)
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if elementName == "title" {
// Parsing of the title element is complete, save the data
// update with appropriate title formatting
foundCharacters = foundCharacters.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
foundCharacters = foundCharacters.stringByReplacingOccurrencesOfString("'", withString: "'", options: [], range: nil)
let title:String = foundCharacters.stringByReplacingOccurrencesOfString(""", withString: "\"", options: [], range: nil)
self.currentlyConstructingEvent.setEventTitle(title)
此问题的问题是最新版本 here 中的传输安全性。
所以您应该将 NSAppTransportSecurity 字典添加到您的 info.plist。然后将 NSAllowsArbitraryLoads 键添加到该字典并将布尔值设置为 true。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>