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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:38:31+00:00 2026-06-17T00:38:31+00:00

I was just quickly playing with blocks today and I came across the error:

  • 0

I was just quickly playing with blocks today and I came across the error:

NSString *(^testBlock)(int) = ^(int option) {
    if (option == 1) return @"ONE";
    if (option == 2) return @"TWO";
    return nil;
};
NSLog(@"OUTPUT: %@", testBlock(4));
Return type 'void *' must match previous return type 'NSString *' when block literal has unspecified explicit return type

As I really wanted to return nil if “1” or “2” were not input I decided to simply cast the final return back to an NSString using:

NSString *(^testBlock)(int) = ^(int option) {
    if (option == 1) return @"ONE";
    if (option == 2) return @"TWO";
    return (NSString *) nil;
};

This works just fine, I was just curious if this was the correct solution or even bad practice as I have never thought about casting nil before?

  • 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-17T00:38:32+00:00Added an answer on June 17, 2026 at 12:38 am

    It’s not the best approach.

    You should correct the first line to this:

    NSString *(^testBlock)(int) =  ^NSString*(int option){
        if(option == 1) return @"ONE"; 
        if(option==2) return @"TWO";
        return nil;
    };
    

    This way the block literal has the return type specified and the error goes away. Correctly.

    EDIT: Adding explanation on the initial error:

    A block without a return type will have the return type inferred by the compiler (which doesn’t happen with functions). When you have 2 return statements in the block with different types (note that nil is void*) the compiler can’t infer the return type and reports an error.
    To fix that error you have to manually specify a return type to avoid ambiguity for the compiler.

    As a good practice, never return different types from the same block unless you are using polymorphism.

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

Sidebar

Related Questions

I just want to quickly store an array which I get from a remote
I'm just trying to quickly debug a webservice by printing out the contents of
I'm just wondering exactly what factors affect how quickly createthread executes, and how long
I'm just getting the hang of Rails console, and finding it useful for quickly
Just checking my JS and I have an error, but I cannot see where.
Say I have a table People , is there a way to just quickly
I was trying to just quickly integrate the Windows Media Player via COM to
I started playing around with xtext a few days ago and just went through
Excuse the unhelpful variable names and unnecessarily bloated code, but I just quickly whipped
I just want to quickly see the properties and values of an object in

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.