Learn Objective C(6)Programming with Objective-C - Working with Blocks and Deali

Learn Objective C(6)Programming with Objective-C - Working with Blocks and Dealing with Errors and Conventions

7. Working with Blocks
…snip...

8. Dealing with Errors
NSError objects.

8.1 Use NSError for Most Errors
Some Delegate Methods Alert You to Errors
For example, NSURLConnectionDelegate protocol includes a connection:didFailWithError: method.
- (void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error;

If an error occurs, this delegate method will be called to provide us with an NSError object to describe the problem.

Some Methods Pass Errors by Reference
- (BOOL) writeToURL:(NSURL *) aURL
               options: (NSDataWritingOptions) mask
                errors:( NSError **) errorPtr;

Before we call this method, we need to to create a suitable pointer.

NSError *anyError;
BOOL success = [receivedData writeToURL: someLocalFileURL
                                                  options:0
                                                       error:&anyError];
if(!success) {
     NSLog(@"write failed with error: %@", anyError);
}

We need to check the status first, then check the NSError object, if we are not interested in the error object, just pass NULL.

Recover if Possible or Display the Error to the User
How to alert the user, UIAlertView.

Generating Your Own Errors
Define the error domain first,
com.sillycat.easyLocation.ErrorDomain

NSString *domain = @"com.sillycat.easyLocation.ErrorDomain";
NSString *desc = NSLocalizedString(@"Unable to … ", @"");
NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : desc };

NSError *error = [NSError errorWithDomain:domain
                                                  code: -101
                                              userInfo:userInfo];

NSLocalizedString -----> Localizable.strings

8.2 Exceptions Are Used for Programmer Errors
NSException

@try {
     //do something that might throw an exception
}
@catch(NSException *exception) {
     //deal with the exception
}
@finally {
     
}

9. Conventions
9.1 Some Names Must Be Unique Across Your App
Class Names Must Be Unique Across an Entire App
Objective-C classes must be named uniquely not only within the code that you're writing in a project, but also across any frameworks or bundles you might be including.

We should avoid using generic class names like ViewController or TextParser.
NS   ----   Foundation(OS X and iOS) and Application Kit(OS X)
UI    ----   UIKit(iOS)
AB   ----   Address Book
CA ------ Core Animation
CI -------  Core Image

Method Names Should be Expressive and Unique Within a Class

Always Use a Prefix for Method Names in Categories on Framework Classes

Local Variables Must Be Unique Within The Same Scope

9.2 Some Method Names Must Follow Conventions
Accessor Method Names Must Follow Conventions
firstName ----  accessor method firstName
               ----  setter method    setFirstName
paused     ----  accessor method isPaused
               ----   setter method    setPaused

Object Creation Method Names Must Follow Conventions

NSMutableArray *array = [[NSMutableArray alloc] init ];

NSMutableArray *array = [NSMutableArray new];

NSMutableArray *array = [NSMutableArray array]; 


References:
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html#//apple_ref/doc/uid/TP40011210-CH8-SW1

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/ErrorHandling/ErrorHandling.html#//apple_ref/doc/uid/TP40011210-CH9-SW1

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Conventions/Conventions.html#//apple_ref/doc/uid/TP40011210-CH10-SW1

相关推荐