#文档名: Programming with OBJECTIVE-C

Source: Internet
Author: User

label:

#Document Name: Programming with Objective-C

## About Objective-C (OC for short)

When writing programs for iOS and OS X, Objective-C is the main language used by programmers. OC is an extended set of C language that provides object-oriented features and a dynamic runtime.
The OC syntax, basic types, and control flow statements are all 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
Like identifier support, it provides dynamic typing and dynamic binding, deferring many things to runtime.

### Content shocking glance

This document introduces the OC language and some extended examples. 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
Existing with the language, but many features and framework classes are inseparable.

### An App is built from a network of objects

Writing apps for iOS and OS X is mainly about dealing with objects. Objects are instances of the OC class. There are classes provided by Cocoa, as well as classes defined by themselves.

If you want to start creating your own class, start by describing that class, and learn for yourself what the public interface of the instance objects of this class looks like. This interface includes the relevant package
Common attribute data, as well as a series of methods. Method statement shows the object can accept messages at any time when they were called acceptable method of parameter information. Custom class
Also included in an implementation, sight includes executable code before all methods declared in the interface.

### with the "classification" to extend an existing class
In order to increase an existing class of little features, not worthwhile to create a new class can be a "category" to add custom behavior to an existing class by definition. Use classification can give any
What kind of add method, even inside the framework classes that you do not have the source code, such as NSString.

If you have an existing class of source code, you can use a class extension to add new properties or modify existing properties. Class extends a private behavior often used to hide it in a single source
File or a custom implementation of the framework.

### "Protocol" specification-defined message
The most frequent operation in OC program is actually a message to each other between objects. Generally, these messages are explicitly defined in the form of the interface class method. Sometimes, however, a group of
Related classes are not necessarily directly bound to a particular class.

OC uses a protocol to define a set of related methods, such as those methods that objects will call in delegates, which can be set as required or optional. Any class can choose
To implement a protocol, that is, this class must implement the methods that must be implemented as defined in the protocol.

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

The collection class will be wrapped and used as an instance of the Cocoa collection class, such as NSArray array, NSSet collection, or NSDictionary dictionary, which can be used as a collection of OC objects.

### "Code blocks" simplify common tasks

A code block is a language feature that has been introduced in C, OC, and C ++ to represent a unit of work; a code block wraps 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 testing operations in collection classes. Code blocks can also enable concurrent or asynchronous multitasking using GCD technology
Operation becomes simpler.

### Error objects used in runtime problems

Although OC includes exception handling syntax, Cocoa will only use exceptions when compile-time errors occur (such as array access out of bounds). These issues should be fixed before the App is released.
positive.

All other errors, runtime errors, such as disk space overflow or network service failure, are wrapped as instances of the NSError class. Your program should be prepared and handled
To ensure a good user experience.

### OC code should follow existing code specifications

When writing OC code, you should always keep in mind several code specifications. The method name should start with a lowercase letter, and then be named after camel case, doSomething and doSomethingElse. This is not to say capitalized
How important the letters are, but to make sure your code is readable, the method names should be meaningful, but not too verbose.

In addition, there are some rules that must be followed when using frameworks or language features. For example, property accessor methods must adhere to strict naming conventions to support technologies such as KVC and KVO
Surgery.

## Leading knowledge

If you are new to development, before studying this document, please go to the Start Developing iOS Apps Today document and have a comprehensive understanding of iOS program development. Plus you also
To be familiar with the basic usage of Xcode, Xcode is an IDE tool for developing programs; use Xcode to write code, design interfaces, test applications, debug programs, etc.

In terms of language, it is good to have some basic knowledge of C language or a series of languages based on C language. This document does contain some sample code of C language flow control statements. in case
Do you have experience of other high-level languages, such as ruby or Python, that is possible, then down it.

Another basic knowledge of object-oriented leading some of the basic concepts, as well as some general basic principles of object-oriented. If absolutely no idea, it is recommended to look at the relevant section OC programming
concept.

OC use ARC to manage object's life cycle, ARC will manage these things, but if you can not use the ARC, you need a good thing they care about these memory management, you should look at
Advanced Memory Management Programming Guide document.

 

# Document Title: Programming with 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.