Re: Block error
Re: Block error
- Subject: Re: Block error
- From: Eeyore <email@hidden>
- Date: Fri, 15 Jul 2011 16:47:07 -0700
On Jul 15, 2011, at 4:38 PM, Rick Mann wrote:
>
> On Jul 15, 2011, at 16:21 , Jens Alfke wrote:
>
>>
>> On Jul 15, 2011, at 2:42 PM, Rick Mann wrote:
>>
>>> void
>>> bar()
>>> {
>>> (id) ^(id inParam1)
>>> {
>>> if ([inParam1 isEqual: @"baz"])
>>> {
>>> Foo* foo = [[Foo alloc] init];
>>> return foo;
>>> }
>>>
>>> return nil; // Error here
>>> };
>>> }
>>
>>
>> id myblock = ^(id inParam1) { … };
>>
>> The compiler will then infer that the return type of the block is ‘id’.
>
> Not quite. I just read the docs more closely. The compiler infers the return type of the block fro the return statement. However, if you have multiple return statements, as I do, then they must match. I guess that's fine, except I feel like nil should always work.
Learning this stuff myself and want to confirm I'm following this (don't have Xcode handy), would the code below avoid the error since the one return clearly returns a Foo*?
(id) ^(id inParam1)
{
Foo* foo = nil;
if ([inParam1 isEqual: @"baz"])
{
foo = [[Foo alloc] init];
}
return foo;
};
Thanks,
Aaron _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden