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

  • Home
  • SEARCH
  • 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 6880563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:59:24+00:00 2026-05-27T04:59:24+00:00

why is error:&error used here (objective-c) NSError *error = nil; NSArray *array = [moc

  • 0

why is “error:&error” used here (objective-c)

NSError *error = nil;
NSArray *array = [moc executeFetchRequest:request error:&error];

wouldn’t an object in objective-c be effectively pass-by-reference anyway?

  • 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-05-27T04:59:25+00:00Added an answer on May 27, 2026 at 4:59 am

    The argument type for error: is NSError** (i.e. a pointer to a pointer to an object). This permits the moc object to allocate and initialize a new NSError object as required. It is a common pattern, especially in Cocoa.

    The NSError documentation gives some indication of the motivation for this approach:

    Applications may choose to create subclasses of NSError to provide better localized error strings by overriding localizedDescription.

    Passing in an NSError** argument allows that method to return any subclass of NSError that makes sense. If you passed in NSError*, you would have to supply an existing NSError object, and there would be no way for the method to return a different object from the one you passed in.

    To be clear, the method could look something like this:

    - (NSArray*)executeFetchRequest:(Request *)request error:(NSError**)error {
        ...
        if ((error != NULL) && (some_error_condition)) {
            *error = [[[SomeNSErrorSubclass alloc] init...] autorelease];
            return nil;
        }
    }
    

    Note that this also allows the calling code to ignore errors by simply passing in NULL for the error: parameter, as follows:

    NSArray *array = [moc executeFetchRequest:request error:NULL];
    

    Update: (in response to questions):

    There are two reasons why the argument type has to be NSError** instead of NSError*: 1. variable scoping rules, and 2. NSError instances are imutable.

    Reason #1: variable scoping rules

    Let’s assume that the function declaration were to look like this:

    - (NSArray*)executeFetchRequest:(Request *)request error:(NSError*)error;
    

    And we were to call the function like this:

    NSError * error = nil;
    [someArray executeFetchRequest:someRequest error:error];
    if (error != nil) { /* handle error */ }
    

    When you pass in a variable this way, the function body will not be able to modify the value of that variable (i.e. the function body will not be able to create a new variable to replace the existing one). For example, the following variable assignments will exist only in the local scope of the function. The calling code will still see error == nil.

    - (NSArray*)executeFetchRequest:(Request *)request error:(NSError*)error {
        ...
        error = [[[NSError alloc] init...] autorelease];             // local only
        error = [[[SomeNSErrorSubclass alloc] init...] autorelease]; // local only
    }
    

    Reason #2: instances of NSError are immutable

    Let’s keep the same function declaration, but call the function like this:

    NSError * error = [[[NSError alloc] init...] autorelease];
    [someArray executeFetchRequest:someRequest error:error];
    if (error != nil) { /* handle error */ }
    

    First of all, the variable scoping rules guarantee that error can not be nil, so the if (error != nil) { ... condition will always be true, but even if you wanted to check for specific error information inside the if block, you would be out of luck because instances of NSError are immutable. This means that once they are created, you cannot modify their properties, so the function would not be able to change the domain or userInfo of that NSError instance that you created in the calling code.

    - (NSArray*)executeFetchRequest:(Request *)request error:(NSError*)error {
        ...
        error.domain = ...   // not allowed!
        error.userInfo = ... // not allowed!
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just finished my website here I used ASP.net & MySQL. I uploaded my
How do I ignore case when using pcre_compile and pcre_exec? pcre_exec( pcre_compile(pattern,0,&error,&erroroffset,0), 0, string,
Getting this error with jquery & jquery.form. Site has been live for awhile..upgraded to
The error occurs when I try to do this friend std::ostream& operator<<(std::ostream& os, const
My password contains special characters like '&' and VS giving error on this like
Sometimes users mistakenly redirected to ?Process=ViewImages&PAGEID=. When this happens, they get the following error.
Consider the following: int ival = 1.01; int &rval = 1.01; // error: non-const
#!/bin/bash if test $# == 4; then echo $*; else echo args-error >&2; fi;
why does void operator<<(ostream out, Test &t); return an error whereas void operator<<(ostream &out,
here is a url of my website, this url is used to call the

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.