user3046185 user3046185 - 2 months ago 7
iOS Question

How to check if view has tag?

Help me pls with this unexpected trouble.

if (view.tag != nil)


^^ this worked for me, but suddenly i've assigned 0 tag to the view, and now everything's broken.

How can i check if view has a tag assigned ?

Thanks

Answer

All views have a tag. default is Zero 0

It depend on what you have. I believe you should consider a view with tag 0 is a view that is not tagged. and start tagging from 1.

Since the tag property of a view is of type NSInteger it can not be nil. NSInteger is a primitive type and therefore can not have a nil pointer. The default value assigned to the property will be 0.

Also, when you compare nil with 0 it gives a YES in objective-c. Thats what is breaking your logic.

if (view.tag != nil) equivalent to if (view.tag != 0)