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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:36:10+00:00 2026-05-15T05:36:10+00:00

I put return in quotes because I don’t want to literally return it. I

  • 0

I put “return” in quotes because I don’t want to literally return it. I want to do it similar to how you pass a pointer-to-a-pointer for [NSString stringWithContentsOfFile:usedEncoding:error:].

I would like to make parseFiles:error return nil and have the error reference that was passed in contain the first or second error, depending on which one failed. It seems like a Cocoa way to do it?

EDIT: Sorry, I should’ve been more clear about where I was having the problem. If the first path is bogus, it functions as I want. (I get the error instance outside and it prints.) If the first path is legit, as it filler string below implies, I get EXC_BAD_ACCESS.

But now I fixed it. I need to refer to it as *error inside the parseFiles:error: method and use == nil when checking if it failed. I thought I could just to if (error)…

EDIT 2 Ok, it doesn’t work. I’m getting EXC_BAD_ACCESS. I’m not sure what I’m doing wrong with the conditions that check for the errors.

@implementation PassingError

- (id)init {
    self = [super init];

    NSError *error;
    [self parseFiles:@"/untitled.py" error:&error];

    if (error != nil) {
        NSLog(@"I failed because: %@", error);
    }
    return self;
}

// Wraps with reading errors.
- (NSString *)parseFiles:(NSString *)path error:(NSError **)error {

    NSStringEncoding enc1;
    NSString *contents1 = [NSString stringWithContentsOfFile:path
                                               usedEncoding:&enc1 error:*&error];

    // there was a read error

    // I need an asterisk here...
    if (*error != nil) {
        // ...and also one here
        NSLog(@"FIRST ERROR: %@", *error);
        return nil;
    }


    // here is where you'd do something that might cause another error,
    // I'll just try and read a second file for simplicity
    NSStringEncoding enc2;
    NSString *contents2 = [NSString stringWithContentsOfFile:@"/untitled.py"
                                               usedEncoding:&enc2 error:*&error];

    // there was a SECOND error
    if (*error != nil) {
        NSLog(@"SECOND ERROR: %@", *error);
        return nil;
    }


    // return both or whatever
    return [NSArray arrayWithObjects:contents1, contents2, nil];
}

@end
  • 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-15T05:36:11+00:00Added an answer on May 15, 2026 at 5:36 am

    Passing pointers around in Objective-C can get confusing. I remember having trouble grasping what needed to be done. When you have a method like this:

    - (BOOL) saveValuesAndReturnError:(NSError **) error
    {
        BOOL success = [self doSomethingImportant];
        
        if (!success && error)
        {
            // Unsuccessful and error is a valid ptr-to-ptr-to-NSError.
            
            // Basically, someone has given us the address of a (NSError *).
            // We can modify what that pointer points to here.
            *error = [NSError errorWithDomain:@"myDomain" code:100 userInfo:nil];
        }
        
        return success;
    }
    

    This is intended to be invoked like this:

    // If the caller doesn't care that it failed:
    [someObject saveValuesAndReturnError:NULL];
    
    
    
    // Or, if the caller wants to get error information on failure
    NSError *anError = nil;
    BOOL success;
    
    // pass address of our (NSError *)
    success = [someObject saveValuesAndReturnError:&anError];
    if (!success)
    {
        // anError now points to an NSError object, despite being initialised to nil,
        // because we passed the address of our NSError ptr, the method was able to 
        // change where `anError` points to.
        NSLog (@"An error occurred while saving values: %@", anError);
    }
    

    Perhaps a very relevant read in this case is a CIMGF blog post covering exactly this topic.

    However…

    I remember reading a while ago that methods that return errors via method arguments such as stringWithContentsOfFile:usedEncoding:error: make no guarantee not to modify the error argument for success. In other words, you cannot rely on the value of the error parameter if the method succeeded. In your particular case, it may be better to do:

    
    - (NSString *)parseFiles:(NSString *)path error:(NSError **)error {
    
        NSStringEncoding enc1, enc2;
        NSError *innerError;
        NSString *contents1 = [NSString stringWithContentsOfFile:path
                                                    usedEncoding:&enc1
                                                           error:&innerError];
        
        if (contents1 == nil)
        {
            if (error) *error = innerError;
            return nil;
        }
    
        NSString *contents2 = [NSString stringWithContentsOfFile:@"/untitled.py"
                                                    usedEncoding:&enc2
                                                           error:&innerError];
        
        if (contents2 == nil)
        {
            if (error) *error = innerError;
            return nil;
        }
        
        // do whatever with contents1 and contents2
    }
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I put better in quotes because it's a qualitative question. I've been writing COM
Simply put, is there a way to create a 2D javascript array using similar
I have a bunch of html I want to put into a database field:
I thought request.method is supposed to return a symbol like :get , :put etc.
in the index.php, i put this code in. $smarty->assign('goods_commnet', get_comment_count()); function get_comment_count(){ return 2;
All numbers that divide evenly into x. I put in 4 it returns: 4,
Put differently: Is there a good reason to choose a loosely-typed collection over a
Put it another way: what code have you written that cannot fail. I'm interested
We put all of our unit tests in their own projects. We find that
We put common prefixes on related tables to assure they display next to each

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.