Assigning 'class variable' to variable inside 'lazy var' - ios

I am trying to understand the logic of draggable collection view cells. It works with dummy data however, I couldn't figure out how to make it work with real data.
I couldn't know what title to give to the question, please feel free to edit/improve it
If I use this approach with a dummy array items and call the function
class TableViewController: UITableViewController, KDRearrangeableCollectionViewDelegate, UICollectionViewDataSource {
lazy var data : [[String]] = {
var array = [[String]]()
let images = [
"1.jpg", "2.jpg", "3.jpg"
]
if array.count == 0 {
var index = 0
var section = 0
for image in images {
if array.count <= section {
array.append([String]())
}
array[section].append(image)
index += 1
}
}
return array
}()
func moveDataItem(fromIndexPath : NSIndexPath, toIndexPath: NSIndexPath) {
let name = self.data[fromIndexPath.section][fromIndexPath.item]
self.data[fromIndexPath.section].removeAtIndex(fromIndexPath.item)
self.data[toIndexPath.section].insert(name, atIndex: toIndexPath.item)
print(self.data)
}
At this point print(self.data) prints the new order of the array items after dragging/rearranging.
Example print(self.data) log:
[["2.jpg", "1.jpg", "3.jpg"]]
Now I have my real data as and it gets appended by items after fetching from database..
var realImages = [NSURL]()
// I tried assigning `images` array inside `lazy var data` but received error:
lazy var data : [[String]] = {
var array = [[String]]()
let images = realImages // error here
...
Instance member realImages cannot be used on type TableViewController
What is the proper way of using my real array in that case?
(This explanation and its Git repo was great for understanding it). I simplified it even further with dummy data but I couldn't understand the logic of 'lazy var' and regular 'var' and how to make it work in this scenario

You need to use self explicitly like let images = self.realImages.
lazy var data : [[String]] = { [unonwed self] in
var array = [[String]]()
let images = self.realImages // error gone :)
(Note that we had to say [unowned self] in here to prevent a strong reference cycle)

Related

How to sort cells in collectionView from newest to oldest. Realm, Swift

These are my realm classes and functions:
class RealmItems: Object {
#objc dynamic var header = String()
#objc dynamic var specification = String()
#objc dynamic var day = Int()
#objc dynamic var month = Int()
#objc dynamic var deadline = String()
#objc dynamic var status = String()
}
class RealmModel {
static let shared = RealmModel()
private let realm = try! Realm()
func addTask(headerTask: String, specificationTask: String, dayTask: Int, monthTask: Int, deadlineTask: String, statusTask: String) {
let new = RealmItems()
new.header = headerTask
new.specification = specificationTask
new.day = dayTask
new.month = monthTask
new.deadline = deadlineTask
new.status = statusTask
try! realm.write {
realm.add(new)
}
}
func deleteTask(name: RealmItems) {
try! realm.write {
realm.delete(name)
}
}
func getTasks() -> [RealmItems] {
var arrayTasks: [RealmItems] = []
for task in realm.objects(RealmItems.self) {
arrayTasks.append(task)
}
return arrayTasks.sorted{$0.day > $1.day}
}
}
function getTasks() doesn't work the way i want it works. Now collection shows oldest cells higher than newest - that's wrong. I want to newest were higher than oldest
A few things to be aware of.
First, read Realm Objects Are Lazily Loaded e.g. don't cast them to an array or sort them using Swift functions. Well, you can but it can lead to other issues so best to avoid that unless there's a specific use case that requires it.
Secondly, Realm Results do not have a guaranteed order unless you specify that order. On the other hand, a List object maintains it's order.
Ordering a Results collection is simple, and actually keeps the objects in the results in order as object properties change, for example
let orderedResults = realm.objects(RealmItems.self).sorted(byKeyPath: "day", ascending: false)
Will load all of the RealmItems objects sorted by day, descending (e.g. 20th will be at the top and the 1st will be at the bottom)and the results will maintain their order. e.g. if a day changes, it will auto sort within the list. Pretty magical, huh?

For-in loop requires '[UserVehicles]?' to conform to 'Sequence'; did you mean to unwrap optional? Swift

I have a data model which I made for API returns, it is something like this:
struct VehicleData: Codable {
let _embedded: Embedded
}
struct Embedded: Codable {
let userVehicles: [UserVehicles]
}
struct UserVehicles: Codable {
let id: String
let images: [String]
let userId: String
let vehicle: Vehicle
let originalPrice: OriginalPrice
let hasBasicInsurance: Bool
}
I have used callback function to pass it to my ViewController, now I want to get check in the useVehiclers list, how many vehicles hasBasicInsurance. basically, vehicleList?._embedded.userVehicles[i] = true
this is my function code to use the vehicle data in ViewController:
var vehicleManager = VehicleManager()
var vehicleList: VehicleData?
var i: Int = 0
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
vehicleManager.retrieveUserVehicle()
vehicleManager.onDataUpdate = { [weak self] (data: VehicleData) in
self?.useData(data: data)
}
tableView.dataSource = self
tableView.delegate = self
tableView.tableFooterView = UIView() //remove empty tableView cells
tableView.register(UINib(nibName: Constants.vehicleListCellNibName, bundle: nil), forCellReuseIdentifier: Constants.vehicleListToBeInsuredIdentifier)
}
func useData(data: VehicleData) {
vehicleList = data
// code below has issues....
for i in [vehicleList?._embedded.userVehicles] {
if let vechile = vehicleList?._embedded.userVehicles[i].hasBasicInsurance {
if vehicle == true {
i = i + 1
print(">>number of of insured vehidle: \(i)")
} else {
print(">>>number of of insured vehidle: \(i)")
}
}
}
}
Do you know how to fix it?
You need to supply a default value for optional as a good practise instead of force unwrap
for i in vehicleList?._embedded.userVehicles ?? [] { }
It's not clear from your code, but it looks like vehicleList is optional. It probably should not be (see Leo Dabus's comments). It is rare that it makes sense to have an optional array. That suggests there's some difference between an empty array and a missing array. If there is, then that's fine, but in most cases you should just use a non-optional array and make it empty.
Whether you fix that or not, the solution to this particular problem is to just use a non-optional value, and you have one: data. So change the loop to:
for i in data._embedded.userVehicles { ... }
From your updated question, you note "I want to get check in the useVehiclers list, how many vehicles hasBasicInsurance." It seems you want to put that value in i. If so, that would be:
func useData(data: VehicleData) {
vehicleList = data
i = data._embedded.userVehicles
.filter(\.hasBasicInsurance)
.count
}
You can also use for_each loop for this, for eg like this:
vehicleList?._embedded.userVehicles.for_each(x in /*Do your thing here*/)

Updating array in class property after segue

I have a UIViewController containing a UITableView that is populated via an array of custom class objects. These custom class objects have an array property. As you can see from my code below, the objects are equatable. When I segue to a second vC, the array of custom class objects (and obviously the array properties associated with each object) is passed over.
I have a function in the second vC that matches an object with one that is contained in the array. After matching, the property array of the object in the array that matched is updated. However, when I print what should be the updated property array, no change has been made. Below is a representation of my code:
class Object: Equatable {
var propertyArray: [String] = []
static func ==(lhs: object, rhs: object) -> Bool {
return lhs.someProperty == rhs.someProperty
}
}
class ArrayOfObjects {
var array: [Object] = []
}
class vC1: UIViewController, UITableViewDelegate, UITableViewDataSource {
var objectArray1 = ArrayOfObjects()
override viewDidLoad() {
//populate objectArray1.array
}
prepareForSegue(segue: UIStoryboardSegue) {
segue.destination.objectArray2 = objectArray1 //segue to vC2
}
}
class vC2: UIViewController {
var objectArray2 = ArrayOfObjects()
var someOtherObject = Object() //object that has same properties as an object in objectArray2
func someFunc() {
let indexOfMatchingObject = objectArray2.array.index(of: someOtherObject)
let matchingObject = objectArray2.array[indexOfSomeOtherObject]
matchingObject.propertyArray.append("TestString")
print("\(matchingObejct.propertyArray)") //prints []
}
}
Why doesn't it print ["TestString"]? The same goes for when I remove one of the values from the array, the update doesnt occur.
If you are wondering why I am doing this, it's because the objects are modified in the second vC and the UI of the tableView cells in the first vC are dependent upon the properties of the objects. Hence why my data is represented as a class (reference type).
Update your someFunc():
func someFunc() {
//try to find the same object
let indexOfMatchingObject = objectArray2.array.index(of: someOtherObject)
//obtain this common objec from array
let matchingObject = objectArray2.array[indexOfSomeOtherObject]
//update propertyArray from matchingObject
matchingObject.propertyArray.append("TestString")
}
Also update your first VC table view in viewWillApear.
I realised that the answer to my question is nothing to do with the class I had created or the segue, but is in actual fact to do with the state of the array. You cannot append to an array that has not been initialised. In order to append to it, you must first initialise it, then you may append to it. Playground code has been provided below to demonstrate this in the context of the question above:
//Create object class
class Object: Equatable {
var propertyArray: [String]?
var id: Int = Int()
static func ==(lhs: Object, rhs: Object) -> Bool {
return lhs.id == rhs.id
}
}
//Create class that will hold array of objects
class ArrayOfObjects {
var array: [Object] = []
}
//initialise class that will hold array of objects
var array = ArrayOfObjects()
//set the initial values for the array
func setArray() {
let object1 = Object()
object1.id = 1
object1.propertyArray = ["hello"]
let object2a = Object()
object2a.id = 2
object2a.propertyArray = ["bye"]
array.array = [object1, object2a]
}
setArray()
//Create new object that will be used to match with one already in the array
let object2b = Object()
object2b.id = 2
object2b.propertyArray = ["bye"]
//Find if the new object exists in the array
let index = array.array.index(of: object2b)
let matchingObject = array.array[index!]
matchingObject.propertyArray?.append("welcome")
//We were able to append to the matchingObject (object2a) because the property array had been initialised
print(matchingObject.propertyArray) //prints ["bye", "welcome"]
//Create new object with uninitialised propertyArray
let object3a = Object()
object3a.id = 4
//Append this new object to the array
array.array.append(object3a)
//Create another new object that will be used to match with object3a
var object3b = Object()
object3b.id = 4
//Find if object3b can be matched in the array
let index2 = array.array.index(of: object3b)
let matchingObject2 = array.array[index2!]
matchingObject2.propertyArray?.append("hello")
//A match was found for object3b, but the array had not been initialised and so we couldn't append to it
print(matchingObject2.propertyArray) //prints nil
//Initialise the array
matchingObject2.propertyArray = []
matchingObject2.propertyArray?.append("goodbye")
//We can now append to it as it has been initialised
print(matchingObject2.propertyArray) //prints ["goodbye"]

Swift 2 Unable to remove optional binding

I am new in Swift and don't have much more idea on optional (! , ?). I tried to fetch data from plist, create Model and show to UITableView. Table data shows perfectly, but it shows with Optional() binding. I tried change ! to ? but unable to unwrap. Could you please, guide me to solve this problem.
Here is my code & output -
var fileName : String?
var dataArray : Array<SHQuesAns>?
For fetch data from pList -
func loadTableView(){
dataArray = SHDataAccess.init(fname: fileName).arrayFromPlist()
self.questionTableView.dataSource = self
self.questionTableView.delegate=self
self.questionTableView.reloadData()
}
SHDataAccess class -
import UIKit
var fileName : String!
class SHDataAccess: NSObject {
init(fname:String?) {
super.init()
fileName = fname
}
func arrayFromPlist() -> Array <SHQuesAns>?{
let dataPlists = NSMutableArray(contentsOfFile:NSBundle.mainBundle().pathForResource(fileName, ofType: "plist")!)
var dataObj : Array <SHQuesAns>? = Array()
for data in dataPlists! {
dataObj?.append(SHQuesAns.init(_dic: data as! NSDictionary))
}
return dataObj
}
}
And UITableView delegates -
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray == nil ? 0 : dataArray!.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let aCell = self.questionTableView.dequeueReusableCellWithIdentifier("qcell",forIndexPath: indexPath) as! SHQuestionCell
let q : SHQuesAns = dataArray![indexPath.row]
aCell.lblQuestion.text = "\(q.question)"
return aCell
}
Here is the output -
This will remove that Optional() text:
if let q = q.question as? String {
aCell.lblQuestion.text = "\(q.question!)"
} else {
aCell.lblQuestion.text = ""
}
The key is to unwrap the string contained in the question object so that when it is assigned to the text of the label, the Optional() part will not be included.
I’ve added support for the nil case if the question string is not defined.
You might also consider not making your dataObj array optional? what purpose does it serve to be optional? Seems to me that if you need to add items to the array then you know it should exist and since you've initialized it it will always exist but then may be empty. Instead just make it implicitly unwrapped and then return nil if there's no data, then the objects of the array won't all be optional.
if you have a default in mind that you would want the optional string to fall back to, a simple fix would be something like:
"\(q.question ?? "")"
which will default to an empty string if q.question is nil
also: be careful of all of your force unwraps. it might make more sense to have some guard statements or if let unwraps.
and swift array's can be written like so: var dataArray : [SHQuesAns]?
but there aren't many situations where you need to differentiate between a nil array and an empty array so you can just do var dataArray = [SHQuesAns]() and save yourself the need to unwrap

Issue with arrays using Swift: Can't access all objects of an array after For loop

I've had this problem for a very long time. I'm trying to populate a UITableView with NSMutableArray. But I'm not getting all the objects out side of the loop. The total count of objects is 167. But instead it returns 167 of the same object.
I declared the array outside of the loop like this:
class TableViewController: UITableViewController {
var sermon = SermonLink()
var sermons: NSMutableArray = []
override func viewDidLoad() {
super.viewDidLoad()
loadContent()
}
The whole thing happens in a method:
func loadContent() {
var elements: NSArray = []
let urlForHTML = NSURL(string:"http://ontherock.dk/?page_id=1141")
var dataForHTML: NSData = NSData(contentsOfURL: urlForHTML!)!
var htmlParser = TFHpple(HTMLData: dataForHTML)
var htmlXPathString = "//td[#class='lyf_td_filename']/a"
elements = htmlParser.searchWithXPathQuery(htmlXPathString) as NSArray
var sermonArr:NSMutableArray = []
for item in elements {
var element: TFHppleElement = item as! TFHppleElement
var firstChildItem: NSString = element.firstChild.content
var dashSeperatedSermonArray = firstChildItem.componentsSeparatedByString("-") as! [String]
sermon.subject = dashSeperatedSermonArray[4].stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
sermon.speaker = dashSeperatedSermonArray[3].stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
sermon.urlForAudioSermon = element.attributes["href"] as! String
sermonArr.addObject(sermon)//Here inside the loop I get the expected data
}
sermons = sermonArr //sermons contains only the object at the last index, 167 times
self.tableView.reloadData()
}
The below could be the problem.
Since you initialised the var sermon = SermonLink()
as a property of the class and you are updating same objects inside the for loop. Try to create this inside the for loop for each iteration.

Resources