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 7950557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:18:41+00:00 2026-06-04T02:18:41+00:00

Is this allowed and why? void (^bar)(NSNumber *) = ^(NSNumber *number) { NSLog(@Value is

  • 0

Is this allowed and why?

void (^bar)(NSNumber *) = ^(NSNumber *number) {
    NSLog(@"Value is %@, class is %@.", number, [number class]);
};
bar([NSNumber numberWithInt:10]);

void (^foo)(id) = bar;
foo([NSDate date]);

Output is:

Value is 10, class is __NSCFNumber.
Value is 2012-05-17 18:54:14 +0000, class is __NSDate.

I couldn’t find anything related that explains this. Can you provide a link to the objective c blocks specification that covers this?

Currently I’m working on a block based UITableView subclass and it would make things a lot easier, if I’m safe to use this.

  • 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-04T02:18:42+00:00Added an answer on June 4, 2026 at 2:18 am

    The underlying reasons this works is a combination of:

    [1] Objective-C (and C) is not strongly typed at compile time. While warnings may be produced by the compiler they can usually be silenced by (sometimes type unsafe) casts. Your assignment in this case is invalid as you are assigning a block reference which declares it requires an argument value compatible with NSNumber * to another block reference which only declares it requires an argument value compatible with id. This is type unsafe and will produce runtime errors sometimes, see below.

    [2] Objective-C runtime message passing is dynamic, that is the target code for a message is determined as the code runs. This means as all your uses of number in the block are non-specific to NSNumber when you pass an NSDate at runtime suitable methods are still located dynamically. However change your bar to:

    void (^bar)(NSNumber *) = ^(NSNumber *number)
    {
       NSLog(@"Value is %@, class is %@, int value is %d.", number, [number class], [number intValue]);
    };
    

    and you will see runtime errors.

    [3] Both [NSNumber numberWithInt:10] and [NSDate date] are declared to return values of type id, not NSNumber * & NSDate * as you might expect. This means you don’t need foo, you can just type:

    bar([NSDate date]);
    

    and get the same result without any warnings… As a further example consider this:

    NSNumber *num = [NSNumber numberWithInt:3];
    NSDate *date = num;         // produces a warning
    id erase = num;             // erase type info and do...
    date = erase;               // effectively the same assignment, no warning
    

    Take way: Objective-C is not a type safe language, the compiler will in many cases warn you about potential problems, but it will not do so in all cases.

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

Sidebar

Related Questions

Is this allowed? : class A; void foo() { static A(); } I get
I hope this code explains the problem: class Foo { void a() { /
class Foo { public: void someFunk( Foo &bar ); private: int a; ... };
I hope this is allowed but I have a number of questions regarding Facebook
Sometimes, C++'s notion of privacy just baffles me :-) class Foo { struct Bar;
If I have a function void Foo(params int[] bar){} The following runs fine: int[]
Possible Duplicate: Java abstract interface public interface Foo { abstract public void bar(); }
I have a templated class like this: template <typename T> class AguiEvent { std::vector<std::tr1::function<void(T,
This is not allowed in Mysql: SELECT CAST(0 as DOUBLE) as ZERO How do
I have followed this tutorial which allowed me to create a Silverlight DataGrid that

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.