swift - ios: why it call deinit immediately -


today faced problem , vc never call deinit, added weak

func showaddcityviewcontroller() {         weak var vc:swaddcityviewcontroller!         vc =  swaddcityviewcontroller()         vc.delegate = self         let nav = uinavigationcontroller(rootviewcontroller: vc)         dispatch_async(dispatch_get_main_queue(), {             self.presentvc(nav)         })     } 

i run function ,

fatal error: unexpectedly found nil while unwrapping optional value

the vc go nil ,but don't know why , should make code happy?

you wrote this:

    weak var vc:swaddcityviewcontroller!     vc =  swaddcityviewcontroller() 

the vc variable “implicitly unwrapped optional”, means can either point existing (not-deallocated) object, or can nil.

you create new swaddcityviewcontroller object , assign vc. after assignment statement completes, there 1 weak reference new object (in vc) , there no strong references it. object deallocated has no strong references, deallocated assignment statement completes.

since vc weak reference object, part of deallocating object sets vc nil. when try set vc.delegate on next line, swift generates code unwrap vc automatically (since declared !). since vc nil, fatal error. cannot unwrap optional that's set nil because it's not wrapping anything.

i don't see reason declare vc weak in function. rid of weak attribute.

your other complaint (with weak) object doesn't deallocated later. have “retain cycle”. did declare delegate property of swaddcityviewcontroller using weak? want declare delegate properties weak.

if doesn't fix problem, need other places have retain cycle involving object.


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -