//찾아볼것
//1. 배열, 딕셔너리
//2. xml 파서
//3. guard
import UIKit
class ViewController: UIViewController,UITableViewDataSource,XMLParserDelegate {
//배열에 딕셔너리를 넣었다.
var datalist = [[String:String]]()
var detaildata = [String:String]()
var elementTemp:String = ""
var blank = true
override func viewDidLoad() {
super.viewDidLoad()
let urlString = "https://raw.githubusercontent.com/ChoiJinYoung/iphonewithswift2/master/weather.xml"
//print(URL(string: urlString)!)
guard let baseURL = URL(string: urlString) else {
print("url error")
return
}
guard let parser = XMLParser(contentsOf: baseURL) else {
print("cant read data")
return
}
parser.delegate = self
if !parser.parse(){
print("parser failure")
}
}
/*
//로컬부터 로컬까지 하나의 딕셔너리에 들어간다.
didstartelement:weatherinfo
foundCharacters:
didstartelement:local
foundCharacters:
didstartelement:country
foundCharacters:한국
didEndElement:country
foundCharacters:
didstartelement:weather
foundCharacters:비
didEndElement:weather
foundCharacters:
didstartelement:temperature
foundCharacters:20
didEndElement:temperature
foundCharacters:
didEndElement:local
foundCharacters:
*/
//parser가 시작 태그를 만나면 호출된다.
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
//print("didstartelement:"+elementName) //local
elementTemp = elementName
blank = true
print("start:\(elementName)")
}
//현재 태그에 담겨있는 string값이 전달된다.
func parser(_ parser: XMLParser, foundCharacters string: String) {
//print("foundCharacters:"+string)
if blank == true && elementTemp != "local" && elementTemp != "weatherinfo"{
detaildata[elementTemp] = string.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
}
//parser가 닫는 태그를 만나면 호출된다.
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
//print("didEndElement:"+elementName)
if elementName == "local"{
datalist += [detaildata]
}
blank = false
print("end:\(elementName)")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datalist.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! WeatherViewCell
var dicTemp = datalist[indexPath.row]
cell.contryLable.text = dicTemp["country"]
let weatherStr = dicTemp["weather"]
cell.weatherLable.text = weatherStr
cell.temLable.text = dicTemp["temperatur"]
if weatherStr == "맑음" {
cell.imgView.image = UIImage(named:"sunny.png")
}else if weatherStr == "비" {
cell.imgView.image = UIImage(named:"rainy.png")
}else if weatherStr == "흐림" {
cell.imgView.image = UIImage(named:"cloudy.png")
}else if weatherStr == "눈" {
cell.imgView.image = UIImage(named:"snow.png")
}else {
cell.imgView.image = UIImage(named:"blizzard.png")
}
return cell
}
}
//// WeatherViewCell.swift
//
// WeatherViewCell.swift
// XML
//
// Created by MacBookPro on 2017. 12. 18..
// Copyright © 2017년 MacBookPro. All rights reserved.
//
import UIKit
class WeatherViewCell: UITableViewCell {
@IBOutlet weak var contryLable: UILabel!
@IBOutlet weak var weatherLable: UILabel!
@IBOutlet weak var temLable: UILabel!
@IBOutlet weak var imgView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
/*
//로컬부터 로컬까지 하나의 딕셔너리에 들어간다.
didstartelement:weatherinfo
foundCharacters:
didstartelement:local
foundCharacters:
didstartelement:country
foundCharacters:한국
didEndElement:country
foundCharacters:
didstartelement:weather
foundCharacters:비
didEndElement:weather
foundCharacters:
didstartelement:temperature
foundCharacters:20
didEndElement:temperature
foundCharacters:
didEndElement:local
foundCharacters:
*/
'ios 뽀개기 > ios 응용해보기' 카테고리의 다른 글
php에서 myql파일 json으로 가져오기 (0) | 2017.12.19 |
---|---|
json parser (0) | 2017.12.18 |
페이지 뷰 (0) | 2017.12.15 |
탭뷰 (0) | 2017.12.15 |
맵뷰 map view (0) | 2017.12.14 |
댓글