Programming with OBJECTIVE-C----Introduction----objective-c Learning (I.)

Source: Internet
Author: User

About 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 main programming language when writing OS X and iOS software. It is a superset of the C programming language and provides object-oriented functionality as well as a dynamic running environment. Objective-c inherits the syntax of the C language, the primitive types, and the process control statements, and adds syntax for defining classes and methods. In addition, it adds support for object graph management and object literals language layers, while providing dynamic type (typing) and binding (binding), as well as delaying many responsibilities until run time (deferring many Responsibilities until runtime).

At a Glance

I. 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 Objecttive-c and provides a large area of usage examples. You will learn how to create your own classes to describe custom objects and how to use some of the framework classes provided by Cocoa and Cocoa touch. Although frameworks are separated from language, they are closely related to OBJECTIVE-C programming. And the functionality of many language layers relies on the behavior methods provided by these classes.

An App was Built from a Network of Objects

1. An application is a network of 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 build OS X or IOS apps, you spend most of your time working with a variety of objects. Those objects are instances of the Objective-c class, some of which are provided by Cocoa or Cocoa touch, some of which you have written yourself.

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 are writing your own class, start by providing a description of the class that details the public interface that the instance of the class uses. This interface includes common features for encapsulating related data, as well as a range of methods. A method declaration indicates a variety of information that an object can receive and includes various parameter information that is required at any time to invoke the method. You will also provide an implementation of a class that includes code implementations for all the methods in the interface.

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

Related chapters: defining Classes, working with Objects, encapsulating Data

Categories Extend Existing Classes

2. Classification for the expansion of some classes

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 .

Instead of creating a whole new class to implement the minor additional functionality of an existing class, you might want to define a category (class) on the basis of an existing class to add custom behavior. You can use a category to add a variety of methods to any method, even those that do not have the original source code, such as the framework class for NSString.

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 do have an initial source code for a class, you can use the extension of a class to add a new attribute, or modify the properties of an existing attribute. Class extensions are typically used in a single source code file or in a private implementation of a custom framework to hide private behavior methods.

Relevant chapters: Customizing Existing Classes

Related chapters: Customizing Existing Classes

Protocols Define Messaging Contracts

3. Agreement to define various message contracts

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.

Most of the work that happens in a OBJECT-C application causes each object to pass various messages to each other. Typically, these messages are explicitly defined in the interface of a class. Sometimes it is also useful to define a series of related methods that are not bound to a particular class.

OBJECTIVE-C uses protocols to define a group of related methods, such as the methods an object might call on its Deleg Ate, 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.

Object-c uses a variety of protocols to define a set of related methods, such as the various methods an object might invoke in its proxy, which may be optional or necessary. If a protocol is used for any class, it means that it must implement all of the required (required) methods in the Protocol.

Relevant chapters: working with Protocols

Related chapters: Working with protocols

Values and collections are Often represented as Objective-c Objects

4. Values and sets are often seen as objective-c objects

It's common in objective-c to use Cocoa or Cocoa Touch classes to represent values. NSStringThe class is used for strings of characters, the NSNumber class for different types of numbers such as Integer or float ING 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 .

It is common to use Cocoa or Cocoa Touch classes to represent various values in OBJECT-C. The NSString class is used for strings, and the NSNumber class is used for different types of numbers, such as integer or floating-point, and Nsvalue classes are used to represent a variety of other values, such as the C-language structure. You can also use any of the original types defined in the C language, such as int, float, or char.

Collections is usually represented as instances of one of the collection classes, such as NSArray , NSSet , or NSDictionary , which a Re each used to collect other objective-c objects.

A collection is typically represented by an instance of the collection class, such as Nsarray, Nsset, or nsdictionary, which are used to collect other objective-c objects.

Relevant chapters: Values and Collections

Related chapters: Values and collections

Blocks Simplify Common Tasks

5. Block simplifies common tasks

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).

Block is a language feature used in C, objective-c, and C + + to represent a unit of code that encapsulates the state of code and snapping in a block, making them similar to the closure (closures) of other programming languages. Blocks are often used to simplify common tasks such as collection enumeration, sorting, and testing. They also make it easy to schedule tasks such as concurrency and asynchrony using the Grand Central Dispath (GCD) technology.

Relevant chapters: working with Blocks

Related chapters: Working with Blocks

Error Objects is used for Runtime problems

6. Error object used to troubleshoot run-time issues

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 objective-c contains syntax for exception handling, exception handling for cocoa and cocoa touch is used only for coding errors (such as array out-of-bounds) that need to be addressed before an application is published.

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---include run-time errors, such as insufficient disk space or inability to access a network service---are managed by instances of the Nserror class. When errors occur, in order to provide the best possible user experience, your application should plan for a variety of errors and decide how best to handle them.

Relevant chapters: dealing with Errors

Related chapters: dealing with Errors

Objective-c Code follows established conventions

7. Objective-c code follows established custom

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 object-c code, you should keep in mind some established customs. For the method name, for example, start with a lowercase letter, multiple words are concatenated with the first letter, such as dosomething or dosomethingelse; not only is capitalization important, you should also ensure that your code is readable as much as possible. This means that the method name should be impressive but not too long.

In addition, there is a few conventions that is required if you wish to take advantage of language or framework Featu Res. Property accessor methods, for example, must follow strict naming conventions  key-value coding  (KVC) or key-value observing  (KVO).

key-value coding  (KVC) or  key-value observing  (KVO) works together, and attribute accessor methods must follow strict naming conventions.

Relevant chapters: conventions

Related chapters: Conventions

Prerequisites

II. Preparation of knowledge

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 starting OS X or iOS development, you should read the document first before reading start developing IOS apps today or start developing MAC apps today , so You can read the iOS and OS x development process with a rough idea. In addition, you should familiarize yourself with Xcode before most of the sections in your exercise book documentation. Xcode is the IDE for building iOS and OS x applications, and you'll use it to write code, design your application's user interface, test your application, and debug a variety of issues.

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.

Although it is best to have some knowledge of C or some C language-based language, such as Java or C #, this document does contain some basic C language features, such as Process control statements. In addition, if you have other high-level programming language knowledge, such as Ruby or Python, you should be able to keep up with the content of this document.

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.

Object-oriented throughout the context of objective-c, and if you are unfamiliar with basic object-oriented concepts, you should read the relevant chapters in the concepts in OBJECTIVE-C programming .

See Also

Third, see

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.

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.

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 andobjective-c runtime Reference.

Next

Programming with OBJECTIVE-C----Introduction----objective-c Learning (I.)

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.