尝试离线保存数据。但是,获取错误无法将类型'[AnyObject?]'的值转换为预期的参数类型'AnyObject?'。无法弄清楚如何将AnyObject数组转换为String数组。谢谢你们的帮助。
// outside the function
var senderArray = [String]()
var messageArray = [String?]()
var photoArray = [UIImage?]()
// func.........
query.whereKey("downloaded", equalTo: false)
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in
if error != nil {
}
for object in objects! {
self.senderArray.append(object.objectForKey("sender") as! String)
self.messageArray.append(object.objectForKey("message") as? String)
if object.objectForKey("photo") != nil {
if let converyPFFile = object.objectForKey("photo") as? PFFile{
let temp = try! converyPFFile.getData()
let image = UIImage(data: temp)
self.photoArray.append(image)
}
} else {
self.photoArray.append(nil)
}
}
var tempLocalNameArray = [AnyObject?]()
var tempLocalMessageArray = [AnyObject?]()
var tempLocalImageArray = [AnyObject?]()
if NSUserDefaults.standardUserDefaults().arrayForKey("nameArray") != nil {
tempLocalNameArray = NSUserDefaults.standardUserDefaults().arrayForKey("nameArray")!
tempLocalMessageArray = NSUserDefaults.standardUserDefaults().arrayForKey("messageArray")!
tempLocalImageArray = NSUserDefaults.standardUserDefaults().arrayForKey("imageArray")!
}
for i in 0 ..< self.senderArray.count {
tempLocalNameArray.append(self.senderArray[i])
tempLocalMessageArray.append(self.messageArray[i])
tempLocalImageArray.append(self.photoArray[i])
}
// error highlighted
NSUserDefaults.standardUserDefaults().setObject(tempLocalNameArray, forKey: "nameArray")
// error highlighted
NSUserDefaults.standardUserDefaults().setObject(tempLocalMessageArray, forKey: "messageArray")
// error highlighted
NSUserDefaults.standardUserDefaults().setObject(tempLocalImageArray, forKey: "imageArray")
self.loadChat()
}
答案 0 :(得分:0)
您正尝试使用NSUserDefaults存储图像。但NSUserDefaults不存储任何类型的数据。请阅读documentation:
value参数只能是属性列表对象:NSData, NSString,NSNumber,NSDate,NSArray或NSDictionary。对于NSArray和 NSDictionary对象,其内容必须是属性列表对象。
此article可能会对您有所帮助。