# Document name: use Objective-C for programming,

Source: Internet
Author: User

# Document name: use Objective-C for programming,

# Document name: use Objective-C for programming

# Objective-C (OC)

When writing programs for iOS and OS X, Objective-C is the main language used by programmers. OC is an extension set of C language. It provides object-oriented features and a dynamic runtime.
The OC syntax, basic type, and control flow statements are also inherited from the C language, and the syntax for defining classes and methods is also added. OC also adds language-level object Graphics Management and
Supports object identifiers to provide dynamic classification and dynamic binding, and delays many tasks to the runtime.

### A glimpse of the content

This document describes the OC language and some examples of extensions. Learn how to create custom classes to describe objects and how to use the framework classes provided by Cocoa and Cocoa Touch. Although the framework is independent
And language, but many features and framework classes are inseparable.

### An App is built on an object's Network

The App that writes iOS and OS X mainly deals with objects. The object is an instance of the OC class, including the classes provided by Cocoa and the classes defined by itself.

If you want to start creating your own class, you can start by describing the class and think about what the public interface of the Instance Object of this class looks like. This interface includes encapsulated related
There are also a series of methods to share the attributes of data. The Declaration of the method displays the information about the parameters that the method can accept when an object is called at any time. Custom class
It also includes an implementation, including the executable code of all methods previously declared in the interface.

### Use "category" to extend existing classes
To add a small function to an existing class, you can define a "category" to add custom behaviors to the existing class. You can use any
Class addition method, even the classes in the framework that you do not have source code, such as NSString.

If you have source code of an existing class, you can use a class extension to add new attributes or modify existing attributes. Class extension is often used. A private behavior is hidden in a single source.
File or a custom framework.

### "Protocol" defines message specifications
The most common operation in an OC program is to send messages between objects. Generally, these messages are explicitly defined in the class interface in the form of methods. But sometimes
Related classes are not necessarily directly bound to a specific class.

OC uses the Protocol to define a set of related methods. For example, the methods called by objects in the delegate can be set as required or optional. Any class can be selected
To implement a protocol, that is, this class must implement the required methods defined in the Protocol.

### Value classes and collection classes are often packaged as OC objects
Generally, OC uses classes in Cocoa to represent value classes. The NSString class is used to indicate strings. The NSNumber class is used to encapsulate integer or floating-point numbers. The NSValue class is used to encapsulate other classes.
Type, such as the structure of the C language. You can also use the basic types in C language, such as int, float, or char.

The Collection class is encapsulated into instances of the Cocoa collection class. For example, the NSArray array, NSSet set, or NSDictionary dictionary can be used as a set of OC objects.

### "Code block" simplifies general tasks

The code block is a language feature that has been introduced by C, OC, and C ++ to represent a unit of work. The code block encapsulates a piece of code with a pair of curly braces, which looks like a closure in other languages.
. Code blocks are often used to simplify general task operations, such as enumeration, sorting, and test operations in the Collection class. Code blocks can also enable the use of GCD Technology for concurrent or asynchronous multitasking
The operation becomes simpler.

### Error object used for running issues

Although OC contains the syntax for exception handling, Cocoa only uses exceptions when a compilation error occurs (for example, the array access is out of bounds). These problems should be fixed before the App is released.
Positive.

All other errors, runtime errors, such as disk space overflow or network service faults, are packaged as NSError instances. Your program should be prepared and processed
To ensure a good user experience.

### OC code must comply with existing code specifications

When writing OC code, you should always remember several code specifications. The method name should start with a lower-case letter, and then use the camper name, doSomething and doSomethingElse. This is not an upper case
How important letters are, but make sure your code is readable. The method name should be meaningful, but it should not be too long.

In addition, some rules must be followed when using the framework or language features. For example, attribute accessors must comply with strict naming rules to work with techniques such as KVC and KVO.
.

# Leading knowledge

If you are a beginner in development, before learning this document, go to the Start Developing iOS Apps Today document and have a comprehensive understanding of iOS program development. You also
To be familiar with the basic usage of Xcode, Xcode is an IDE tool for developing programs. Xcode is used to write code, design interfaces, test applications, and Debug programs.

In terms of language, it is good to have some knowledge of C language basics or a series of languages based on C language. This document does contain sample code of some C language process control statements. If
You have experience in other advanced languages, such as ruby or Python. You can also proceed.

Another basic leading knowledge is some basic concepts of object-oriented and some general basic principles of object-oriented. If there is no concept at all, we recommend that you read the relevant sections for OC programming.
Concept.

OC uses ARC to manage object lifecycles. ARC manages these things, but if you cannot use ARC, you need to care about these memory management things. You should look at it.
Advanced Memory Management Programming Guide document.

 

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.