まず、foundatationをimportします。
import UIKit import Foundation
続いて、jsonを用意します。
let jsonString:String = "{\"dollaryen\":109, \"euroen\":110}" var code:Int = 0
prepareでjsonを取得して計算します。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { guard let identifier = segue.identifier else { return } let personalData: Data = self.jsonString.data(using: String.Encoding.utf8)! do { let items = try JSONSerialization.jsonObject(with: personalData) as! Dictionary<String,Any> self.code = items["dollaryen"] as! Int } catch { self.code = 1 } if identifier == "showResult" { let CalcVC = segue.destination as! CalcViewController CalcVC.mySales = Int(Double(self.sales.text!)! / Double(self.code)) CalcVC.myProfit = Int(Double(self.profit.text!)! / Double(self.code)) CalcVC.myPrice = Int(self.price.text!)! CalcVC.myEPS = Double(self.eps.text!)! } }
あれ、エラーが出まくってる。。。
2018-06-03 18:42:56.527314+0900 account[17768:334563] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/mac/Library/Developer/CoreSimulator/Devices/EA305F63-7A92-4933-9E03-89D80DFE7553/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2018-06-03 18:42:56.539423+0900 account[17768:334563] [MC] Reading from private effective user settings.
2018-06-03 18:42:58.597654+0900 account[17768:334563] [Common] _BSMachError: port 8c03; (os/kern) invalid capability (0x14) “Unable to insert COPY_SEND”
2018-06-03 18:43:12.349999+0900 account[17768:338350] XPC connection interrupted
buildはsucceedするが、うまくいかないです。