2

I'm currently porting some classes from the Apple iOS Foundation Framework to C++ and i'm expecting some issues. I'm trying to port this Obj-C method from the NSExpression @class :

- (id, NSArray *, NSMutableDictionary *)expressionBlock

So in my sfExpression class, I have the following code (when deleting others methods ^^)

class sfExpression : public sfObject {
public:
    id (^ExpressionBlock())(id, NSArray*, NSMutableDictionary*);

private:
    NSExpression* _Expression;
};

And when I'm trying to implement this function like this :

id (^sfExpression::ExpressionBlock())(id, NSArray*, NSMutableDictionary*) {
    return [_Expression expressionBlock];
}

It doesn't work... Any ideas ?

I've tried many things but... without success :'(

EDIT : The code is right. Just switch to LLVM Compiler instead of GCC

0

2 Answers 2

3

Edit: Moral of the story, don't use GCC when dealing with blocks.

Here is a full example as far as I can see, this works in my tests.

class Foo
{
public:
    Foo(NSExpression *_exp) : expression([_exp retain])
    {

    }

    ~Foo()
    {
        [expression release];
    }

    id (^ExpressionBlock())(id, NSArray*, NSMutableDictionary*);
private:
    NSExpression *expression;
};

id (^Foo::ExpressionBlock())(id, NSArray*, NSMutableDictionary*)
{
    return [expression expressionBlock];
}


int main()
{
    NSAutoreleasePool *pool = [NSAutoreleasePool new];
    Foo a([NSExpression expressionForBlock:^id(id evaluatedObject, NSArray *expressions, NSMutableDictionary *context) {
        return evaluatedObject;
    } arguments:[NSArray arrayWithObject:@"Test"]]);
    NSLog(@"%@", a.ExpressionBlock()(@"Hello", nil, nil));
    [pool drain];
}
Sign up to request clarification or add additional context in comments.

3 Comments

Compiler return : error: cannot convert 'void (^)(objc_object*, NSArray*, NSMutableDictionary*)' to 'objc_object* (^)(objc_object*, NSArray*, NSMutableDictionary*)' in return
It works only with LLVM Compiler 2.0. LLVM GCC 4.2 & GCC 4.2 return the error i've copied 3mins ago. Thanks a lot.
Let me guess, your'e using GCC? You should switch to using LLVM if your'e dealing with blocks. GCC doesn't support them properly
0

/usr/include/Block.h manipulates ObjC blocks using opaque void pointers, so that's probably what you should use to pass them around to C++ code if you can't compile it with a compiler supporting blocks. Also take note of the Block_copy and Block_release functions in that header. There's also an article on memory management of ObjC blocks.

1 Comment

I have to use void pointers ? Pointing on the block ? Can you make an exemple because i'm confused... I already read the article but there is somethings that i've don't understood. (Hope you understand my poor english... I'm a french guy...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.