Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9187915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:53:23+00:00 2026-06-17T19:53:23+00:00

I read Mike Ash’s Objective-C pitfalls page , and now I’m paranoid about implicitly

  • 0

I read Mike Ash’s Objective-C pitfalls page, and now I’m paranoid about implicitly casting variables of type id to BOOL.

Assume I have a ‘dangerous’ pointer with the lower bits zeroed out, so that casting it to a BOOL would produce NO even though it points to a valid object. Let’s call this ‘dangerous’ pointer foo.

How do simple if statements work?
Does the if statement cast foo to a BOOL when it evaluates the condition? Or does it cast to a pure boolean?

if( foo )
{
  [myArray addObject:foo];
}

Does it work the same way in ternary expressions?

// Does this break when foo is 'dangerous'?
self.titleLabel.hidden = foo ? YES : NO ; 

Or will the ternary only break if I do this:

// I'm pretty sure it breaks now
self.titleLabel.hidden = ((BOOL)foo) ? YES : NO ; 

I feel like I’m missing something basic about logical operations in C, and how they relate to BOOL. Please help enlighten me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:53:24+00:00Added an answer on June 17, 2026 at 7:53 pm

    When does Objective-C implicitly cast to BOOL?

    Never, since this phrase is semantically incorrect. A cast is, by definition, explicit. What you’re asking about is called an “implicit type conversion” (coercion – thanks, Josh!).

    Also, strictly speaking, the conditionals in Objective-C are not special: they’re inherited from C. So expressions, when needed to evaluated as booleans, aren’t treated as BOOL, but as int.

    // Does this break when foo is 'dangerous'?`
    self.titleLabel.hidden = foo ? YES : NO;
    

    No, it doesn’t. (expr) when used as a “boolean” expression, is equivalent to (expr != 0). No risks here.

    // I'm pretty sure it breaks now
    self.titleLabel.hidden = ((BOOL)foo) ? YES : NO;
    

    Now this can break, since BOOL is just typedeffed to signed char, which is 8 bit on iOS. Thus, if foo, which is a (32-bit) pointer, gets truncated to 8 bits, and the lower 8 bits of the pointer were all zero, but the pointer itself wasn’t nil, this will incorrectly report false. You don’t want to do this superfluous, ugly and dangerous cast. If you want to be more explicit, write

    var = (obj != nil) ? valueOne : valueTwo;
    

    instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about remote threads.I've read Mike Stall's article present here: <
Read about Server push here . I want to push data to client from
Read about the issue in this stackoverflow question . Still have the same issue
I read an article about a regular expression to detect base64 but when I
I have recently read Mike McShaffry's Game Coding Complete and noticed the code style
Read on before you say this is a duplicate, it's not. (as far as
Read (skimmed enough to get coding) through Erlang Programming and Programming Erlang . One
I read this question in stackoverflow. The excerpt answer provided by bbum is below:
Having read the documentation for VPython and GTK threading , it seems to me
I read a text file to get some info from it and later on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.