- model to json swift3
protocol JSONRepresentable {
var JSONRepresentation: AnyObject { get }
}
protocol JSONSerializable: JSONRepresentable {
}
extension JSONSerializable {
var JSONRepresentation: AnyObject {
var representation = [String: AnyObject]()
for case let (label?, value) in Mirror(reflecting: self).children {
switch value {
case let value as JSONRepresentable:
representation[label] = value.JSONRepresentation
case let value as NSObject:
representation[label] = value
default:
// Ignore any unserializable properties
break
}
}
return representation as AnyObject
}
}
extension JSONSerializable {
func toJSON() -> String? {
let representation = JSONRepresentation
guard JSONSerialization.isValidJSONObject(representation) else {
return nil
}
do {
let data = try JSONSerialization.data(withJSONObject: representation, options: [])
return String(data: data, encoding: String.Encoding.utf8)
} catch {
return nil
}
}
}
- tableview选中背景
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
取消选中效果
tableView.deselectRow(at: indexPath, animated: false)