How save tableview data in swift MVC?
How can i save data in table on another ViewController?
I have a custom class called ToDoItem.
func save() {
let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: ToDoItem.self)
UserDefaults.standard.set(encodedData, forKey: "todosave")
UserDefaults.standard.synchronize()
}
//
func load() {
if let decoded = UserDefaults.standard.object(forKey: "todosave") {
let _ = NSKeyedUnarchiver.unarchiveObject(with: decoded as! Data) as! [ToDoItem]
}
}
I've tried saving it with NSUserDefaults but I get an error.
ToDoItem.swift
import Foundation
class ToDoItem: NSObject, NSCoding {
var id: String
var image: String
var title: String
var date: Date
init(id: String, image: String, title: String, date: Date) {
self.id = id
self.image = image
self.title = title
self.date = date
}
required convenience init(coder aDecoder: NSCoder) {
let id = aDecoder.decodeObject(forKey: "id") as! String
let image = aDecoder.decodeObject(forKey: "image") as! String
let title = aDecoder.decodeObject(forKey: "title") as! String
let date = aDecoder.decodeObject(forKey: "date") as! Date
self.init(id: id, image: image, title: title, date: date)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(id, forKey: "id")
aCoder.encode(image, forKey: "image")
aCoder.encode(title, forKey: "title")
aCoder.encode(date, forKey: "date")
}
}
I would suggest using Object Mapper and then you can convert the object to JSON, after convert you save it to User Defaults as normally with type Any. Then for loading data, you map the data from the User Defaults key that you saved before to your TODOItem.
The link for ObjectMapper: https://github.com/Hearst-DD/ObjectMapper
Also you can use this library for mapping different JSONs to your desired objects
Related
I'm having trouble archiving and/or unarchiving (not sure where the problem is, exactly) a set of custom classes from the iOS documents directory. The set is saved to disk (or at least it appears to be saved) because I can pull it from disk but I cannot unarchive it.
The model
final class BlockedUser: NSObject, NSSecureCoding {
static var supportsSecureCoding = true
let userId: String
let name: String
let date: Int
var timeIntervalFormatted: String?
init(userId: String, name: String, date: Int) {
self.userId = userId
self.name = name
self.date = date
}
required convenience init?(coder: NSCoder) {
guard let userId = coder.decodeObject(forKey: "userId") as? String,
let name = coder.decodeObject(forKey: "name") as? String,
let date = coder.decodeObject(forKey: "date") as? Int else {
return nil
}
self.init(userId: userId, name: name, date: date)
}
func encode(with coder: NSCoder) {
coder.encode(userId, forKey: "userId")
coder.encode(name, forKey: "name")
coder.encode(date, forKey: "date")
}
}
Writing to disk
let fm = FileManager.default
let dox = fm.urls(for: .documentDirectory, in: .userDomainMask)[0]
let dir = dox.appendingPathComponent("master.properties", isDirectory: true)
do {
let userData: [URL: Any] = [
/* Everything else in this dictionary is a primitive type (string, bool, etc.)
and reads and writes without problem from disk. The only thing I cannot
get to work is the entry below (the set of custom classes). */
dir.appendingPathComponent("blockedUsers", isDirectory: false): blockedUsers // of type Set<BlockedUser>
]
for entry in userData {
let data = try NSKeyedArchiver.archivedData(withRootObject: entry.value, requiringSecureCoding: true)
try data.write(to: entry.key, options: [.atomic])
}
} catch {
print(error)
}
Reading from disk
if let onDisk = try? Data(contentsOf: dir.appendingPathComponent("blockedUsers", isDirectory: false)) {
if let blockedUsers = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(onDisk) as? Set<BlockedUser> {
print("success")
} else {
print("file found but cannot unarchive") // where I'm currently at
}
} else {
print("file not found")
}
The problem is that you are trying to decode an object instead of decoding an integer. Check this post. Try like this:
class BlockedUser: NSObject, NSSecureCoding {
static var supportsSecureCoding = true
let userId, name: String
let date: Int
var timeIntervalFormatted: String?
init(userId: String, name: String, date: Int) {
self.userId = userId
self.name = name
self.date = date
}
func encode(with coder: NSCoder) {
coder.encode(userId, forKey: "userId")
coder.encode(name, forKey: "name")
coder.encode(date, forKey: "date")
coder.encode(timeIntervalFormatted, forKey: "timeIntervalFormatted")
}
required init?(coder: NSCoder) {
userId = coder.decodeObject(forKey: "userId") as? String ?? ""
name = coder.decodeObject(forKey: "name") as? String ?? ""
date = coder.decodeInteger(forKey: "date")
timeIntervalFormatted = coder.decodeObject(forKey: "timeIntervalFormatted") as? String
}
}
I have two model swift files under below.
// Item.swift
import UIKit
class Item: NSObject, NSCoding {
var name: String
var valueInDollars: Int
var serialNumber: String?
let dateCreated: Date
let itemKey: String
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: "name")
aCoder.encode(dateCreated, forKey: "dateCreated")
aCoder.encode(itemKey, forKey: "itemKey")
aCoder.encode(serialNumber, forKey: "serialNumber")
aCoder.encode(valueInDollars, forKey: "valueInDollars")
}
required init(coder aDecoder: NSCoder) {
name = aDecoder.decodeObject(forKey: "name") as! String
dateCreated = aDecoder.decodeObject(forKey: "dateCreated") as! Date
itemKey = aDecoder.decodeObject(forKey: "itemKey") as! String
serialNumber = aDecoder.decodeObject(forKey: "serialNumber") as! String?
valueInDollars = aDecoder.decodeInteger(forKey: "valueInDollars")
super.init()
}
}
// ItemStore.swift
import UIKit
class ItemStore {
var allItems = [Item]()
let itemArchiveURL: URL = {
let documentsDirectories =
FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentDirectory = documentsDirectories.first!
return documentDirectory.appendingPathComponent("items.archive")
}()
func saveChanges() -> Bool {
print("Saving items to: \(itemArchiveURL.path)")
return NSKeyedArchiver.archiveRootObject(allItems, toFile: itemArchiveURL.path)
}
}
These two model files confirming to NSCoding protocol and using archiveRootObject to archive the data.
But the archiveRootObject is deprecated, and the NSCoding is not as safe as the NSSecureCoding, how can I tweak the code to adjust all of these?
You can rewrite you saveChanges function to something like this:
func saveChanges() -> Bool {
print("Saving items to: \(itemArchiveURL.path)")
do {
let data = try NSKeyedArchiver.archivedData(withRootObject: allItems, requiringSecureCoding: false)
try data.write(to: itemArchiveURL)
}
catch {
print("Error archiving data: \(error)")
return false
}
return true
}
Using Swift 3, blog reader app reading from a MYSQL database using JSON and PHP. User has the ability to save the blog they want to keep getting updates from using a follow button (as well as unfollow button). Instead of saving the entire array, just trying to save the followed blogs ID so the app just finds the blogs id and shows that specific blog that the user followed.
This is the error I'm getting when loading the user defaults
Initializer for conditional binding must have Optional type, not 'Void' (aka '()')
This error is in func loadUserDefaults() in MainController.swift at first line if let data = UserDefaults..
After the user clicks the follow button, I move the cells between arrays, between sections in the tableview and then I call saveUserDefaults()
This is MainController.swift
var mainArray = [Blog]()
var followedArray = [Blog]()
var filteredArray = [Blog]()
var followedIdentifiers = Set<String>()
override func viewDidLoad() {
super.viewDidLoad()
// Receiving Data from Server
retrieveDataFromServer()
// NSCoding - Unarchiving Data (followedID)
loadUserDefaults()
}
// NSCoding: Archiving UserDefaults
func saveUserDefaults() {
// Saving to UserDefaults
let encodedData = NSKeyedArchiver.archivedData(withRootObject: self.followedIdentifiers)
UserDefaults.standard.setValue(encodedData, forKey: "followedID")
UserDefaults.standard.synchronize()
}
// NSCoding: Unarchiving UserDefaults *** ERROR IS HERE ***
func loadUserDefaults() {
// Unarchiving Data -- ERROR: THIS FIRST LINE --
if let data = UserDefaults.standard.setValue(Array(self.followedIdentifiers), forKey: "followedID"),
let myFollowedList = NSKeyedUnarchiver.unarchiveObject(with: data) as? [Blog] {
self.followedIdentifiers = myFollowedList
self.followedIdentifiers = Set(UserDefaults.standard.stringArray(forKey: "followedID")!)
} else {
print("Error/ Empty: (Loading UserDefaults (followedID))")
}
}
// Retrieving Data from Server
func retrieveDataFromServer() {
let getDataURL = "http://example.com/receiving.php"
let url: NSURL = NSURL(string: getDataURL)!
do {
let data: Data = try Data(contentsOf: url as URL)
let jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! NSMutableArray
// Clear the arrays
self.followedArray = [Blog]()
self.mainArray = [Blog]()
// Looping through jsonArray
for jsonObject in jsonArray {
if let blog = Blog(jsonObject:jsonObject as! [String : Any]) {
// Check if identifiers match
if followedIdentifiers.contains(blog.blogID) {
self.followedArray.append(blog)
} else {
self.mainArray.append(blog)
}
}
}
} catch {
print("Error: (Retrieving Data)")
}
myTableView.reloadData()
}
This is Blog.swift which handles all the blogs objects and NSCoding
class Blog: NSObject, NSCoding {
var blogName: String
var blogStatus1: String
var blogStatus2: String
var blogURL: String
var blogID: String
var blogType: String
var blogDate: String
var blogPop: String
private init (name: String,status1: String,status2: String,url: String,id: String,type: String,date: String,pop: String) {
blogName = name
blogStatus1 = status1
blogStatus2 = status2
blogURL = url
blogID = id
blogType = type
blogDate = date
blogPop = pop
super.init()
}
convenience init?(jsonObject: [String:Any]) {
guard let bID = jsonObject["id"] as? String,
let bName = jsonObject["blogName"] as? String,
let bStatus1 = jsonObject["blogStatus1"] as? String,
let bStatus2 = jsonObject["blogStatus2"] as? String,
let bURL = jsonObject["blogURL"] as? String,
let bType = jsonObject["blogType"] as? String,
let bDate = jsonObject["blogDate"] as? String,
let bPop = jsonObject["blogPop"] as? String
else {
print("Error: (Creating Blog Object)")
return nil
}
self.init(name: bName, status1: bStatus1, status2: bStatus2, url: bURL, id: bID, type: bType, date: bDate, pop: bPop)
}
convenience required init?(coder aDecoder: NSCoder) {
guard let blogName = aDecoder.decodeObject(forKey: "blogName") as? String,
let blogStatus1 = aDecoder.decodeObject(forKey: "blogStatus1") as? String,
let blogStatus2 = aDecoder.decodeObject(forKey: "blogStatus2") as? String,
let blogURL = aDecoder.decodeObject(forKey: "blogURL") as? String,
let blogID = aDecoder.decodeObject(forKey: "blogID") as? String,
let blogType = aDecoder.decodeObject(forKey: "blogType") as? String,
let blogDate = aDecoder.decodeObject(forKey: "blogDate") as? String,
let blogPop = aDecoder.decodeObject(forKey: "blogPop") as? String else {
print("Error: (Creating Blog Object)")
return nil
}
self.init(name: blogName, status1: blogStatus1, status2: blogStatus2, url: blogURL, id: blogID, type: blogType, date: blogDate, pop: blogPop)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(blogName, forKey: "blogName")
aCoder.encode(blogStatus1, forKey: "blogStatus1")
aCoder.encode(blogStatus2, forKey: "blogStatus2")
aCoder.encode(blogURL, forKey: "blogURL")
aCoder.encode(blogID, forKey: "blogID")
aCoder.encode(blogType, forKey: "blogType")
aCoder.encode(blogDate, forKey: "blogDate")
aCoder.encode(blogPop, forKey: "blogPop")
}
}
The line of code:
let data = UserDefaults.standard.setValue(Array(self.followedIdentifiers), forKey: "followedID")
makes no sense since setValue method doesn't return any values.
Looks like you need to replace it with (Swift 3):
let data = UserDefaults.standard.data(forKey: "followedID")
Updated with #closetCoder comment
The setValue function is a void function so it can't be assigned to the variable data since it has no return value.
Here is the documentation for UserDefaults from Apple: https://developer.apple.com/documentation/foundation/userdefaults
I have a model class User that I want to save in UserDefaults
import UIKit
class User: NSObject {
var name:String!
var email:String!
var userId:String!
var phone:String!
var admin_status:String!
var social_code:String!
var token:String!
var otp:String!
var forget_otp:String!
var p_img:String!
var created:String!
var status:String!
static var currentUser:User = User()
override init() {
super.init()
}
required init(coder aDecoder: NSCoder) {
self.name = aDecoder.decodeObject(forKey: "name") as! String
self.email = aDecoder.decodeObject(forKey: "email") as! String
self.userId = aDecoder.decodeObject(forKey: "userId") as! String
self.phone = aDecoder.decodeObject(forKey: "phone") as! String
self.admin_status = aDecoder.decodeObject(forKey: "admin_status") as! String
self.social_code = aDecoder.decodeObject(forKey: "social_code") as! String
self.token = aDecoder.decodeObject(forKey: "token") as! String
self.otp = aDecoder.decodeObject(forKey: "otp") as! String
self.forget_otp = aDecoder.decodeObject(forKey: "forget_otp") as! String
self.p_img = aDecoder.decodeObject(forKey: "p_img") as! String
self.created = aDecoder.decodeObject(forKey: "created") as! String
self.status = aDecoder.decodeObject(forKey: "status") as! String
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encode(name, forKey: "name")
aCoder.encode(email, forKey: "email")
aCoder.encode(userId, forKey: "userId")
aCoder.encode(phone, forKey: "phone")
aCoder.encode(admin_status, forKey: "admin_status")
aCoder.encode(social_code, forKey: "social_code")
aCoder.encode(token, forKey: "token")
aCoder.encode(otp, forKey: "otp")
aCoder.encode(forget_otp, forKey: "forget_otp")
aCoder.encode(p_img, forKey: "p_img")
aCoder.encode(created, forKey: "created")
aCoder.encode(status, forKey: "status")
}
}
Code to save and get from UserDefaults
class func setUserDefault(ObjectToSave : AnyObject? , KeyToSave : String)
{
let defaults = UserDefaults.standard
if (ObjectToSave != nil)
{
defaults.set(ObjectToSave, forKey: KeyToSave)
}
UserDefaults.standard.synchronize()
}
class func getUserDefault(KeyToReturnValye : String) -> AnyObject?
{
let defaults = UserDefaults.standard
if let name = defaults.value(forKey: KeyToReturnValye)
{
return name as AnyObject
}
return nil
}
Saving
let user:User = User()
user.name = json["data"]["first_name"].string
user.email = json["data"]["email"].string
user.phone = json["data"]["phone"].string
user.social_code = json["data"]["social_code"].string
user.admin_status = json["data"]["admin_status"].string
user.otp = json["data"]["otp"].string
user.forget_otp = json["data"]["forget_otp"].string
user.p_img = json["data"]["p_img"].string
user.status = json["data"]["status"].string
user.userId = json["data"]["id"].string
user.created = json["data"]["created"].string
Utilities.setUserDefault(ObjectToSave: user, KeyToSave: "user")
I also tried this
let encodedData = NSKeyedArchiver.archivedData(withRootObject: user)
UserDefaults.standard.set(encodedData, forKey: "User")
but it crashes because of static var currentUser:User = User()
how to fix this ?
As for NSCoding: you have wrong method signature. Change
func encodeWithCoder(aCoder: NSCoder)
to
func encode(with aCoder: NSCoder)
Also, your object should explicitly conform to the NSCoding protocol:
class User: NSObject, NSCoding {...}
As for UserDefaults, you can not store custom objects in the UserDefaults, only NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary (or similar types in Swift), see documentation
Have you tried by replacing your this line,
static var currentUser:User = User()
with this line,
static var currentUser:User!
Error is crashing app when using NSCoder and NSKeyArchiver.
I had made a recent post around NSCoder but since then I've changed my code around and got a new error and decided a new post is best.
The app is a blog reader, reading from a MYSQL database using PHP to fill a table view with custom objects in Swift using JSON. I've been trying to save mainArray so that when the user moves cells across sections (each section has an array) it can save where the user left it.
Blog.swift: Handles the Blogs custom objects
import UIKit
class Blog: NSObject, NSCoding {
var blogName: String!
var blogStatus1: String!
var blogStatus2: String!
var blogURL: String!
var blogID: String!
var blogType: String!
var blogDate: String!
var blogPop: String!
static func createBlog(from jsonObject: AnyObject) -> Blog? {
guard let bID: String = jsonObject.object(forKey: "id") as? String,
let bName: String = jsonObject.object(forKey: "blogName") as? String,
let bStatus1: String = jsonObject.object(forKey: "blogStatus1") as? String,
let bStatus2: String = jsonObject.object(forKey: "blogStatus2") as? String,
let bURL: String = jsonObject.object(forKey: "blogURL") as? String,
let bType: String = jsonObject.object(forKey: "blogType") as? String,
let bDate: String = jsonObject.object(forKey: "blogDate") as? String,
let bPop: String = jsonObject.object(forKey: "blogPop") as? String
else {
print("Error: (Creating Blog Object)")
return nil
}
let blog = Blog()
blog.blogName = bName
blog.blogStatus1 = bStatus1
blog.blogStatus2 = bStatus2
blog.blogURL = bURL
blog.blogID = bID
blog.blogType = bType
blog.blogDate = bDate
blog.blogPop = bPop
return blog
}
// NSCoding
convenience required init?(coder aDecoder: NSCoder) {
self.init (coder : aDecoder) // *** Crashes Here ***
self.blogName = aDecoder.decodeObject(forKey: "blogName") as! String
self.blogStatus1 = aDecoder.decodeObject(forKey: "blogStatus1") as! String
self.blogStatus2 = aDecoder.decodeObject(forKey: "blogStatus2") as! String
self.blogURL = aDecoder.decodeObject(forKey: "blogURL") as! String
self.blogID = aDecoder.decodeObject(forKey: "blogID") as! String
self.blogType = aDecoder.decodeObject(forKey: "blogType") as! String
self.blogDate = aDecoder.decodeObject(forKey: "blogDate") as! String
self.blogPop = aDecoder.decodeObject(forKey: "blogPop") as! String
}
func encode(with aCoder: NSCoder) {
aCoder.encode(blogName, forKey: "blogName")
aCoder.encode(blogStatus1, forKey: "blogStatus1")
aCoder.encode(blogStatus2, forKey: "blogStatus2")
aCoder.encode(blogURL, forKey: "blogURL")
aCoder.encode(blogID, forKey: "blogID")
aCoder.encode(blogType, forKey: "blogType")
aCoder.encode(blogDate, forKey: "blogDate")
aCoder.encode(blogPop, forKey: "blogPop")
}
}
MainController.swift - Where table view is located
var mainArray = [Blog]()
var followedArray = [Blog]()
override func viewDidLoad() {
super.viewDidLoad()
// Receiving Data from Server
retrieveData()
if let data = UserDefaults.standard.data(forKey: "mainArrayKey"),
let myBlogList = NSKeyedUnarchiver.unarchiveObject(with: data) as? [Blog] {
mainArray = myBlogList
print("mainArray: \(mainArray)")
} else {
print("Error: (Saving to UserDefaults)")
}
}
// Retrieving Data from Server
func retrieveData() {
let getDataURL = "http://example.com/receiving.php"
let url: NSURL = NSURL(string: getDataURL)!
do {
let data: Data = try Data(contentsOf: url as URL)
let jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! NSMutableArray
// Looping through jsonArray
for jsonObject in jsonArray {
if let blog = Blog.createBlog(from: jsonObject as AnyObject) {
mainArray.append(blog)
// Save to UserDefaults
let encodedData = NSKeyedArchiver.archivedData(withRootObject: mainArray)
UserDefaults.standard.set(encodedData, forKey: "mainArrayKey")
}
}
}
catch {
print("Error: (Retrieving Data)")
}
myTableView.reloadData()
// Logs
print("This is mainArray", mainArray)
// Check UserDefaults
if UserDefaults.standard.object(forKey: "mainArrayKey") != nil{
print("mainArray key exists")
}
else {
print("mainArray key does not exist")
}
}
Looks like an infinite loop to me. You call init(coder:), and the first line calls init(coder:), and the first line calls init(coder:), and so on ad infinitum.
You need to call a different initializer inside it. Try self.init() instead.
As others have stated it's indeed an infinite loop. What you need to do is change it to self.init() and also add in the following to your code. Or implement your own init that does whatever needs to be done.
override init() {
super.init()
}