let dic = NSMutableDictionary()
dic["age"] = 20
dic["name"] = "明月"
dic["address"] = nil
dic["weight"] = 1
dic["student"] = true
dic["list"] = ""
//let array = dic["list"] as! NSArray 强行拆包会崩溃
let list:NSArray = (dic["list"] as? NSArray)!
//dic[AnyHashable("age")] as! String 强行拆包会崩溃
let age = self.stringFromObject(object: dic["age"] as AnyObject)
MyPrint(message: age)
let name = self.stringFromObject(object: dic["name"] as AnyObject)
MyPrint(message: name)
let weight = self.stringFromObject(object: dic["weight"] as AnyObject)
MyPrint(message: weight)
let address = self.stringFromObject(object: dic["address"] as AnyObject)
MyPrint(message: address)
let address1 = dic["address"] as? String
MyPrint(message: address1)
let student = self.stringFromObject(object: dic["student"] as AnyObject)
MyPrint(message: student)
var isStudent = false
if student == "1" {
isStudent = true
}
MyPrint(message:isStudent)
}
//Int、float返回值为"\(value)"、bool返回值为"0","1"
func stringFromObject(object:AnyObject) -> String{
var value = ""
if let code = object as? Int{
value = "\(code)"
}else if let code = object as? CGFloat{
value = "\(code)"
}else if let code = object as? String{
value = code
}
//else if object is NSNull{
// value = ""
//}
return value
}