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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:33:36+00:00 2026-06-12T00:33:36+00:00

Xcode 4.3 I’ve read the SO questions on NSError**, so I wrote a simple

  • 0

Xcode 4.3

I’ve read the SO questions on NSError**, so I wrote a simple test program that uses a slightly different syntax recommended by Xcode 4.3 (see __autoreleasing below), so I’m not 100% sure if this is correct, although the code does appear to function properly. Anyway, just a simple file reader, prints an error if the file can’t be found.

Questions

Would like to know if the NSError initialization, argument passing using &, and error condition checking are correct.
Also, in the readFileAndSplit.. method, I noticed a big difference between if(!*error) and if(!error), in fact, if(!error) does not work when no error condition is raised.

File Reading Method w/Possible Error Condition

 -(NSArray*) readFileAndSplitLinesIntoArray:(NSError *__autoreleasing *) error {
    NSString* rawFileContents =
          [NSString stringWithContentsOfFile:@"props.txt"
                                    encoding:NSUTF8StringEncoding    
                                       error:error
    NSArray* fileContentsAsArray = nil;  
    if(!*error)
          fileContentsAsArray = 
                [rawFileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

    return fileContentsAsArray;

Caller

SimpleFileReader* reader = ...
NSError* fileError = nil;
NSArray* array = [reader readFileAndSplitLinesIntoArray: &fileError];

if(fileError){
  NSLog(@"Error was : %@, with code: %li", 
       [fileError localizedDescription],(long)[fileError code]);
} 
  • 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-06-12T00:33:37+00:00Added an answer on June 12, 2026 at 12:33 am

    There are a couple of issues.

    First, As per Apple’s Error Handling Programming Guide, you should be checking a method’s return value to determine whether a method failed or not, and not NSError. You only use NSError to get additional error information in the event that the method failed.

    E.g.:

    NSArray* fileContentsAsArray = nil;
    NSString* rawFileContents = [NSString stringWithContentsOfFile:@"props.txt"
                                    encoding:NSUTF8StringEncoding    
                                       error:error];
    if (rawFileContents)
    {
        // Method succeeded
        fileContentsAsArray = [rawFileContents ...];
    }
    
    return fileContentsAsArray; // may be nil
    

    Second, NSError out parameters are typically optional and may be NULL. But if you pass a NULL error variable into your method it will crash on this line:

    if (!*error) { 
    

    because you’re dereferencing a NULL pointer. Instead, you must always check for NULL before referencing a pointer, like so:

    if (error && *error) 
    {
        // Do something with the error info
    }
    

    However, if you rewrite the method as indicated above then you won’t be accessing the error variable at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

XCode: This template provides a starting point for an application that uses a single
Xcode 4 has a very nice built-in help/documentation that you can access e.g. by
Xcode 4 project contains two targets: " normal " application target and " test
XCode has been acting really, really strange recently. It is telling me that various
XCode is giving me a warning that I can't figure out why. @interface SFHFKeychainUtils
Xcode 4.2 had a tab in preferences that dealt with sharing compilation tasks within
Xcode Analyze complained that I incorrectly decremented reference count for username. Here is the
xcode gives me that error: Undefined symbols for architecture i386: _OBJC_CLASS_$_MPVolumeView, referenced from: objc-class-ref
Xcode is telling me that I'm missing a ')' but I can't tell where
XCode is a little weak in the way that it obliges you to format

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.