custom view with parameter from a remote json - swift

I have a custom view and the VC for it. The custom view has all kind of things in it, one of these are the labels for the available sizes for the given product. :
class CustomView: UIView {
var productSize: Array<String>
// all other labels and stuff
required init(size: Array<String>) {
self.productSizes = size
super.init(frame: .zero)
setupView()
setupConstraints()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
And the other view:
class CustomViewViewController: UIViewController {
private let productDetailView = ProductDetailsView(size: [])
let product: Product
}
I get the product without any problem, but how should I connect the available sizes with the view? Obviously that empty array there is not correct, how should i connect the data from the remote file with the view init?
I was thinking about making an array and pushing it with the product itself from the original table view like this:
if let productsArray = self.productsArray {
let allSizes: Array<String> = productsArray[indexPath.row].sizes?.components(separatedBy: ",") ?? []
let customVC = CustomViewViewController(product: productsArray[indexPath.row], allSizes: allSizes)
navigationController?.pushViewController(customVC, animated: true)
}
But I could't really figure out how this one should work either...

You can make it a lazy var
lazy var productDetailView:ProductDetailsView = {
let res = ProductDetailsView(size:self.product.sizes?.components(separatedBy: ",") ?? [])
return res
}()
OR
1- var productDetailView:ProductDetailsView!
2- Inside viewDidLoad
productDetailView = ProductDetailsView(size:self.product.sizes?.components(separatedBy: ",") ?? [])

Related

Generically setting accessibility identifier for automation tools?

We have a requirement in our project. We need to set accessibility identifier for all the components in approximately 40 view controllers. I was thinking how to achieve these basic work by getting each view controller name and iboutlet names in run time and generate ids by combining these values as accessibility id. For these, I need to get IBOutlet's names. How can I do that ? Or do you have any alternative idea for automating this process another way ?
Thanks.
You can try Sourcery
It able to parse all your source files and provide you information about IBOutlets of all controllers:
You interested in classes -> variables -> attributes
You can generate inline for all such variables didSet block in which you will setup proper accessibility identifier
you can check this link and you find a great solution to automate setting accessibilityIdentifier with the same name of the variable
https://medium.com/getpulse/https-medium-com-dinarajas-fixing-developer-inconveniences-ios-automation-e4832108051f
import UIKit
protocol AccessibilityIdentifierInjector {
func injectAccessibilityIdentifiers()
}
extension AccessibilityIdentifierInjector {
func injectAccessibilityIdentifiers() {
var mirror: Mirror? = Mirror(reflecting: self)
repeat {
if let mirror = mirror {
injectOn(mirror: mirror)
}
mirror = mirror?.superclassMirror
} while (mirror != nil)
}
private func injectOn(mirror: Mirror) {
for (name, value) in mirror.children {
if var value = value as? UIView {
UnsafeMutablePointer(&value)
.pointee
.accessibilityIdentifier = name
}
}
}
}
extension UIViewController: AccessibilityIdentifierInjector {}
extension UIView: AccessibilityIdentifierInjector {}
class BaseView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
injectAccessibilityIdentifiers()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
injectAccessibilityIdentifiers()
}
}
How to use it
class ViewController: BaseViewController {
let asd1 = BaseView()
let asd2 = BaseView()
let asd3 = BaseView()
let asd4 = BaseView()
let asd5 = BaseView()
let asd6 = BaseView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
[asd1, asd2, asd3, asd4, asd5, asd6].forEach {
print($0.accessibilityIdentifier)
}
}
}

Swift: Convert object instance name to String for use as key

Given var varName = "varValue", is there a way in Swift to convert the variable name to String at runtime? For example in this case I would get back "varName".
I already know about Mirror APIs for reflection in Swift. That allows me to iterate over the properties of a given class but I would like to apply this to self for any given class. I want to use this to generate String keys automatically for any given object (irrespective of which class it belongs to)
extension UIView {
var key: String {
return "" //TODO: Convert self to varName as String
}
}
// Usage
let customView = UIView()
customView.key // should be "customView"
Update:
The OP added this comment clarifying the requirements:
I need the 'key' to be different for 2 instances of UIView. I want that key to be the same every time for that particular instance i.e. the key shouldn't change if the app is restarted or the view instance is destroyed and recreated. I can use this key as key in caching. Another use case can be to use it as accessibilityIdentifier to help with UITesting.
In that case, I suggest to not even think about using ✨magic✨. Just explicitly give your view instances an identifier. You could also totally just reuse existing properties on UIView like tag or accessibilityIdentifier. If that's not enough or not convenient enough, subclass:
class IdentifiableView: UIView {
public private(set) var identifier: String
init(frame: CGRect, identifier: String) {
self.identifier = identifier
super.init(frame: frame)
self.accessibilityIdentifier = identifier
}
init() {
fatalError("Must use init(frame:identifier:)")
}
override init(frame: CGRect) {
fatalError("Must use init(frame:identifier:)")
}
required init?(coder aDecoder: NSCoder) {
fatalError("Must use init(frame:identifier:)")
}
}
// Usage
let firstView = IdentifiableView(frame: .zero, identifier: "First View")
firstView.identifier
firstView.identifier
let otherView = IdentifiableView(frame: .zero, identifier: "Second View")
otherView.identifier
otherView.identifier
If, according to your comment, you simply want "objects to return a unique key that does not change", you could simply use their memory address:
extension UIView {
var key: String {
return "\(Unmanaged.passUnretained(self).toOpaque())"
}
}
let firstView = UIView()
firstView.key // -> "0x00007fbc29d02f10"
firstView.key // -> "0x00007fbc29d02f10"
let otherView = UIView()
otherView.key // -> "0x00007fbc29d06920"
otherView.key // -> "0x00007fbc29d06920"
For each instance of UIView you create this will return a unique value that will not change.
I'm not sure what you are trying to accomplish here but I think you can convert variable name to string in Swift with #keyPath(propertyName) but it requires you to add #objc to your var.
For example
class MyViewController : UIViewController {
#objc var name: String = "John"
}
print(#keyPath(MyViewController.name))
prints name in the console.

Setting values of a class for example UIButton

I am lacking some basic understandings.
Today I wanted to subclass some UIView and I looked in to the UIButton definition, but I can not figure out how it works.
In the UIButton definition are properties like:
open var adjustsImageWhenHighlighted: Bool
open var adjustsImageWhenDisabled: Bool
When using an UIButton it does not matter when the values of the UIButton get set, it always gets configured the correct way same with tableView or any other UIKit classes.
I made a example:
class customView: UIView {
var shouldSetupConstraints = true
var addAdditionalSpacing = true
var elements: [String]? {
didSet{
if addAdditionalSpacing == false {
doSomething()
}
}
}
func doSomething() {
}
override init(frame: CGRect) {
super.init(frame: frame)
setUpLayout()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func updateConstraints() {
if(shouldSetupConstraints) {
// AutoLayout constraints
shouldSetupConstraints = false
}
super.updateConstraints()
}
func setUpLayout() {
}
}
Using CustomView:
lazy var customV: CustomView = {
let v = CustomView()
v.addAdditionalSpacing = true
v.elements = ["One","Two"]
return v
}()
lazy var customV2: CustomView = {
let v = CustomView()
v.elements = ["One","Two"]
v.addAdditionalSpacing = true
return v
}()
So if I am using CustomView it makes a difference in which order I set it up, I understand why but I do not understand in which way I have to design my classes so I can set the values whenever I want, except with different if didSet configurations. Why do the properties in UIButton do not have any setters, how do the values get set in that case?
Any links to documentations are appreciated as well.
First of all, in a UIButton you can take control of the didSet property observer for existing properties like this:
override var adjustsImageWhenDisabled: Bool {
didSet {
doStuff()
}
}
Secondly, in your class scenario you might consider passing the parameters in the constructor:
init(shouldSetupConstraints: Bool = true, var addAdditionalSpacing = true)
This way the properties will be available for you whenever you need them.
Let me know if this helps.

Saving Array with NSCoding

I have a small app that has a few saving functionalities. I have a data model class called: Closet:
class Department: NSObject, NSCoding {
var deptName = ""
var managerName = ""
var Task: [Assignment]? // <----- assignment class is in example 2
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(deptName, forKey: "deptName")
aCoder.encodeObject(managerName, forKey: "mngName")
// aCoder.encodeObject(Task, forKey: "taskArray")
}
required init(coder aDecoder: NSCoder) {
super.init()
course = aDecoder.decodeObjectForKey("deptName") as! String
instructor = aDecoder.decodeObjectForKey("mngName") as! String
// Task = aDecoder.decodeObjectForKey("tasKArray") as? [Assignment]
}
override init() {
super.init()
}
}
So this is the main controller data model which in the first View Controller, a user is able to tap the "+" button to add a department name and manager name. The problem is not with saving this as i save it successfully using NSKeyedArchive and loads it back when the app starts.
The Problem:
I want to add an array of assignments on this data model Department called Assignment which would have a title and a notes variable. This is the Data model for Assignment:
Assignment.swift
class Assignment: NSObject, NSCoding {
var title = ""
var notes = ""
func encodeWithCoder(aCoder: NSCoder) {
// Methods
aCoder.encodeObject(title, forKey: "Title")
aCoder.encodeObject(notes, forKey: "notepad")
}
required init(coder aDecoder: NSCoder) {
// Methods
title = aDecoder.decodeObjectForKey("Title") as! String
notes = aDecoder.decodeObjectForKey("notepad") as! String
super.init()
}
override init() {
super.init()
}
}
So what i am essentially trying to achieve is an app where a user enters different departments with different manager names which work now in my app, but within a department, the user can click the "+" button to add an assignment title and notes section that can be editable when clicked which i can handle afterwards. These assignments are different from department to department.
My big problem is achieving this functionality. I can't seem to get this working.
I want this array assigment property to be part of the Department Class so each cell can have their own sort of To-Do list. any help would definitely help me out a lot. Thanks :)
You are using NSCoder correctly, but there are two errors in capitalization. The first error affects the functionality of the application, and the second error is a stylistic mistake. You encoded Task with the key "taskArray", but you tried to decode it with the key "tasKArray". If you fix the capital K in the latter, then your code will work.
The second capitalization error is a stylistic mistake: Task, like all properties in Swift, should be written in lowerCamelCase (llamaCase).
Be sure to pay close attention to indentation. In programming, there are special indentation rules we follow that help make code clear. Here is the corrected code with proper capitalization and indentation:
class Department: NSObject, NSCoding {
var deptName = ""
var managerName = ""
var task: [Assignment]?
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(deptName, forKey: "deptName")
aCoder.encodeObject(managerName, forKey: "mngName")
aCoder.encodeObject(task, forKey: "taskArray")
}
required init(coder aDecoder: NSCoder) {
super.init()
course = aDecoder.decodeObjectForKey("deptName") as! String
instructor = aDecoder.decodeObjectForKey("mngName") as! String
task = aDecoder.decodeObjectForKey("taskArray") as? [Assignment]
}
override init() {
super.init()
}
}
class Assignment: NSObject, NSCoding {
var title = ""
var notes = ""
func encodeWithCoder(aCoder: NSCoder) {
// Methods
aCoder.encodeObject(title, forKey: "Title")
aCoder.encodeObject(notes, forKey: "notepad")
}
required init(coder aDecoder: NSCoder) {
// Methods
title = aDecoder.decodeObjectForKey("Title") as! String
notes = aDecoder.decodeObjectForKey("notepad") as! String
super.init()
}
override init() {
super.init()
}
}
Updated for Swift 5 / Xcode Version 12.4 (12D4e)
Thanks for the example above Tone416 -- I've reworked it for Swift 5 as the protocols and methods have changed. I've also included a simple test to prove it out so you should be able to just cut and paste this into a playground a run it.
import Foundation
class Department: NSObject, NSCoding {
var deptName = ""
var managerName = ""
var task: [Assignment]?
func encode(with coder: NSCoder) {
coder.encode(deptName, forKey: "deptName")
coder.encode(managerName, forKey: "mngName")
coder.encode(task, forKey: "taskArray")
}
required init(coder aDecoder: NSCoder) {
super.init()
deptName = aDecoder.decodeObject(forKey: "deptName") as! String
managerName = aDecoder.decodeObject(forKey: "mngName") as! String
task = aDecoder.decodeObject(forKey: "taskArray") as? [Assignment]
}
override init() {
super.init()
}
convenience init(deptName: String, managerName: String, task: [Assignment]?) {
self.init()
self.deptName = deptName
self.managerName = managerName
self.task = task
}
}
class Assignment: NSObject, NSCoding {
var title = ""
var notes = ""
func encode(with coder: NSCoder) {
// Methods
coder.encode(title, forKey: "Title")
coder.encode(notes, forKey: "notepad")
}
required init(coder aDecoder: NSCoder) {
// Methods
title = aDecoder.decodeObject(forKey: "Title") as! String
notes = aDecoder.decodeObject(forKey: "notepad") as! String
super.init()
}
override init() {
super.init()
}
convenience init(title: String, notes: String) {
self.init()
self.title = title
self.notes = notes
}
}
// Create some data for testing
let assignment1 = Assignment(title: "title 1", notes: "notes 1")
let assignment2 = Assignment(title: "title 2", notes: "notes 2")
let myDepartment = Department(deptName: "My Dept", managerName: "My Manager", task: [assignment1, assignment2])
// Try archive and unarchive
do {
// Archive
let data = try NSKeyedArchiver.archivedData(withRootObject: myDepartment, requiringSecureCoding: false)
print ("Bytes in archive: \(data.count)")
// Unarchive
let obj = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as! Department
// Print the contents of the unarchived object
print("Department: \(obj.deptName) Manager: \(obj.managerName)")
if let task = obj.task {
for i in 0...task.count-1 {
print("Task: \(task[i].title) \(task[i].notes)")
}
}
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
Enjoy

Passing Multiple Objects To WatchKit In A NSUserDefaults

With the help of some great tutorials and users here, I've had success implementing SwiftyJSON in my app and getting a basic WatchKit app built alongside. My last hurdle to pass is getting my whole set of parsed JSON data to be passed to WatchKit, as to allow me to choose from a cell in a TableView and pull up more specific detail on a piece of criteria.
I'm parsing JSON data in my Minion.swift file, like so;
import UIKit
class Minion {
var name: String?
var age: String?
var height: String?
var weight: String?
class func fetchMinionData() -> [Minion] {
let dataURL = NSURL(string: "http://myurl/json/")
var dataError: NSError?
let data = NSData(contentsOfURL: dataURL!, options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &dataError)
let minionJSON = JSONValue(data)
var minions = [Minion]()
for minionDictionary in minionJSON {
minions.append(Minion(minionDetails: minionDictionary))
}
return minions
}
init(minionDetails: JSONValue) {
name = minionDetails["san"].string
age = minionDetails["age"].string
height = minionDetails["height"].string
weight = minionDetails["free"].string
}
}
For my iOS app, this is working well to populate my UITableView and subsequent Detail View. I have my ViewController.Swift like so;
import UIKit
class ViewController: UITableViewController {
let minions: [Minion]
required init(coder aDecoder: NSCoder!) {
minions = Minion.fetchMinionData()
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
let defaults = NSUserDefaults(suiteName: "group.com.mygroup.data")
let key = "dashboardData"
defaults?.setObject(minions, forKey: key)
defaults?.synchronize()
}
// MARK: Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
I've truncated much of the code as I don't believe it's relevant to WatchKit. In the WatchKit extension, I have my InterfaceController.swift like so;
import WatchKit
class InterfaceController: WKInterfaceController {
#IBOutlet weak var minionTable: WKInterfaceTable!
let defaults = NSUserDefaults(suiteName: "group.com.mygroup.data")
var dashboardData: String? {
defaults?.synchronize()
return defaults?.stringForKey("dashboardData")
}
let minions = ???
When I run the iOS app, it throws me the error "Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType')" because I am passing the whole set of JSON data as "minions." If I set my NSUserDefaults key to "minions[0].name" it will pass the single string, but passing the whole set of data so the WatchKit table can allow me to choose a row seems to be evading me.
In advance, as always, I am most grateful.
Your Minion class need to implement the NSCoding. Then in your view controller you need to transfer your Minion object to NSData object.
class Minion: NSObject, NSCoding {
.....
init(coder aDecoder: NSCoder!) {
aCoder.encodeObject(name, forKey: "name")
aCoder.encodeObject(age, forKey: "age")
aCoder.encodeObject(height, forKey: "height")
aCoder.encodeObject(weight, forKey: "weight")
}
func encodeWithCoder(aCoder: NSCoder) {
name = aDecoder.decodeObjectForKey("name") as String
age = aDecoder.decodeObjectForKey("age") as String
height = aDecoder.decodeObjectForKey("height") as String
weight = aDecoder.decodeObjectForKey("weight") as String
}
}
In your ViewController class:
NSKeyedArchiver.setClassName("Minion", forClass: Minion.self)
defaults?.setObject(NSKeyedArchiver.archivedDataWithRootObject(minions), forKey: "minions")
If you want to retrieve the data from NSUserDefaults:
if let data = defaults?.objectForKey("minions") as? NSData {
NSKeyedUnarchiver.setClass(Minion.self, forClassName: "Minion")
let minions = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! [Minion]
}