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
- 浏览: 2539719 次
- 性别:
- 来自: 成都
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
相关推荐
根据给出的内容,我们可以总结出以下关于Objective-C编程语言的知识点,这些知识点将覆盖从Objective-C的基础知识到高级特性,以及如何在iOS移动开发中使用Objective-C: 1. Objective-C简介 Objective-C是一种面向...
Based on Big Nerd Ranch's popular Objective-C Bootcamp, Objective-C Programming: The Big Nerd Ranch Guide covers C, Objective-C, and the common programming idioms that enable developers to make the ...
UNIT 6 - WORKING WITH MUTABLE DATA TYPES Lesson 24 - Mutable and immutable objects Lesson 25 - Working with lists Lesson 26 - Advanced operations with lists Lesson 27 - Dictionaries as maps between ...
学习Objective-C之前,建议具备一定的C语言基础,了解基本的数据类型、控制流结构和指针操作。 ### See Also 参考文档和教程对于学习Objective-C非常有帮助。官方文档提供了详细的API参考和最佳实践指南。 ### ...
If you’re getting started with iOS development, or want a firmer grasp of the basics, this practical guide provides a clear view of its fundamental building blocks—Objective-C, Xcode, and Cocoa ...
《Objective-C 编程语言》是一本深入探讨Objective-C编程的资源,该压缩包包含的主文件是"The Objective-C Programming Language.pdf"。Objective-C是一种基于C语言的面向对象编程语言,广泛应用于苹果的iOS和macOS...
"Learn Objective-C on the Mac" 这本书的配套实例代码,为学习者提供了实践的机会,加深对Objective-C的理解。 首先,让我们深入了解一下Objective-C的关键概念: 1. **消息传递**: Objective-C 是基于 Smalltalk...
《Programming With Objective-C》是一本由Apple官方提供的介绍Objective-C编程语言的文档。Objective-C是一种在苹果平台上广泛使用的编程语言,尤其是在开发macOS和iOS应用程序时。在这份文档中,介绍了Objective-C...
《Learn Objective-C on the Mac》由Mark Dalrymple撰写,是一本旨在帮助开发者入门Objective-C的经典教程。 在本书中,读者将学习到以下核心知识点: 1. **Objective-C语法**:Objective-C的语法基于C语言,但...
《Object-Oriented Programming with Object C》是一本深入探讨面向对象编程(OOP)与Objective-C语言的专业书籍。Objective-C是Apple开发的一种强大的、面向对象的编程语言,主要用于iOS和macOS的应用程序开发。这...
You’ll learn how to work with the Xcode IDE, Objective-C’s Foundation library, and other developer tools such as Event Kit framework and Core Animation. Along the way, you’ll build example projects...
"Learn Objective-C On The Mac"教程是专为在Mac平台上学习Objective-C而设计的,它涵盖了语言的基本概念以及如何在Xcode集成开发环境中应用这些知识。 该教程的英文PDF部分可能包括以下章节和主题: 1. **...
Objective-C是C语言的超集,它继承了C的语法,并添加了面向对象的特性。这些特性包括类、对象、继承、多态和消息传递。类是对象的模板,而对象则是类的实例。继承允许创建一个类(子类)来扩展或修改另一个类(父类...
这个"Learn Objective-C 中文版 v2"的学习资源可能是针对那些希望深入理解Objective-C语言特性和编程实践的开发者设计的。下面我们将详细探讨Objective-C的关键知识点。 1. **面向对象编程基础**: - **类与对象**...
Objective-C是C语言的超集,它扩展了C语言,加入了面向对象的特性。面向对象编程(OOP)的核心概念包括类、对象、继承、封装和多态。Objective-C通过消息传递机制实现了这些概念,使得代码更加模块化和可维护。 2....
《Cocoa and Objective-C Cookbook》这本书提供了超过 70 个简单有效的实战案例,帮助读者掌握 Cocoa 和 Objective-C 的高级应用技巧。例如: - **使用 KVC 和 KVO 实现数据绑定**:介绍如何利用 KVC 和 KVO 来实现...
《Programming in Objective-C 2.0》是学习Objective-C编程语言的重要教材,主要面向初学者,也适合有一定编程基础但对Objective-C不熟悉的开发者。Objective-C是Apple开发平台上的主要编程语言,尤其在iOS和macOS...
Programming in Objective-C (6th Edition) Objective-C 是一种面向对象的编程语言,由Brad Cox和Tom Love在1980年代初开发,主要用于苹果公司的 macOS 和 iOS 操作系统中。Objective-C 是从 Smalltalk 语言中借鉴...
1. **Objective-C基础**:Objective-C是在C语言基础上扩展的,因此,理解C语言的基本语法是必要的。Objective-C添加了消息传递机制、类和协议等面向对象特性。 2. **消息传递**:Objective-C中的对象通过发送消息来...