Will multiple calls to removeFromSuperView lead to crashes/bugs?

In my application in some edge cases I might call

[view removeFromSuperView]

twice. Will this lead to crashes or other bugs?

Upvotes: 0

Views: 272

Answers (2)

AJ7
AJ7

Reputation: 149

This post is very old but in the current version, when you call removeFromSuperView() twice, the app will crash, at least in Swift.

You get the following error:

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
2019-05-23 17:29:03.819930+0530 PIM[9117:140811] Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

I believe this happens because my view is nil as it was removed from the super view the first time. So checking for nil should resolve the crash:

if (myView != nil) {
    // Perform Further Operations
}

Hope this helps.

Upvotes: 0

Wain
Wain

Reputation: 119031

No, unless something else is wrong in your code (like the view was released before the second call is made).

From a bugs point of view, if you discard the view after its removed you should be fine. If you add the view to a new superview you could lead to the view then being removed (which may cause it to be destroyed).

Upvotes: 2

Related Questions