testThis() 함수를 실행하여 모든 결과를 확인합니다. self.user.jsonTest는 변형 가능한 유형의 핵심 데이터입니다. Transformable CoreData 변수와 storeJSON으로 바꾸고 JSON을 검색하십시오. swiftyJSON을 사용하고 있습니다 만 사용하지 않으면 제거하는 것이 매우 쉽습니다.
func testThis() {
makeFakeData() {
self.retrieveJSON(self.user.jsonTest) {
json in
print("json: \(json)")
}
}
}
func storeJSON(dataToStore: [String: AnyObject], completion: (data: NSData?) -> Void) {
do {
let data = try NSJSONSerialization.dataWithJSONObject(dataToStore, options: [])
completion(data: data)
} catch let error as NSError {
print("NSJSONSerialization Error: \(error)")
completion(data: nil)
}
}
func retrieveJSON(dataToGet: NSObject?, completion: (json: JSON?) -> Void) {
if let data = dataToGet as? NSData {
do {
let nsJSON = try NSJSONSerialization.JSONObjectWithData(data, options: [])
completion(json: JSON(nsJSON))
} catch let error as NSError {
print("NSJSONSerialization Error: \(error)")
completion(json: nil)
}
}
}
func makeFakeData(completion:() -> Void) {
var solarResourceDic: [String: String] = [:]
var srDics: [[String: String]!] = []
for i in 0..<5 {
solarResourceDic = [:]
solarResourceDic["system_capacity"] = "\(i)"
solarResourceDic["azimuth"] = "\(i + 1)"
solarResourceDic["tilt"] = "\(i + 2)"
solarResourceDic["array_type"] = "\(i + 3)"
solarResourceDic["module_type"] = "\(i + 4)"
solarResourceDic["losses"] = "\(i + 5)"
srDics.append(solarResourceDic)
}
let dic = ["Solar Resource": srDics]
storeJSON(dic) {
data in
self.user.jsonTest = data
appDelegate.coreData.saveContext()
}
completion()
}
관련 코드를 게시 –
주세요 'results_category_list = NSJSONSerialization JSONObjectWithData : 데이터 선택해 NSJSONReadingMutableLeaves 오류 & myError];' –
데이터로 저장하지 않으시겠습니까? 핵심 데이터 모델에서 유형을 BLOB로 변경하고, URL에서 반환 된 데이터를 저장하고, 필요할 때이를 dict 표현으로 변환 할 수 있습니까? – bennythemink