我尝试在代码中使用可选绑定(bind),但在执行和调试时它仍然显示为零,所以我想知道我是否使用了错误的方法?
Here is the code which shows me trying to parse my JSON
这是我用来尝试解析 JSON 的代码:
import Foundation
protocol ListingModelProtocol: class {
func itemsDownloaded(items: NSArray)
}
class ListingModel: NSObject, NSURLSessionDataDelegate {
weak var delegate: ListingModelProtocol!
var data : NSMutableData = NSMutableData()
let urlPath: String = "http://booksmart.hol.es/service.php" // this will be changed to the path where service.php lives
func downloadItems() {
let url: NSURL = NSURL(string: urlPath)!
var session: NSURLSession!
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
let task = session.dataTaskWithURL(url)
task.resume()
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
self.data.appendData(data);
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
if error != nil {
print("Failed to download data")
}else {
print("Data downloaded")
self.parseJSON()
}
}
func parseJSON() {
var jsonResult: NSMutableArray = NSMutableArray()
do{
jsonResult = try NSJSONSerialization.JSONObjectWithData(self.data, options:NSJSONReadingOptions.AllowFragments) as! NSMutableArray
} catch let error as NSError {
print(error)
}
var jsonElement: NSDictionary = NSDictionary()
let properties: NSMutableArray = NSMutableArray()
for(var i = 0; i < jsonResult.count; i+=1)
{
jsonElement = jsonResult[i] as! NSDictionary
let property = PropertyModel()
//the following insures none of the JsonElement values are nil through optional binding
if let propertyType = jsonElement["Property Type"] as? String,
let price = jsonElement["Price"] as? String,
let distance = jsonElement["Distance"] as? String
{
property.propertyType = propertyType
property.price = price
property.distance = distance
}
properties.addObject(property)
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.delegate.itemsDownloaded(properties)
})
}
}
这是我用来从数据库下载数据的代码:
import Foundation
class PropertyModel: NSObject {
//properties
var propertyType: String?
var price: String?
var distance: String?
//empty constructor
override init()
{
}
//construct with @propertyType, @price and @distance parameters
init(propertyType: String, price: String, distance: String) {
self.propertyType = propertyType
self.price = price
self.distance = distance
}
//prints object's current state
override var description: String {
return "Property Type: \(propertyType), Price: \(price), Distance: \(distance)"
}
}
最后,这是我用来尝试将其快速放入表格单元格的代码:
import UIKit
class First_ResultsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, ListingModelProtocol {
//Properties
var feedItems: NSArray = NSArray()
var selectedProperties : PropertyModel = PropertyModel()
@IBOutlet weak var PropertyListTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
//set delegates and initialize homeModel
self.PropertyListTableView.delegate = self
self.PropertyListTableView.dataSource = self
let listingModel = ListingModel()
listingModel.delegate = self
listingModel.downloadItems()
// Do any additional setup after loading the view.
}
func itemsDownloaded(items: NSArray) {
feedItems = items
self.PropertyListTableView.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of feed items
return feedItems.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Retrieve cell
let cellIdentifier: String = "BasicCell"
let myCell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)!
// Get the location to be shown
let item: PropertyModel = feedItems[indexPath.row] as! PropertyModel
// Get references to labels of cell
myCell.textLabel!.text = item.propertyType
return myCell
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
最佳答案
您发布的以下代码和您的评论不正确。
//the following insures none of the JsonElement values are nil through optional binding
if let propertyType = jsonElement["Property Type"] as? String,
let price = jsonElement["Price"] as? String,
let distance = jsonElement["Distance"] as? String
以上并不能确保值不nil
。您的 if
语句正在检查是否所有此 jsonElement 都不 nil
那么它将输入并设置您的属性。
此外,如果上述任何属性在 json 响应中不是字符串,则不会进入 if
语句。您应该使用返回的类型来检查它。将 替换为?双倍
与您的 json 响应返回的类型。
if let propertyType = jsonElement["Property Type"] as? String,
let price = jsonElement["Price"] as? Double,
let distance = jsonElement["Distance"] as? Double {
property.propertyType = propertyType
property.price = "\(price)"
property.distance = "\(distance)"
}
如果您想在 nil
时将其设置为空字符串,您应该使用 as String ?? “”
。
//the following ensure that when the element is nil, we change it to a empty string and update our attributes
let propertyType = jsonElement["Property Type"] as? String ?? ""
let price = jsonElement["Price"] as? String ?? ""
let distance = jsonElement["Distance"] as? String? ??
property.propertyType = propertyType
property.price = price
property.distance = distance
您不再需要 if
语句,因为它不会为零。
关于ios - 我怎样才能确保 json 元素不为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39281122/