Objective-c Getting Started notes

Source: Internet
Author: User

Brief introduction

Based on the C language, you can build a class that requires two files by writing a single-C + + code:

. h's header file

. m implementation file (if it is a mixed-C + + file with a. mm file suffix)

Since there is a header file, so many people will take it with C + + analogy, its most important three features are: Message, Category, Protocol, and C + + analogy, the difference between the two:

1, it does not have new nor delete, but there are similar methods alloc, Dealloc;

2, does not support multiple inheritance, only supports single inheritance, but provides protocol;

3, it does not have a name space;

4, does not support the inline function;

5, the parameter default value is not supported;

C + + is too large, so it is not particularly appropriate to compare the two, compared with Java, I think it might be more appropriate

Quick Start

1) Definition of class

. h header File

@Interface

. m implementation file
"Hello.h"
@implementationFraction

@end

Usually we define a class that will inherit from NSObject. Someone would ask NSObject {} How omitted, because I did not define "implementation variable" so I omitted it. because OC does not provide namespaces, it is important to pay special attention to class naming, otherwise it is easy to overwrite/conflict and cause many unknown problems. Apple defines the class, the general class name is preceded by two letters, such as NSObject in the Ns,uikit UI, so in order to avoid the apple-defined class conflict, the custom class starts with three letters, such as BNR2) methods of definition and invocation methods are divided into two kinds: instance methods, class methods, as shown, the difference between the class method and the instance method, mainly its declarator:-, + + (return value type) class method name-(return value type) instance method name, need to be created before use, such as: [[[[[]] INIT] methodName]; Either way, the call is using the brackets "[]", which is exactly what it should be called a message, it has three features: receiver (message receiver) selector (selector, usually referred to as the method name) arguments (argument) method that calls itself in a class, using the [self Xx],self is an implicit local variable that does not need to declare self, and the program automatically assigns a value to itself, which is understood as this in JavaScript. all of the default methods in OC are public, so if you want to define a private method, you do not need to declare the method in. h, it is good to define the method directly in. m, but it is recommended to start with p_, which avoids conflicts with some private method names defined in Apple. 3) The basic data type OC is built on the C language, so all the basic data types in C are supported, such as int, float, char ...  The data types defined by OC itself are contained in the foundation, and the class name begins with NS, for example: Nsarray, Nsinteger, NSNumber, definition: NSString *mystr; Learn more about OBJECTIVE-C, recommended Ry ' s objective-c Tutorial

Objective-c Getting Started notes

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.