IOS Developer Libray (Chinese version)--About OBJECTIVE-C

Source: Internet
Author: User
Tags gcd

This is my own learning iOS development when reading the document, some of the translation is not very accurate, but the meaning is still right, after all, my English is not very good, many sentences can not be accurate word translation, we may as a reference, there are mistakes welcome points, I will try to translate better, We work together to enter, interested students can study together.

Additional Word document download

About Objective-c on Objective-c

OBJECTIVE-C is the primary programming language you use when writing software for OS X and IOS. It's a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime. Objective-c inherits the syntax, primitive types, and flow control statements of C and adds syntax for defining classes an D methods. It also adds Language-level support for object graph management and object literals while providing dynamic typing and bin Ding, deferring many responsibilities until runtime.

Objective-c is the primary language programmed on OS X and iOS (hereafter referred to as OC), which is a superset of C and provides dynamic runtime (because OC is based on C, so you can understand that OC is a very good library of functions written by someone else, of course it is not so simple), OC inherits the syntax, basic data types, and flow control statements of C, and adds and defines its own syntax, classes, and methods. While providing both dynamic types and bindings, it also adds support for the object graph management and object literal language levels, deferring many tasks to run-time processing.

At a Glance overview

This document introduces the Objective-c language and offers extensive examples of their use. You'll learn how to create your own classes describing custom objects and see how to work with some of the framework class ES provided by Cocoa and Cocoa Touch. Although the framework classes is separate from the language, their use is tightly wound to coding with objective-c and Many language-level features rely on behavior offered by these classes.

This document describes the OC language and provides a number of usage examples. You will learn how to create a class to describe a custom object, how to use some cocoa or cocoa Touch to provide a framework class work. Although these framework classes are language-independent, they are closely related to programming with OC, and some language features depend on these classes.

An app are Built from a Network of Objects applications and objects

When building apps is OS X or IOS, you'll spend most of the your time working with objects. Those objects is instances of objective-c classes, some of which is provided for your by Cocoa or Cocoa Touch and some of which you ' ll write yourself.

When you create an OS X or iOS app, you spend most of your time on objects. These objects are the entities of the OC class, some are provided by cocoa or cocoa Touch, and some are custom to you.

If you ' re writing your own class, start by providing a description of the the class then details the intended public interface To instances of the class. This interface includes the public properties to encapsulate relevant data and along with a list of methods. Method declarations indicate the messages that a object can receive, and include information about the parameters require D whenever the method is called. You'll also provide a class implementation, which includes the executable code for each method declared in the interface.

If you create your own class, the first thing to prepare is the description of the class and the definition of the exposed interface, and the declaration of the Interface (interface) includes a (public) property and a list of methods. The declaration of a method defines a method that can receive a message, and the type of the parameter when the method is called. Of course you also need to provide the implementation of the method (Implementation), the executable code that contains the method in the implementation.

Relevant chapters : Defining Classes, working with Objects, encapsulating Data

Related chapters: defining classes, handling objects, data encapsulation

Categories Extend Existing Classes category and extension

Rather than creating an entirely new class to provide minor additional capabilities over an existing class, it ' s possible To define a category to add custom behavior to an existing class. You can use a category to add methods to any class, including classes for which you don't have the original implementation Source code, such as framework classes like nsstring.

Compared to creating a new class on the basis of an existing class to implement a small function, adding a class to the existing class is a better option, you can add classes to any class, including classes that you do not implement the source code (something someone else has written, or some of the system will take protective measures, can be referenced but not see the source code) such as: NSString in the framework.

Original source code for a class, you can use a class extension to add new properties, or modify the Tributes of existing properties. Class extensions is commonly used to hide private behavior for use either within a single source code file, or within the Private implementation of a custom framework.

If you have a class of source code, you can use the extension to add attributes, or modify existing properties, the extension is often used to deal with the behavior of hiding some classes inside, or in some of the custom framework. (This is one of the protective measures)

Relevant chapters: customizing Existing Classes

RELATED links: Customizing existing classes (to save time and effort, no more repeating hyperlinks will be added later)

Protocols Define Messaging Contracts Protocol

The majority a Objective-c app occurs as a result of objects sending messages to each other. Often, these messages is defined by the methods declared explicitly in a class interface. Sometimes, however, it's useful to being able to define a set of related methods the aren ' t tied directly to a specific CLA Ss.

A large part of the OC app's work is to reproduce the messages sent by other applications (which can simply be understood as communication). Typically, these messages are explicitly declared in the interface of the class, but sometimes the message is used by a series of classes instead of a class.

OBJECTIVE-C uses protocols to define a group of related methods, such as the methods an object might call on its delegate, Which is either optional or required. Any class can indicate that it adopts a protocol, which means that it must also provide implementations for all of the Req Uired methods in the protocol.

The OC uses the protocol to define a set of related methods, such as the Protocol provided by the delegate object, which declares whether the method is optional (optional) or required (required). Any class that adheres to this protocol needs to implement the (required) method that must be implemented in the Protocol.

Relevant chapters: Working with Protocols

Related chapters: Use agreements

Values and collections is Often represented as Objective-c Objects value and collection

It's common in objective-c to use Cocoa or Cocoa Touch classes to represent values. The NSString class is used for strings of characters, the NSNumber class for different types of numbers such as Integer or Floating point, and the Nsvalue class for other values such as C structures. You can also with any of the primitive types defined by the C language, such as int, float or char.

One common thing in OC is to use classes in Cocoa or Cocoa Touch to represent values, such as the NSString class is used to represent characters in a string, and the NSNumber class is used to represent different numeric types, such as shaping, floating-point types, and nsvalue to represent the struct of C. Of course, you can also use any basic C data type, such as int, float, char.

Collections is usually represented as instances of one of the collection classes, such as Nsarray, Nsset, or nsdictionary , which is each used to collect and other objective-c objects.

Collections are usually represented by entities of a class, such as Nsarray, Nsset, and nsdictionary, which are used to assemble other OC objects.

Relevant chapters: Values and collections

Related chapters: Values and collections

Blocks Simplify Common Tasks block code block

Blocks is a language feature introduced to C, Objective-c and C + + to represent a unit of work; They encapsulate a block of code along with captured state, which makes them similar to closures in other programming lang Uages. Blocks is often used to simplify common tasks such as collection enumeration, sorting and testing. They also make it easy-to-schedule tasks for concurrent or asynchronous execution using technologies like Grand Central Di Spatch (GCD).

BLOCKS (code block) is a language feature that is introduced in C, OC, C + + to represent a unit task, which encapsulates a piece of code capture State, which makes him look like it is completely isolated from other programming languages. Code blocks are often used to handle some common things, such as data collection, classification, and testing. At the same time, he makes it easy to schedule concurrent or asynchronous work with GCD.

Relevant chapters: Working with Blocks

related chapters: Using BLOCK

Error Objects is used for Runtime problems nserror

Although objective-c includes syntax for exception handling, Cocoa and Cocoa Touch use exceptions only for programming err ORS (such as Out of bounds array access), which should was fixed before an app is shipped.

Although the syntax contained in OC contains exception handling, cocoa and cocoa touch are only used to handle programming errors (such as array out-of-bounds), which should be the size of the array before the array is loaded.

All other errors-including runtime problems such as running out of disk space or not being able to access a web Service-ar E represented by instances of the Nserror class. Your app should plan for errors and decide how best to handle them on order to present the best possible user experience W Hen something goes wrong.

All other errors-including run-time problems, such as insufficient disk space and inability to use the network, are handled by the Nserror class. The application should prepare for possible errors and develop a reasonable approach to provide a better user experience.

Relevant chapters: Dealing with Errors

Related sections: error handling

OBJECTIVE-C code follows established conventions coding habits

When writing objective-c code, you should keep a number of established coding conventions. Method names, for example, start with a lowercase letter and use camel case for multiple words; For example, dosomething or Dosomethingelse. It's not just the capitalization that ' s important, though; should also make sure this your code is as readable as possible, which means this method names should be expressive, b UT not too verbose.

When you write the OC code, you should pay attention to some coding habits. For example, method name: Start with a lowercase letter, use the Hump naming method, like DoSomething, Dosomethingelse. It's not just a formality, it's important, and you should make sure your code is readable, so your method name should be able to express its meaning clearly, but not too verbose.

In addition, there is a few conventions that is required if you wish to take advantage of language or framework features . Property accessor methods, for example, must follow strict naming conventions in order to work with technologies LIKEKEY-V Alue Coding (KVC) or Key-value observing (KVO).

In addition, if you want to use the advantages of the OC language framework, there are some conventions you must follow, such as: property access methods, in order to use KVC or kvo you must strictly abide by the grammatical rules.

Relevant chapters: Conventions RELATED Links: conventions

Prerequisites Preparation Conditions

If you were new to OS X or iOS development, you should read through start developing IOS Apps Today or Start D Eveloping Mac Apps Today before reading this document, to get a general overview of the application development Proce SS for IOS and OS X. Additionally, should become familiar with Xcode before trying to follow the exercises at the end Of most chapters in this document. Xcode is the IDE used to build apps for IOS and OS X; You'll use it to write your code, design your app's user interface, test your application, and debug any problems.

If you are just beginning to touch OS X, iOS development, you should read through the start developing iOS apps today or start developing MAC apps today to learn about OS X, The general process of iOS app development, and in addition, you should familiarize yourself with the basic use of Xcode before you go. Xcode is for OS X, iOS app development editor. You can use it to write code, design applications, test and debug your app.

Although it ' s preferable to has some familiarity with C or one of the c-based languages such as Java or C #, this document Does include inline examples of basic C language features such as flow control statements. If you have knowledge of another higher-level programming language, such as Ruby or Python, you should is able to follow t He content.

Of course, you'd better have some C or C-based language, such as Java, C # documents have some C language features, such as flow control statements, if you have other advanced programming language experience, such as Ruby, Python, you should be able to accept the next content.

Reasonable coverages are given to general object-oriented programming principles, particularly as they apply in the context of Objective-c, but it is assumed so you had at least a minimal familiarity with basic object-oriented concepts. If you ' re not familiar with these concepts, you should read the relevant chapters in Concepts in Objective-c Programmi Ng.

To understand the basics of object-oriented development, let's say you have at least a little bit of object-oriented knowledge, especially object-oriented in OC. If you don't know anything about it, you should read the relevant chapters in OC development first.

See Also attached:

The content in this document applies to Xcode 4.4 or later and assumes is targeting either OS X v10.7 or later, or IO S 5 or later. For more information on Xcode, see Xcode overview. For information in language feature availability, see objective-c feature availability Index.

This document applies to Xcode4.4 and above, and is developed in OS X10.7 and above, or iOS5 or above, for more information about Xcode see: Xcode overview, information about language features, see: OC features.

Objective-c apps use reference counting to determine the lifetime of objects. For the more part, the Automatic Reference counting (ARC) feature of the compiler takes care of the. If you're unable to take advantage of ARC, or need to convert or maintain legacy code this manages an object ' s memory mans Ually, you should read the Advanced Memory Management ProgrammingGuide.

OC uses reference counting to determine the life cycle of an object, and in most cases the compiler's automatic memory management (ARC) function handles these things for you, and if you can't make ARC, or need to maintain old code that uses manual memory management (MRC), see: Advanced Memory Management Guide.

In addition to the compiler, the Objective-c language uses a runtime system to enable its dynamic and Object-oriented feat Ures. Although you don ' t usually need to worry on how Objective-c "works," it's possible to interact directly with this Runti Me system, as described by objective-c Runtime Programming Guide and objective-c runtime Reference.

In addition to compilers, OC uses the runtime mechanism to implement dynamic language and object-oriented features, but most of the time you don't have to worry about how OC works, you can use OC directly. Refer to: OC Programming Guide, OC Runtime Reference.

IOS Developer Libray (Chinese version)--About OBJECTIVE-C

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.