【问题标题】:NSXMLParser is not being called in Swift 2.0 (Could not open data stream)NSXMLParser 未在 Swift 2.0 中调用(无法打开数据流)
【发布时间】:2015-12-28 08:21:47
【问题描述】:

我正在尝试让 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)

【问题讨论】:

  • 实现parseErrorOccurred委托方法,看看是否有错误。
  • 感谢您的建议。产生的错误是 "Optional(Error Domain=NSCocoaErrorDomain Code=-1 "(null)" UserInfo={NSXMLParserErrorMessage=Could not open data stream})"
  • 正确的 url 被传递给解析器。我将进一步调查错误,但这让我指出了正确的方向。
  • 我也遇到过这样的错误(Could not open data stream)。即使通过了正确的 url 并且它在 swift 1.X 中运行良好 ...

标签: ios swift2 nsxmlparser


【解决方案1】:

此问题的问题是最新版本 here 中的传输安全性。

所以你应该 NSAppTransportSecurity 字典到你的 info.plist。然后将 NSAllowsArbitraryLoads 键添加到该字典并将布尔值设置为 true。

<key>NSAppTransportSecurity</key>  
     <dict>  
          <key>NSAllowsArbitraryLoads</key><true/>  
     </dict>  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 2011-07-16
    • 1970-01-01
    相关资源
    最近更新 更多