Pretty simple question - how to store Images, Strings and Doubles temporally in Swift?
My app gets ID from server, using this ID app can request from server to download image and then with another request can get String and Double. Then app has to show image with corresponding String and Double. All downloaded data should be deleted once app is killed and downloaded again once app starts.
Does using custom class would be a good idea?
For example, custome class:
class UserInformation: NSObject {
var string: String
var double: Double
var image: String
init(string: String, double: Double, image: String) {
self.string = string
self.double = double
self.image = image
}
}
Then to save and access data:
var info = [UserInformation]()
func save() {
//Alamofire functions to download image and string, double values goes here
var newImage = //Downloaded image from server
var newString = //Downloaded String from server
var newDouble = //Downloaded Double from server
//Save image
let imageName = NSUUID().UUIDString
let imagePath = getDocumentsDirectory().stringByAppendingPathComponent(imageName)
if let jpegData = UIImageJPEGRepresentation(newImage, 80) {
jpegData.writeToFile(imagePath, atomically: true)
}
let userInfo = UserInformation(string: newString, double: newDouble, image: imageName)
info.append(userInfo)
}
func load() {
//For ex. I am using [0], although in my app
//it would be for _ in _ loop to access all existing values
let info = UserInformation[0]
string.text = info.string
double.double = info.double
let path = getDocumentsDirectory().stringByAppendingPathComponent(info.image)
imageView.image = UIImage(contentsOfFile: path)
}
func getDocumentsDirectory() -> NSString {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
return documentsDirectory
}
EDIT I realize that using NSUserData (as in my example) is possible way to store data, what I am unsure if that is a good idea, in my case