itsji10dra
itsji10dra

Reputation: 4675

XCode 6.3 Warning : Comparison of address of 'myObject' not equal to null pointer is always true

After updating to XCode 6.3, compiler started giving this warning.

Comparison of address of 'myObject' not equal to null pointer is always true.

Here is my piece of code,

enter image description here

Scratching my head with it, but didn't find any solution or workaround to get rid of this warning.

My question is linked with question over here. But, it can't be solved using answer discussed.

Any help will be welcomed :)

Upvotes: 11

Views: 6803

Answers (3)

Felipe FMMobile
Felipe FMMobile

Reputation: 1641

For the first instruction, a changed to :

 if (!CGRectIsNull(anotherRect)) {

 }

Upvotes: 1

dgatwood
dgatwood

Reputation: 10407

There are four mistakes here. The first two are that you aren't setting any initial values for the CGRect and UIView variables, so there's no way to detect a failure. Try setting the CGRect to a zero-width, zero-height rect initially if you think that is useful, and set the UIView to nil.

The third and fourth mistakes are that the address of a local variable is never NULL. (Note that this is not true for global variables in libraries, as I mentioned in another comment—the address of an NSString pointer constant might be NULL if the symbol didn't exist on the version of the OS you're running—but for local variables, you're guaranteed an address up to the point where your stack overflows and your app crashes.)

I'm also puzzled by why you're calling a delegate method yourself. Normally, the OS itself calls those methods, so calling them yourself is somewhat atypical usage. To be fair, I've done it on occasion when a delegate method performs a computation that I need somewhere else in the code, and I've also done it when implementing delegates that call other delegates, but in the context of this question, it seemed like a potential mistake.

Upvotes: 1

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

Correct way of checking the pointer is

if (anotherInView != nil) {
}

You are comparing address of a variable with the NULL. Every variable has an address you can't have a variable whose address is NULL, you can have a variable whose value is NULL

Also anotherRect != NULL is again not valid. anotherRect will always have a value as it's a struct

Upvotes: 10

Related Questions