So yeah, I don't really like asking questions here as a beginner because many of you are pretty rough, even rude with people like me just starting out, but I want to learn so I will suck it up.
I am wondering if there is a way to reduce the repetitive code below (clean it up)?
I am aware I am asking a lot and there is a lot of code below, I am just eager to learn.
Thanks for any help.
Please be understanding!
UserDefaults.standard.set(machineODInputText.text, forKey: "TBMOD")
UserDefaults.standard.set(pipeODInputText.text, forKey: "pipeOD")
UserDefaults.standard.set(muckUpInputText.text, forKey: "muckUp")
UserDefaults.standard.set(jackingSpeedInputText.text, forKey: "speed")
UserDefaults.standard.set(pipeLengthInputText.text, forKey: "pipeLength")
UserDefaults.standard.set(driveLengthInputText.text, forKey: "driveLength")
UserDefaults.standard.set(noOfBenoBagsInputText.text, forKey: "noOfBenoBags")
UserDefaults.standard.set(weightOfBenoBagInputText.text, forKey: "weightOfBenoBag")
UserDefaults.standard.set(benoQtyForTanksInputText.text, forKey: "benoQtyForTanks")
UserDefaults.standard.set(noOfBenoBagsPerPalletInputText.text, forKey: "benoBagsPerPallet")
and
machineODInputText.inputAccessoryView = toolBar()
pipeODInputText.inputAccessoryView = toolBar()
pipeLengthInputText.inputAccessoryView = toolBar()
driveLengthInputText.inputAccessoryView = toolBar()
muckUpInputText.inputAccessoryView = toolBar()
jackingSpeedInputText.inputAccessoryView = toolBar()
weightOfBenoBagInputText.inputAccessoryView = toolBar()
noOfBenoBagsInputText.inputAccessoryView = toolBar()
benoQtyForTanksInputText.inputAccessoryView = toolBar()
noOfBenoBagsPerPalletInputText.inputAccessoryView = toolBar()
and
machineODInputText.clearsOnBeginEditing = true
pipeODInputText.clearsOnBeginEditing = true
pipeLengthInputText.clearsOnBeginEditing = true
driveLengthInputText.clearsOnBeginEditing = true
muckUpInputText.clearsOnBeginEditing = true
jackingSpeedInputText.clearsOnBeginEditing = true
weightOfBenoBagInputText.clearsOnBeginEditing = true
noOfBenoBagsInputText.clearsOnBeginEditing = true
benoQtyForTanksInputText.clearsOnBeginEditing = true
noOfBenoBagsPerPalletInputText.clearsOnBeginEditing = true
and finally
override func viewDidAppear(_ animated: Bool) {
if let a = UserDefaults.standard.object(forKey: "TBMOD") as? String {
machineODInputText.text = a
}
if let a = UserDefaults.standard.object(forKey: "pipeOD") as? String {
pipeODInputText.text = a
}
if let a = UserDefaults.standard.object(forKey: "muckUp") as? String {
muckUpInputText.text = a
}
if let a = UserDefaults.standard.object(forKey: "speed") as? String {
jackingSpeedInputText.text = a
}
if let a = UserDefaults.standard.object(forKey: "pipeLength") as? String {
pipeLengthInputText.text = a
}
if let a = UserDefaults.standard.object(forKey: "driveLength") as? String {
driveLengthInputText.text = a
}
if let a = UserDefaults.standard.object(forKey: "noOfBenoBags") as? String {
noOfBenoBagsInputText.text = a
}
if let a = UserDefaults.standard.object(forKey: "weightOfBenoBag") as? String {
weightOfBenoBagInputText.text = a
}
if let a = UserDefaults.standard.object(forKey: "benoQtyForTanks") as? String {
benoQtyForTanksInputText.text = a
}
if let a = UserDefaults.standard.object(forKey: "benoBagsPerPallet") as? String {
noOfBenoBagsPerPalletInputText.text = a
}
In this case, I think a good way to reduce code duplication would be to make a custom UITextField subclass, since a lot of the "duplicate" things you are doing are done to the text fields, and are really just initialising them.
I would create a subclass like this:
#IBDesignable
// you can definitely come up with a better name than I do...
class BentoniteInputField: UITextField {
#IBInspectable
var userDefaultKey: String = ""
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() {
inputAccessoryView = toolBar()
clearsOnBeginEditing = true
if let text = UserDefaults.standard.string(forKey: userDefaultKey) {
self.text = text
}
}
func toolBar() -> UIView {
// move the toolBar method here...
}
func writeToUserDefaults() {
UserDefaults.standard.set(text, forKey: userDefaultKey)
}
}
In the storyboard, you can set the text fields' class to this class:
then you can set the associated user default key for the text field in the storyboard too:
Also remember changing the types of the #IBOutlets!
Now we are only left with the code where you write to the user defaults. You can make that part a little less repetitive by putting all the text fields into an array and loop through that:
let textFields = [
machineODInputText,
pipeODInputText,
// ... list all the text fields
]
for textField in textFields {
textField.writeToUserDefaults()
}
Though personally, I wouldn't have minded just repeated writeToUserDefaults() for each text field.
Related
I am wondering how to save an array of objects from the following class:
class CustomDocument: NSObject, NSCoding {
let name : String
let image : UIImage
init(n: String, i: UIImage){
name = n
image = i
}
//other code excluded
}
Originally, I saved this array to User Defaults. Because the objects took up a lot of space, it caused a lot of lag in the app.
What is the best way to save an array of data that takes up a lot of space?
Thank you so much for the help and all responses are appreciated.
Try this code, Hope it helps:
class CustomDocument: NSObject, NSCoding {
var name : String?
var image : UIImage?
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: "namekey")
if let imageData = image!.jpegData(compressionQuality: 1.0){
aCoder.encode(imageData, forKey: "imagekey")
}
UserDefaults.standard.synchronize()
}
required convenience init?(coder aDecoder: NSCoder) {
self.init()
if let name = (aDecoder.decodeObject(forKey: "namekey") as? String){
self.name = name
}
if let imageData = (aDecoder.decodeObject(forKey: "imagekey") as? Data){
if let image = UIImage(data: imageData){
self.image = image
}
}
}
}
func archiveDocument(document:CustomDocument) -> Data? {
do {
let archivedObject = try NSKeyedArchiver.archivedData(withRootObject: document, requiringSecureCoding: false)
return archivedObject
} catch {
// do something with the error
}
return nil
}
func unarchiveDocument(unarchivedObject:Data) -> CustomDocument? {
do {
if let document = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(unarchivedObject) as? CustomDocument {
return document
}
} catch {
// do something with the error
}
return nil
}
Example:
//Set the object, also you can use an array instead of an object
let obj = CustomDocument()
obj.name = "doc1"
obj.image = UIImage(named: "my_image")
if let archivedObject = archiveDocument(document: obj){
UserDefaults.standard.set(archivedObject, forKey: "obj")
}
//Get the object
if let archivedObject = UserDefaults.standard.data(forKey: "obj"){
obj = unarchiveDocument(unarchivedObject: archivedObject)
let myImage = obj?.image
}
I have a class with some properties, and I am using encode while saving and decoder while fetching, but some of the data are not optional, but at the first time when I am fetching it's null so when the decoder is calling and it found nill value it crashes the app.
So what I want is when I fetch the data and if no data found I can set some default value instead of crashing I can solve this by making those properties optional but I don't want that. and also not able to add try catch to make sure it doesn't crash
Please tell what changes should I make to this class and also share some example or links or tutorial that explains everything about encoder and decoder in depth (not some medium article with just overview)
I am new to swift so if you find anything wrong with this class do tell in comments and best ways to do with latest swift
import Foundation
enum AcModeType {
case acModeCool
case acModeDry
case acModeHeat
case acModeFan
case acModeTimer
}
enum FanModeType {
case fanModeLow
case fanModeMid
case fanModeHigh
case FanModeAuto
}
class AcDevice: Device {
var brandId: String?
var variant: String?
var brandName: String?
var acTemperature: Int = 16
var acMinTemperature: Int = 16
var acMaxTemperature: Int = 32
var weatherTemperature: Int = 0
var roomTemperature: Int = 0
var timeTable = [TimeTable]()
var currentAcMode: AcModeType = .acModeCool
var currentFanMode: FanModeType = .fanModeLow
var lastPowerOnTime: Double = 0.0
var isPowerOn = false
var isSleepOn = false
var isTimeTableOn = false
var hasScreen = false
required public init?(coder aDecoder: NSCoder) {
super.init();
brandId = (aDecoder.decodeObject(forKey: "brandId") as! String?)
variant = (aDecoder.decodeObject(forKey: "variant") as! String?)
brandName = (aDecoder.decodeObject(forKey: "brandName") as! String?)
acTemperature = (aDecoder.decodeObject(forKey: "temperature") as! Int)
acMinTemperature = (aDecoder.decodeObject(forKey: "acMinTemperature") as! Int )
acMaxTemperature = (aDecoder.decodeObject(forKey: "acMaxTemperature") as! Int )
weatherTemperature = (aDecoder.decodeObject(forKey: "weatherTemperature") as! Int)
roomTemperature = (aDecoder.decodeObject(forKey: "roomTemperature") as! Int )
currentAcMode = (aDecoder.decodeObject(forKey: "currentAcMode") as! AcModeType)
currentFanMode = (aDecoder.decodeObject(forKey: "currentFanMode") as! FanModeType)
lastPowerOnTime = (aDecoder.decodeObject(forKey: "lastPowerOnTime") as! Double)
if let timeTable = aDecoder.decodeObject(forKey: "timeTable") as! [TimeTable]? {self.timeTable=timeTable}
}
public override func encode(with aCoder: NSCoder) {
aCoder.encode(self.brandId, forKey: "brandId")
aCoder.encode(self.variant, forKey: "variant")
aCoder.encode(self.brandName, forKey: "brandName")
aCoder.encode(self.acTemperature, forKey: "temperature")
aCoder.encode(self.timeTable, forKey: "timeTable")
aCoder.encode(self.acMinTemperature, forKey: "acMinTemperature")
aCoder.encode(self.acMaxTemperature, forKey: "acMaxTemperature")
aCoder.encode(self.weatherTemperature, forKey: "weatherTemperature")
aCoder.encode(self.roomTemperature, forKey: "roomTemperature")
aCoder.encode(self.currentAcMode, forKey: "currentAcMode")
aCoder.encode(self.currentFanMode, forKey: "currentFanMode")
aCoder.encode(self.lastPowerOnTime, forKey: "lastPowerOnTime")
}
required public init() {
super.init();
brandId = ""
variant = ""
brandName = ""
isPowerOn = false;
isSleepOn = false;
isTimeTableOn = false;
hasScreen = false;
currentAcMode = .acModeCool
acMinTemperature = 16;
acMaxTemperature = 32;
currentFanMode = .fanModeLow
lastPowerOnTime = -1;
timeTable = [TimeTable]()
}
}
In your required public init?(coder aDecoder: NSCoder) you're using as! to force cast the results of decodeObject, so this will crash your app if it fails. Instead you could use as? in combination with a default value to unwrap your optionals.
For example:
brandId = aDecoder.decodeObject(forKey: "brandId") as? String ?? "your default string"
This uses ?? which is the nil coalescing operator to provide a default value when the left hand side is nil.
You can do that with Codable protocols in swift. Just have a look at the below link.
https://hackernoon.com/everything-about-codable-in-swift-4-97d0e18a2999
and,
Apple documentation for this:
https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types
I am trying to save and retrieve a custom class to UserDefaults in my macOS app. I am getting nil for newData
class countClass: NSObject, NSCoding {
var leftClickCount : Int = 0
init(leftClickCount: Int) {
self.leftClickCount = leftClickCount
super.init()
}
func encode(with coder: NSCoder) {
coder.encode(self.leftClickCount, forKey: "leftClickCount")
}
required convenience init?(coder decoder: NSCoder) {
guard let leftClickCount = decoder.decodeObject(forKey: "leftClickCount") as? Int
else {
return nil
}
self.init(
leftClickCount: leftClickCount
)
}
}
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
let leftC = countClass(leftClickCount: 25)
let ud = UserDefaults.standard
let archivedData = NSKeyedArchiver.archivedData(withRootObject: leftC)
ud.set(archivedData, forKey: "data")
ud.synchronize()
let tempData = ud.object(forKey: "data") as! Data
let newData = NSKeyedUnarchiver.unarchiveObject(with: tempData) as! countClass // Getting nil here
}
}
I was able to fix this problem by changing from:
decoder.decodeObject(forKey: "leftClickCount") as? Int
with:
decoder.decodeInteger(forKey: "leftClickCount")
I have a class PredicPair which inherits from NSCoding and NSObject as such:
class PredicPair: NSObject, NSCoding {
var weight : Float
var prediction : String
init(weight: Float, prediction: String) {
self.weight = weight
self.prediction = prediction
super.init()
}
func encode(with aCoder: NSCoder) {
aCoder.encode(weight, forKey: "weight")
aCoder.encode(prediction, forKey: "prediction")
}
required convenience init(coder aDecoder: NSCoder) {
let unarchivedWeight = aDecoder.decodeObject(forKey: "weight") as! Float
let unarchivedPrediction = aDecoder.decodeObject(forKey: "prediction") as! String
self.init(weight: unarchivedWeight, prediction: unarchivedPrediction)
}
class func saveToUserDefaults(pairs: [PredicPair]) {
let dataBlob = NSKeyedArchiver.archivedData(withRootObject: pairs)
UserDefaults.standard.set(dataBlob, forKey: "test")
UserDefaults.standard.synchronize()
}
class func loadFromUserDefaults() -> [PredicPair]? {
guard let decodedNSDataBlob = UserDefaults.standard.object(forKey: "test") as? NSData,
let loadedFromUserDefault = NSKeyedUnarchiver.unarchiveObject(with: decodedNSDataBlob as Data) as? [PredicPair]
else {
return nil
}
return loadedFromUserDefault
}
}
I am trying to store an array of the class in UserDefaults and retrieving it, but the latter always returns nil. Any reason why?
let predicPair1 = PredicPair(weight: 0, prediction: "there")
let predicPair2 = PredicPair(weight: 1, prediction: "hello")
let array = [predicPair1, predicPair2]
PredicPair.saveToUserDefaults(pairs: array)
if let retreivedArray = PredicPair.loadFromUserDefaults() {
print("loaded \(retreivedArray.count) players from NSUserDefaults")
} else {
print("failed")
}
I've also tried saving to a file using NSKeyedArchiver but retrieval fails as well.
I get the error:
[NSKeyedUnarchiver init]: cannot use -init for initialization
when trying to load a custom class.
Heres my Init for the class:
required init?(coder aDecoder: NSCoder) {
print("intializing")
if let quoteName = aDecoder.decodeObjectForKey("quoteName") as? String {
self._quoteName = quoteName
}
if let quote = aDecoder.decodeObjectForKey("quote") as? String {
self._quote = quote
}
if let soundFileName = aDecoder.decodeObjectForKey("soundFileName") as? String {
self._soundFileName = soundFileName
}
if let soundFileType = aDecoder.decodeObjectForKey("soundFileType") as? String {
self._soundFileType = soundFileType
}
if let audioFilePath = aDecoder.decodeObjectForKey("audioFilePath") as? String {
self._soundFileType = audioFilePath
}
if let state = aDecoder.decodeObjectForKey("state") as? Bool {
self._state = state
}
if let number = aDecoder.decodeObjectForKey("number") as? Int {
self._number = number
}
}
Heres my Encoder function
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self._quoteName, forKey: "quoteName")
aCoder.encodeObject(self._quote, forKey: "quote")
aCoder.encodeObject(self._soundFileType, forKey: "soundFileName")
aCoder.encodeObject(self._soundFileType, forKey: "soundFileType")
aCoder.encodeObject(self._audioFilePath, forKey: "audioFilePath")
aCoder.encodeObject(self._state, forKey: "state")
aCoder.encodeObject(self._number, forKey: "number")
}
Any finally my call to load:
class func loadQuoteList(){
print("loading Quotes")
quoteList.removeAll()
var quoteListLength = defaults.integerForKey("quoteListLength")
//unarchives Quote Object
var unarc = NSKeyedUnarchiver()
if let data = defaults.objectForKey("Quote") as? NSData {
unarc = NSKeyedUnarchiver(forReadingWithData: data)
}
print("loading individual quotes")
for(var index = 0; index < quoteListLength; index++){
var newQuote = unarc.decodeObjectForKey("Quote\(index)") as! Quote
quoteList[index] = newQuote
print(newQuote._quoteName)
}
}