ios - Delgator's delegate property points to two Delegators and not to the Delegate class -


i have following class conforms protocol sentencedelegate

class sentencemarkov : sentencedelegate{    var ultimatesentence : sentence {     didset { ultimatesentence.sentencedelegate = self}   }   var penultimatesentence : sentence     init(ult : sentence, penult : sentence){     self.ultimatesentence = ult     self.penultimatesentence = penult   }     func startchain(){     self.penultimatesentence.sentencedelegate = self     self.penultimatesentence.start()   }    func sentencedidfinish(){     self.nextsentence(self.ultimatesentence.type, penulttype:  self.penultimatesentence.type)   }    //etc. } 

i define sentencedelegate follows:

protocol sentencedelegate: class{   func sentencedidfinish()  } 

finally, delegator class, sentence defined follows:

class sentence : nsobject {   var type=""   var eventarray:[soundevent] = []     weak var sentencedelegate: sentencedelegate?     weak var soundeventdelegate: soundeventdelegate? = nil {     didset {         eventarray.foreach() {$0.delegate = soundeventdelegate}     }   }    init(type :string){    //etc.   }     func sentencedone(){     sentencedelegate?.sentencedidfinish() //this program breaks   }    func start(){     self.playevent(0)   }    func handletimer(timer: nstimer) {     guard let index = timer.userinfo as? int else { return }     playevent(index)   }    func playevent(eventindex : int){     if (eventindex < 2){         let currevent = self.eventarray[eventindex]         currevent.startevent()         let nextindex = eventindex + 1         sharingmanager.sharedinstance.globaltimer = nstimer.scheduledtimerwithtimeinterval(currevent.duration, target: self, selector: selector("handletimer:"), userinfo: nsnumber(integer: nextindex), repeats: false)     }     else if (eventindex==2){         let currevent = self.eventarray[eventindex]         currevent.startevent()         sharingmanager.sharedinstance.globaltimer = nstimer.scheduledtimerwithtimeinterval(currevent.duration, target: self, selector: selector("sentencedone"), userinfo: nil, repeats: false)     }     else{         //nothing     }   } } 

i initialize above class , start program viewcontroller in following fashion:

    let s1:sentence = sentence(type: "s3")     let s2:sentence = sentence(type: "s1")     var newmodel = sentencemarkov(ult: s1, penult: s2)     newmodel.startchain() 

when run program not execute sentencedelegate method sentencedidfinish on delegate class. when set break point method, program never stops. when set breakpoint on sentencedone in delegator class, inspect self variable, , see sentencedelegate defined, points 2 copies of delegator class (ultimatesentence , penultimatesentence) , not delegate class. furthermore, these 2 instances of delegator class, upon using breakpoint inspection, have sentencedelegate properties nil.

confer photos:

enter image description here

enter image description here don't understand how sentence property sentencedelegate (which set self in sentencemarkov ) points sentence , not delegate class calls sentencedidfinish. elucidation of errors in reasoning / programming appreciated.

the problem using didset in conjunction init. these methods not called during init, need create specific method , call within init well. example;

var ultimatesentence : sentence {   didset { setdelegate() } }  func setdelegate() {   ultimatesentence.sentencedelegate = self }  init(ult : sentence, penult : sentence){   self.ultimatesentence = ult   self.penultimatesentence = penult   setdelegate() } 

you keeping weak reference delegates. it's not clear me has strong reference them. if nothing has strong reference destroyed once out of scope. confirm putting print inside deinit of sentencemarkov. it's worth noting swift standard classes start uppercase letter , instances start lower case. makes harder read people used convention, when instances start upper case.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -