实例介绍
个人原创,自己搞的PDF,谢绝转发,Effective Objective-C 2.0- 52 Specific Ways to Improve Your iOS and OS X Programs.pdf
John fuller Project Editor Elizabeth Ryan Packager Vicki rowland Copy Editor Evelyn W. Pyle Indexer Sharon hilgenberg Proofreader Archie Brodsky Technical reviewers Anthony herron Cesare Rocch Chris Wagner Editorial assistar Olivia bassegio Cover Designer Chuti Prasertsith Compositor Vicki rowland o Rosie Preface Acknowledgments About the author Chapter 1: Accustoming Yourself to Objective-C Item 1: Familiarize Yourself with Objective-C's Roots Item 2: Minimize Importing Ileaders in Headers Item 3: Prefer Literal Syntax over the Equivalent methods Item 4: Prefer Typed Constants to Preprocessor #define Item 5: Use Enumerations for States, Options, and Status Codes Chapter 2: Objects, Messaging, and the Runtime Item 6: Understand Properties Item 7: Access Instance Variables Primarily Directly When Accessing Them Internally Item 8: Understand Object Equality Item 9: Use the Class Cluster Pattern to Hide Implementation Detail Item 10: Use Associated Objects to Attach Custom Data to Existing Classes Item 11: Understand the Role of objc msgSend Item 12: Understand Message Forwarding Item 13: Consider Method Swizzling to Debi ug Opaque methoda Item 14: Understand What a Class Object Is Chapter 3: Interface and API Design Item 15: Use Prefix Names to Avoid Namespace Clashes Item 16: Have a Designated Initializer Item 17: Implement the description Method Item 18: Prefer Immutable objects Item 19: Use Clear and Consistent Naming Item 20: Prefix Private Method names Item 21: Understand the Objective-C Error Model Item 22: Understand the NSCopying Protocol Chapter 4: Protocols and Categories Item 23: Use Delegate and Data Source Protocols for Interobject Communication Item 24: Use Categories to Break Class Implementations into Manageable Segments Item 25: Always Prefix Category Names on Third-Party Classes Item 26: Avoid Properties in Categories Item 27: Use the Class-Continuation Category to Hide Implementation Detail Item 28: Use a Protocol to Provide Anonymous Objects Chapter 5: Memory Management Item 29: Understand Reference Counting Item 30: Use ARC to Make Reference Counting Easier Item 31: Release References and Clean Up Observation State Only in dealloc Item 32: Beware of Memory Management with Exception-Safe Code Item 33: Use Weak References to Avoid Retain Cycles Item 34: USe Autorelease Pool Blocks to Reduce High-Memory Waterline Item 35: Use Zombies to Help Debug Memory-Management Problems Item 36: Avoid Using retainCount Chapter 6: Blocks and Grand Central Dispatch Item 37: Understand Blocks Item 38: Create typedefs for Common Block Types Item 39: Use Handler Blocks to Reduce Code Separation Item 40: Avoid Retain Cycles Introduced by Blocks Referencing the Object Owning Them Item 41: Prefer Dispatch Queues to Locks for Synchronization Item 42: Prefer gCd to performselector and friends Item 43: Know When to Use GCD and When to Use Operation Queues Item 44: Use Dispatch Groups to Take Advantage of Platform Scaling Item 45: Use dispatch once for Thread-Safe Single-Time Code Execution Item 46: Avoid dispatch get current queue Chapter 7: The System Frameworks Item 47: Familiarize Yourself with the System Frameworks Item 48: Prefer Block Enumeration to for Loops Item 49: Use Toll-Free Bridging for Collections with Custom Memory Management Semantics Item 50: Use NSCache Instead of NSDictionary for Caches Item 51: Keep initialize and load Implementations Lean Item 52: Remember that NSTimer Retains Its Target Index Preface Objective-C is verbose. Objective-C is clunky. Objective-C is ugly. I have heard all these things said about objective-C. On the contrary, I find it elegant, flexible, and beautiful However, to get it to be these things, you must understand not only the fundamentals but also the quirks, pitfalls, and intricacies: the topic of this book About this book This book doesn't teach you the syntax of Objective-C. It is assumed that you know this already. Instead, this book teaches you how to use the language to its full potential to write good code. Objective-C is extremely dynamic, thanks to having its roots in Smalltalk. Much of the work thats usually done by a compiler in other languages ends p being done by the runtime in Objective-C. This leads to a potential for code to function fine during testing but to break in strange ways later down the line in production, perhaps when processing invalid data. Avoiding these problems by writing good code in the first place is, of course, the best solution Many of the topics are not strictly speaking, related to core Objective-C. Reference is made to items found in system libraries, such as Grand Central Dispatch, which is part of libdispatch. Similarly, many classes from the Foundation framework are referred to, not least the root class, NSobject, because developing with modern Objective-C means developing for Mac Os X or iOS. When developing for either, you will undoubtedly be using the system frameworks, collectively known as Cocoa and Cocoa Touch respectively Since the rise of iOS, developers have been flocking to join the ranks of objective-C development. Some of these developers are new to programming, some have come from Java or C++ backgrounds, and some have come from web-development backgrounds. In any case, all developers should take the time to learn how to use a language effectively. Doing so will yield code that is more efficient, easier to maintain, and less likely to contain bugs Even though I have been writing this book for only around six months, it has been years in the making. I bought an iPod Touch on a whim; then, when the first sdk for it was released, I decided to have a play with development. That led to me build my first"app which I released as Subnet Calc, which immediately got many more downloads than I could have imagined. I became certain that my future was in this beautiful language I had come to know. Since then, I have been researching Objective-C, regularly blogging aboutitonmywebsitewww.galloway.me.uk/.Iammostinterestedintheinner workings, such as the guts of blocks and how arc works. When i got the opportunity to write a book about this language, i jumped at the chance In order to get the full potential from this book, I encourage you to jump around it hunting for the topics that are of most interest or relevant to what you're working on right now. Each item can be read individually, and you can use the cross-references to go to related topics. Each chapter collates items that are related, so you can use the chapter headings to quickly find items relevant to a certain language feature Audience for This Book This book is aimed at developers who wish to further their knowledge of objective-C and learn to write code that will be maintainable, efficient, and less likely to contain bugs Even if you are not already an objective-c developer but come from another object oriented language, such as Java or C++, you should still be able to learn. In this case reading about the syntax of Objective-C first would be prudent What This Book Covers It is not the aim of this book to teach the basics of objective-C, which you can learn from many other books and resources. Instead this book teaches how to use the language effectively. The book comprises Items, each of which is a bite-sized chunk of information. These Items are logically grouped into topic areas, arranged as follows .Chapter 1: Accustoming Yourself to Objective-C Core concepts relating to the language in general are featured here Chapter 2: Objects, Messaging, and the Runtime Important features of any object-oriented language are how objects relate to one another and how they interact. This chapter deals with these features and delves into parts of the runtime .Chapter 3: Interface and API Design Code is rarely written once and never reused Even if it is not released to the wider community, you will likely use your code in more than one project. This chapter explains how to write classes that feel right at home in Objective-C Chapter 4: Protocols and Categories Protocols and categories are both important language features to master Effective use of them can make your code much easier to read, more maintainable, and less prone to bugs. This chapter helps you achieve mastery .Chapter 5: Memory Management Objective-C's memory-management model uses reference counting, which has long been a sticky point for beginners, especially if they have come from a background of a language that uses a garbage collector The introduction of Automatic Reference Counting(ArC) has made life easier, but you need to be aware of a lot of important things to ensure that you have a correct object model that doesnt suffer from leaks. This chapter fosters awareness of common memory-management pitfalls Chapter 6: Blocks and Grand Central Dispatch Blocks are lexical closures for C, introduced by Apple. Blocks are commonly used in Objective-C to achieve what used to involve much boilerplate code and introduced code separation. Grand Central Dispatch(GCD)provides a simple interface to threading. Blocks are seen as gcd tasks that can be executed perhaps in parallel, depending on system resources. This chapter enables you to make the most from these two core technologies Chapter 7: The System Frameworks You will usually be writing Objective-C code for Mac Os X or iOS. In those cases, you will have the full system frameworks stack at your disposal: Cocoa and Cocoa Touch, respectively. This chapter gives a brief overview of the frameworks and delves into some of their classes If you have any questions, comments, or remarks about this book, I encourage you to contact me. You can find my full contact details on the web site for this book at www.effectiveobjectivec.com Acknowledgments When asked whether i would like to write a book about objective-C, I instantly became excited. i had already read other books in this series and knew that the task of creating one for Objective-C would be a challenge. But with the help of many people, this book became a reality Much inspiration for this book has come from the many excellent blogs that are dedicated to Objective-C. Mike Ash, Matt Gallagher, and "bbum are a few of the individuals whose blogs i read. These blogs have helped me over the years to gain a deeper understanding of the language. NSHipster by Mattt Thompson has also provided excellent articles that gave me food for thought while compiling this book. Finally, the excellent documentation provided by apple has also been extremely useful I would not have been in a position to write this book had it not been for the excellent mentoring and knowledge transfer that happened while i was working at MX Telecom Matthew Hodgson in particular gave me the opportunity to develop the companys first iOS application, building on top of a mature C++ code base. The knowledge I picked up from this project formed the basis of much of my subsequent work touch either for academic reasons or purely just being there for a beer and a chat. Ad in Over the years, I have had many excellent colleagues with whom I have always stayed have helped me while writing this book Ive had a fantastic experience with the team from Pearson. Trina MacDonald, olivia Basegio, Scott Meyers, and Chris Zahn have all provided help and encouragement when required. They have provided the tools for me to get the book written without distraction and answered my queries when necessary The technical editors i have had the pleasure of working with have been incredibly helpful. Their eagle eyes have pushed the content of the book to be the very best. They should all be proud of the level of detail they used when analyzing the manuscript. Finally, i could not have written this book without the understanding and support from Helen. Our first child was born the day I was supposed to start writing, so I naturall postponed for a short time. Both Helen and Rosie have been fantastic at keeping me going throughout About the author Matt Galloway is an ioS developer from London, UK. He graduated from the University of Cambridge, Pembroke College, in 2007, having completed an M Eng degree, specializing in electrical and information sciences. Since then, he has been programming, mostly in Objective-C. He has been developing for iOS ever since the first SDK was released. You'll find him on Twitter as @mattigalloway, and he is a regular contributortoStackOverflow(http://stackoverflow.com) 1. Accustoming Yourself to Objective-C Objective-C brings object-oriented features to C through an entirely new syntax. Often described as verbose, Objective-C syntax makes use of a lot of square brackets and isn't shy about using extremely long method names. The resulting source code is very readable but is often difficult for C++ or Java developers to master Writing Objective-C can be learned quickly but has many intricacies to be aware of and features that are often overlooked Similarly, some features are abused or not fully understood, yielding code that is difficult to maintain or to debug. This chapter covers fundamental topics; subsequent chapters cover specific areas of the language and associated frameworks Item 1: Familiarize Yourself with Objective-C's Roots Objective-C is similar to other object-oriented languages, such as C++and Java, but also differs in many ways. If you have experience in another object-oriented language, you'll understand many of the paradigms and patterns used. However, the syntax may appear alien because it uses a messaging structure rather than function calling. Objective-C evolved from Smalltalk, the origin of messaging The difference between messaging and function calling looks like this Click here to view code image // Messaging(Objective-C) Object * obj=[Object new] Lobj perform With: parameter and: parameter] // Function calling(C++) Object *obj= new Object obj->perform(parameter, parameter); The key difference is that in the messaging structure, the runtime decides which code gets executed. With function calling the compiler decides which code will be executed. When polymorphism is introduced to the function-calling example, a form of runtime lookup is involved through what is known as a virtual table. But with messaging, the lookup is always at runtime. In fact, the compiler doesn t even care about the type of the object being messaged. That is looked up at runtime as well, through a process known as dynamic binding, covered in more detail in Item 11 The objective-C runtime component, rather than the compiler, does most of the heavy lifting. The runtime contains all the data structures and functions that are required for the object-oriented features of Objective-C to work. For example, the runtime includes all the memory-management methods. Essentially, the runtime is the set of code that glues together all your code and comes in the form of a dynamic library to which your code is linked. Thus, whenever the runtime is updated, your application benefits from the performance improvements. A language that does more work at compile time needs to be recompiled to benefit from such performance improvements Objective-C is a superset of C, so all the features in the C language are available when writing Objective-C. Therefore to write effective Objective-C, you need to understand the core concepts of both C and Objective-C. In particular, understanding the memory model of C will help you to understand the memory model of Objective-C and why reference counting works the way it does. This involves understanding that a pointer is used to denote an object in objective-C. When you declare a variable that is to hold a reference to an object, the syntax looks like this 【实例截图】
【核心代码】
标签:
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论