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


i trying understand logic of draggable collection view cells. works dummy data however, couldn't figure out how make work real data.

i couldn't know title give question, please feel free edit/improve it

if use approach dummy array items , call 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          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 point print(self.data) prints new order of array items after dragging/rearranging.

example print(self.data) log: [["2.jpg", "1.jpg", "3.jpg"]]


now have real data , gets appended items after fetching database..

var realimages = [nsurl]()  // tried assigning `images` array inside `lazy var data` received error:   lazy var data : [[string]] = {      var array = [[string]]()     let images = realimages    // error here             ... 

instance member realimages cannot used on type tableviewcontroller

what proper way of using real array in case?


(this explanation , its git repo great understanding it). simplified further dummy data couldn't understand logic of 'lazy var' , regular 'var' , how make work in scenario

you need use self explicitly let images = self.realimages.

lazy var data : [[string]] = { [unonwed self] in     var array = [[string]]()     let images = self.realimages    // error gone :) 

(note had [unowned self] in here prevent strong reference cycle)


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -