http://www.theappguruz.com/tutorial/xml-parsing-using-nsxmlparse-swift/
위 사이트를 참고했는데, 코드가 여러모로 안 맞아, 지금 시간 까지 하고 있다.
여러 사이트를 뒤지다가 elementName 이 안 맞는 것을 찾아내어 강제 매핑 시켰다.
Table View Cell 에서 Identifier 도 Cell로 지정해 줘야 한다.
//
// bbsTableViewController.swift
// SidebarMenu
//
// Created by YeongSik Pak on 2015. 4. 11..
// Copyright (c) 2015년 AppCoda. All rights reserved.
//
import UIKit
class bbsTableViewController: UITableViewController, NSXMLParserDelegate {
var strXMLData:String = ""
var currentElement:String = ""
var passData:Bool=false
var passName:Bool=false
var parser = NSXMLParser()
var posts = NSMutableArray()
var elements = NSMutableDictionary()
var element = NSString()
var title1 = NSMutableString()
var date = NSMutableString()
@IBOutlet weak var menuButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
if self.revealViewController() != nil {
menuButton.target = self.revealViewController()
menuButton.action = "revealToggle:"
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
// Uncomment to change the width of menu
//self.revealViewController().rearViewRevealWidth = 62
}
var url:String="http://blog.rss.naver.com/suritam9.xml"
var urlToSend: NSURL = NSURL(string: url)!
// Parse the XML
posts = []
parser = NSXMLParser(contentsOfURL: urlToSend)!
parser.delegate = self
parser.parse()
}
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject]) {
self.currentElement = elementName
if (elementName as NSString).isEqualToString("item")
{
elements = NSMutableDictionary.alloc()
elements = [:]
title1 = NSMutableString.alloc()
title1 = ""
date = NSMutableString.alloc()
date = ""
}
}
func parser(parser: NSXMLParser, foundCharacters string: String?)
{
println(currentElement)
if currentElement == "title" {
title1.appendString(string!)
} else if currentElement == "link" {
date.appendString(string!)
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
{
if (elementName as NSString).isEqualToString("item") {
if !title1.isEqual(nil) {
elements.setObject(title1, forKey: "title")
}
if !date.isEqual(nil) {
elements.setObject(date, forKey: "link")
}
posts.addObject(elements)
}
}
func parser(parser: NSXMLParser, parseErrorOccurred parseError: NSError) {
NSLog("failure error: %@", parseError)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return posts.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! BbsTableViewCell
cell.TitleLabel.text = posts.objectAtIndex(indexPath.row).valueForKey("title") as! NSString as String
cell.DetailLabel.text = posts.objectAtIndex(indexPath.row).valueForKey("link") as! NSString as String
return cell as BbsTableViewCell
}
}
댓글 달기