• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: BOOL parameter passed as nil object
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: BOOL parameter passed as nil object


  • Subject: Re: BOOL parameter passed as nil object
  • From: Ken Thomases <email@hidden>
  • Date: Mon, 18 Apr 2016 21:13:35 -0500

On Apr 18, 2016, at 8:56 PM, Carl Hoefs <email@hidden> wrote:
>
> Suppose I have an object with a declared method signature:
>  -(void)myMethod:(BOOL)a_bool;
>
> Q1: If I invoke it like this:
>  [self performSelector:@selector(myMethod:) withObject:nil];  // nil obj
> Will argument a_bool end up with a 0 value assigned to it?

Probably, but it's poor practice to rely on it.  The caller will store a pointer to either an argument-passing register or on the stack, depending on the platform.  The called method will interpret only 1 of those 8 or 4 bytes, but, since they're all zero, it will get zero no matter what.

> Q2: But if I invoke it like this:
>  [self performSelector:@selector(myMethod:) withObject:someObj];  // valid obj
> Will argument a_bool end up with a 1 value assigned to it?

No.  The called method will interpret 1 of the bytes as a bool and that byte may be zero or non-zero, depending on the exact value of the non-nil object pointer that was passed.  Since a valid object pointer may have a zero in, say, its least-significant byte, a_bool may be zero.

You should use another mechanism for invoking the method or create a separate method which takes an object pointer and calls -myMethod: appropriately by interpreting that object pointer, and invoke that using -performSelector:….

Regards,
Ken


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


References: 
 >BOOL parameter passed as nil object (From: Carl Hoefs <email@hidden>)

  • Prev by Date: Re: BOOL parameter passed as nil object
  • Next by Date: Re: BOOL parameter passed as nil object
  • Previous by thread: Re: BOOL parameter passed as nil object
  • Next by thread: Re: BOOL parameter passed as nil object
  • Index(es):
    • Date
    • Thread