开发者

How to assign a value to an pointer-pointer passed in call by reference?

I want to achieve something similar to what these guys do here:

- (NSUInteger)countForFetchRequest:(NSFetchRequest *)reque开发者_运维技巧st error:(NSError **)error

like you can see, you pass an NSError pointer and that nice method will assign a real NSError object to your pointer in case there is an error. So the cool thing about this is, that the method returns an NSUInteger but can ALSO return an NSError, without having to mess around with ugly and fat arrays or dictionaries.

So how could I assign an object to the passed-in error pointer?


It's easy. This Apple guide shows how you might implement a method that returns an NSError object. But to make a long story very, very short:

- (NSUInteger)countForFetchRequest:(NSFetchRequest *)request error:(NSError **)error
{
    //Do stuff, including make MyCustomErrorDomain and errCode and eDict.
    if (error != NULL) { // check to avoid crash if **error is not provided
        *error = [[[NSError alloc] initWithDomain:MyCustomErrorDomain code:errCode userInfo:eDict] autorelease];
    }
    //Do some more stuff.
}

Note the asterisk. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜