urlQueryAllowed (Returns the character set for characters allowed in a query URL component.) 타입으로 인코딩 해준다.
let region = "종로구"
url += region.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
JSONSerialization클래스를 사용하여 JSON을 Foundation 개체로 변환하고 Foundation 개체를 JSON으로 변환합니다.
var responses = [String: Any]()
var bodys = [String: Any]()
var items = [[String : Any]]()
guard let data = data else {
return
}
// 받아온 json을 [String: Any]로 변환하겠다.
guard let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any] else{
return
}
// 받아온 Json 파일
// {"response":
// {"body":
// {"items":[
// response -> body -> items에 데이터가 있을 때
responses = json["response"] as! [String : Any]
bodys = responses["body"] as! [String : Any]
items = bodys["items"] as! [[String : Any]]
func loadFineDust() -> Observable<String>{
return Observable.create{
return Disposables.create()
}
}
func loadFineDust() -> Observable<String>{
return Observable.create{ emitter in
URLSession.shared.dataTask(with: URL(string: url)!) { data, res, err in
if let err = err {
emitter.onError(err) // error 보내기
return
}
guard let data = data else {
return
}
var responses = [String: Any]()
var bodys = [String: Any]()
var items = [[String : Any]]()
guard let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any] else{
return
}
responses = json["response"] as! [String : Any]
bodys = responses["body"] as! [String : Any]
items = bodys["items"] as! [[String : Any]]
let item: [String : Any] = items[0]
let finedust: String = item["pm10Value"] as! String
print(finedust)
emitter.onNext(finedust)
emitter.onCompleted()
}.resume() // resume()을 통해 task 실행
emitter.onCompleted()
return Disposables.create()
}
}
let disposeBag = DisposeBag()
loadFineDust()
.subscribe(onNext: { response in
print(response)
}, onError: { err in
print(err)
}).disposed(by: disposeBag) // disposBag에 담기
func loadFineDust() -> Observable<String>{
return Observable.create{ emitter in
URLSession.shared.dataTask(with: URL(string: url)!) { data, res, err in
if let err = err {
emitter.onError(err)
return
}
guard let data = data else {
return
}
var responses = [String: Any]()
var bodys = [String: Any]()
var items = [[String : Any]]()
guard let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any] else{
return
}
responses = json["response"] as! [String : Any]
bodys = responses["body"] as! [String : Any]
items = bodys["items"] as! [[String : Any]]
let item: [String : Any] = items[0]
let finedust: String = item["pm10Value"] as! String
print(finedust)
emitter.onNext(finedust)
emitter.onCompleted()
}.resume()
emitter.onCompleted()
return Disposables.create()
}
}
loadFineDust()
.subscribe(onNext: { response in
print(response)
}, onError: { err in
print(err)
}).disposed(by: disposeBag)
RxSwift를 공부하기 위해 간단한 미세먼지 앱을 만드려고 한다. 주변에 있는 측정소를 기준으로 미세먼지 데이터를 가져오려고 했으나 엉뚱한 값을 가져와 어떤 방법을 사용해야 될지 고민중이다. 내일은 Alamofire도 사용해봐야쥐 ..