【发布时间】:2018-10-11 05:50:39
【问题描述】:
我需要从 MYSQL 中获取 JSON 值并将其显示在表视图中。我已经完成了 PHP 部分来接收数据,但我的 XCODE 有错误。我的数据将存储传感器的温度、湿度和数字状态(开关)。我有以下问题:
- 如何获取 ON/OFF JSON 值并存储在 Bool 数组中?
- 如何获取 JSON 值并存储在 Float 数组中?
- 如果 JSON 值为 ,如何在 XCODE 中将值存储为 0?
class DataManager {
var nodenameArray: [String] = []
var nodeidArray: [String] = []
var tempArray: [Float] = []
var humArray: [Float] = []
var pirArray: [Bool] = []
var lightArray: [Bool] = []
var relayArray: [Bool] = []
var hallArray: [Bool] = []
var smokeArray: [Bool] = []
@objc func taskdo() {
self.nodenameArray = []
self.nodeidArray = []
self.tempArray = []
self.humArray = []
self.pirArray = []
self.lightArray = []
self.relayArray = []
self.hallArray = []
self.smokeArray = []
if userlogin == true {
let request = NSMutableURLRequest(url: NSURL(string: http://talectric.com/wp-admin/a_p/iot/read_all.php")! as URL)
request.httpMethod = "POST"
let postString = "username=\(username)&password=\(password)&authen=wdwfesf9329140dsvfxkciospdkm"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(error!)")
return
} else {
do {
if let respondString = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
print(respondString)
if let nodedata = respondString.value(forKey: "nodedata") as? NSArray {
for node in nodedata{
if let nodeDict = node as? NSDictionary {
if let nodeid = nodeDict.value(forKey: "node_id") {
self.nodeidArray.insert(nodeid as! String, at: 0)
}
if let nodeid = nodeDict.value(forKey: "node_name") {
self.nodenameArray.insert(nodeid as! String, at: 0)
}
if let nodeid = nodeDict.value(forKey: "temp") {
self.tempArray.insert(Float(nodeid as! String)!, at: 0)
}
if let nodeid = nodeDict.value(forKey: "hum") {
print(nodeid)
self.humArray.insert(Float(Int(nodeid as! String)!), at: 0)
}
}
}
}
}
print(self.nodenameArray)
print(self.nodeidArray)
print(self.tempArray)
print(self.humArray)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
}
catch let error as NSError {
print(error.debugDescription)
}
}
}
task.resume()
}
}
}
下面是回复:
{
nodedata = (
{
"hall_status" = "<null>";
hum = 111;
"light_status" = "<null>";
"node_id" = y2cfwecrw3hqznuxmfvf;
"node_name" = SVIN03;
"pir_status" = OFF;
"relay_status" = "<null>";
"smoke_status" = "<null>";
temp = 2132;
},
{
"node_name" = SVIN04;
nodeid = aj2w1aljw8nd65ax79dm;
},
{
"hall_status" = "<null>";
hum = 100;
"light_status" = "<null>";
"node_id" = mwmfl2og2l8888fjpj2d;
"node_name" = SVIN05;
"pir_status" = ON;
"relay_status" = "<null>";
"smoke_status" = "<null>";
temp = 45;
}
);
numberofnodeid = 3;
}
111 100
无法将“__NSCFNumber”(0x10db6f878)类型的值转换为“NSString”(0x10ca71568)。 2018-10-11 08:11:05.352491+0700 IOT 2[1506:101847] 无法将“__NSCFNumber”(0x10db6f878)类型的值转换为“NSString”(0x10ca71568)。
错误在这一行:
self.humArray.insert(Float(Int(nodeid as! String)!), at: 0)
【问题讨论】:
-
Could not cast value of type '__NSCFNumber' (0x10db6f878) to 'NSString' (0x10ca71568).+ Lineself.humArray.insert(Float(Int(nodeid as! String)!), at: 0)表明nodeid是NSNumber而不是String,因为nodeDict.value(forKey: "temp")不是String。 -
一般来说:避免使用强制展开(使用
!)。不要使用所有这些数组来获得同步值。取而代之的是使用自定义结构/类,在 Swift 4 中也可以使用 Codable 可能是一个有趣的想法。不要在 Swift3+ 中使用 NSStuff,当 Swift 中有等价物时:NSMutableURLRequest=>URLRequest(这也将避免稍后的as URLRequest,NSURL=>URL... -
你得到
temp和hum作为Int所以使用NSNumber而不是NSString作为self.humArray.insert(Float(Int(nodeid as! NSNumber)!), at: 0)以上参数 -
@Larme 你的意思是我不应该使用这段代码,对吧?如果让 nodedata = respondString.value(forKey: "nodedata") as? NSArray { for node in nodedata{ if let nodeDict = node as? NSDictionary { if let nodeid = nodeDict.value(forKey: "node_id"){ self.nodeidArray.insert(nodeid as!String, at: 0) }
标签: ios arrays json swift xcode