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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值