I have a project in XCODE 8 (SWIFT 3) where I try to collect user input from two screens and create an instance of a class. You enter some information on the first scene and hit next button on top right which will perform a segue to a new scene where user sets another property by tapping a CollectionViewCell in a CollectionView that is also under a ViewController. After user tapped the cell and hit next on top right again (or hit back button, return to the first scene to change some properties then hit next again to continue with the entry), it will save all the properties that user defined for that instance as say “transaction1”.
I use datasource and delegation methods to work with a CollectionView nested in a ViewController so it shouldn’t be a problem.
The problem I am having is that I don’t know how to save an individual entry to the whole class. Maybe something like [transaction] += [transaction1] will do but I am not sure how to implement it. After user saved a transaction entry, I will display all the transactions in relevant screens and also manipulate their properties so I need access to each property of every data entry as a “transaction” class.
I am throughly new to whole development process so I might be asking sth easy but I tried my best to deal with it believe me. I have the relevant code to make it more understandable below.
I thank the community for creating such a culture to share and augment ideas with a collective spirit. Its my first post so I couldn't leave that out :) But enough me, the code for class file is here: `
import UIKit
import os.log
class Transaction: NSObject, NSCoding {
// MARK: PROPERTIES
var amount: Int!
var note: String?
var image: UIImage?
var currency: String
var transactionType: transactionType!
var transactionMedium: String
var date: String
var budget: String?
//MARK: Archiving Paths
static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("transactions")
// MARK: TYPES
struct propertyKey {
static let amount = "amount"
static let note = "note"
static let image = "image"
static let currency = "currency"
static let transactionType = "transactionType"
static let transactionMedium = "transactionMedium"
static let date = "date"
static let budget = "budget"
}
// INITIALIZATON
init?(amount: Int!, note: String?, image: UIImage?, currency: String, transactionType: transactionType!, transactionMedium: String, date: String, budget: String?) {
// TRANSACTION INITIALIZATION FAIL SCENARIO
if amount == 0 {
return nil
}
// INITIALIZE STORED PROPERTIES
self.amount = amount
self.note = note
self.currency = currency
self.transactionType = transactionType
self.transactionMedium = transactionMedium
self.date = date
self.budget = budget
self.image = image
}
// MARK: NSCODING
func encode(with aCoder: NSCoder) {
aCoder.encode(amount, forKey: propertyKey.amount)
aCoder.encode(note, forKey: propertyKey.note)
aCoder.encode(currency, forKey: propertyKey.currency)
aCoder.encode(transactionType, forKey: propertyKey.transactionType)
aCoder.encode(transactionMedium, forKey: propertyKey.transactionMedium)
aCoder.encode(date, forKey: propertyKey.date)
aCoder.encode(budget, forKey: propertyKey.budget)
aCoder.encode(image, forKey: propertyKey.image)
}
required convenience init?(coder aDecoder: NSCoder) {
// The amount is required. If we cannot decode a name string, the initializer should fail.
guard let amount = aDecoder.decodeObject(forKey: propertyKey.amount) as? Int
else {
os_log("Unable to decode the amount for a Transaction object.", log: OSLog.default, type: .debug)
return nil
}
guard let currency = aDecoder.decodeObject(forKey: propertyKey.currency) as? String
else {
os_log("Unable to decode the currency for a Transaction object.", log: OSLog.default, type: .debug)
return nil
}
guard let transactionType = aDecoder.decodeObject(forKey: propertyKey.transactionType) as? transactionType!
else {
os_log("Unable to decode the transactionType for a Transaction object.", log: OSLog.default, type: .debug)
return nil
}
let note = aDecoder.decodeObject(forKey: propertyKey.note)
let budget = aDecoder.decodeObject(forKey: propertyKey.budget)
let image = aDecoder.decodeObject(forKey: propertyKey.image)
let date = aDecoder.decodeObject(forKey: propertyKey.date) as? String
let transactionMedium = aDecoder.decodeObject(forKey: propertyKey.transactionMedium) as? String
self.init(amount: amount, note: note as! String?, image: image as! UIImage?, currency: currency, transactionType: transactionType, transactionMedium: transactionMedium!, date: date!, budget: budget as! String?)
}
}
enum transactionType {
case income
case expense
}
`