在Swift 2.0中没有调用NSXMLParser(无法打开数据流)

时间:2015-09-30 13:53:34

标签: ios swift2 nsxmlparser

我试图让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)

1 个答案:

答案 0 :(得分:1)

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

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

 import android.app.Activity;
 import android.os.Bundle;
 import android.text.TextUtils.TruncateAt;
 import android.widget.TextView;

 public class MainActivity extends Activity {
 private TextView textview;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textview = (TextView) this.findViewById(R.id.textview);
    textview.setSelected(true);

    textview.setEllipsize(TruncateAt.MARQUEE);
    textview.setSingleLine(true);
 }

 }