Paul
Paul

Reputation: 789

objective-c : Is it dangerous cast to (NSInteger)self?

Is it dangerous to cast "(NSInteger)self"?

I need to distinguish two views, not using tag value.

If It is dangerous, any ideas?

Upvotes: 0

Views: 100

Answers (3)

jscs
jscs

Reputation: 64002

The NSObject protocol declares an isEqual: method for comparing objects. A class can override this to define its instances as equal if their contents are equal; otherwise two instances will compare as unequal.

This is what you should really use to distinguish objects from each other, rather than comparing their pointer values directly. That is, however, safe to do.

Upvotes: 0

Jim
Jim

Reputation: 73936

You mean you have two UIView pointers and you want to know if they are pointing to the same object? Just use ==. No need to cast them. If you mean something else, you'll need to provide more details on what you mean by "distinguishing two views".

Upvotes: 3

Hot Licks
Hot Licks

Reputation: 47729

Ignoring what convulsions ARC may go through, it's not dangerous to cast self (or any other pointer) to integer. It is exceedingly dangerous, however, to cast the other direction.

And Objective-C has enough odd corners and dark hallways that it's difficult to say with great confidence that self cast to an integer will be reproducible in a useful way.

Upvotes: 1

Related Questions