Objective-c basic knowledge study notes, objective-c

Source: Internet
Author: User
Tags variable scope

Objective-c basic knowledge study notes, objective-c

Objective-c basic knowledge study notes (1)

I have always had the habit of recording notes, but I haven't shared anything for a long time. I just learned about IOS in the first half of the year. Now I have time to write. Due to the development needs, the company specially configured several new MAC for us, and let us learn about it for two weeks, and then officially started development. Now I will share some of my basic knowledge.

I. Development Tools

Svn management tool: versions (Note: I personally think this tool is not very useful, often slow and slow), Development Tool: xcode

 

Ii. IOS system architecture

IOS is based on the UNIX kernel and Android is based on the Linux kernel.

The IOS system architecture is divided into four layers: Core OS layer, Core Services layer, and Media layer) and Cocoa Touch layer ).

1. Core OS is located at the bottom of the iOS system architecture and is the Core operating system layer. It includes memory management, file system, power management, and some other operating system tasks, you can directly interact with hardware devices. As an app developer, you do not need to deal with this layer.

2. Core Services is the Core service layer, which can be used to access some iOS Services.

3. Media is the Media layer. Through this layer, we can use various Media files in an application to record audio and video, draw images, and create basic animation effects.

4. Cocoa Touch is a touchable layer. This layer provides various useful frameworks for application development, and most of them are related to user interfaces, essentially, it is responsible for user touch interaction on iOS devices.

 

Iii. Description of important suffixes

The. h suffix file finger file describes the data components of classes and the methods of classes, which is equivalent to interfaces.

. M files are implemented files (source files)

. Xib suffix file, which is an interface builder file that stores the user interface (UI) of the application)

 

Iv. Basic syntax

Objective c is object-oriented

1. # import (introduce header files) syntax

Eg: # import <Foundation/Foundation. sh>

# Import "SelfClass. h"

@ ClassSelfClass

Note: <> only search for system files, "" first searches for local files. If not, search for system files. import can automatically prevent the same file from being imported multiple times. The instance variable type is generally introduced with @ class, which can shorten the Compilation Time. It is generally used in interfaces, and the methods and variables in it are not available.

2. String NSString

@ "String"

3. boolean BOOL type

There are two values, YES and NO. Note: The number greater than 0 is not necessarily YES.

4. Console output NSLog ()

% @ Output NSString and other object values

 

V. definition and implementation of Classes

1. @ interface is used to define interface classes.

The interface is generally defined in the. h file (header file) to display the class structure.

Eg: defines the Circle Class Based on the NSObject class

@ InterfaceCircle: NSObject

{

ShapeColor * fillColor;

}

-(Void) draw;

-(Void) setFillColor: (ShapeColor *) fillColor secondName :( int) index;

@ End

1) variables in braces are instance variables of the class (only valid in the instance of the object, so it is called the instance variable ivar), followed by method declaration. Note: The c function prototype is not short-lived.

2) The return type of the method is the same as that of the C function: standard type (integer, floating point, string), pointer, referenced object, and struct. If the method uses parameters, a colon is required, otherwise, the colon is not required. Braces are not required when instance variables are not declared.

3) The preceding multi-parameter method definition does not require secondName, but it is not recommended.

4) Some methods are declared as the first plus sign. This method is a class method. This method is a class Object (rather than an Instance Object of the class), similar to a static method, this is usually used to create a new instance. We call the class method used to create a new object as the factory method.

5) there is a * number on the right side of the object type, and all object variables are pointer types. The id type (any type) has been predefined as the pointer type, so you do not need to add a * number.

2. @ implementation

The implementation class is generally implemented in the. m file (implementation file), implementing all the methods declared by the interface.

Eg: Implementation class of Circle above

@ Import "XXX. h" // introduce the header file defined above

@ ImplementationCircle

-(Void) setFillColor: (ShapeColor *) c

{

FillColor = c;

}

 

3. Object Instantiation

1), Circle * circle = [Circle new]; // This is not recommended

2), Circle * circle = [[Circle alloc] init]; // This is generally the initialization method.

4. Call Methods

[CiclesetFillColor: kRedColor]; // there is a space in the middle of the remarks

5. instance variable scope commands:

@ Protected the instance variable can be directly accessed by the class and any method defined in the subclass (default ).

@ Private instance variables can be directly accessed by methods defined in this class, and cannot be accessed directly by methods defined in the quilt class.

@ Public instance variables can be directly accessed by the methods defined in the class, or by methods defined in other classes or modules. So that other methods or functions can access instance variables through (->) (not recommended ).

@ Package for 64-bit images, you can access this instance variable anywhere where the image of this class is implemented.

6. Inheritance

@ Interface Circle: NSObject

Multi-inheritance is not supported, but Objective-C achieves multi-inheritance through other features, such as categories and protocols.

7. composition)

Composite is like composing in music: It combines multiple components and works together to obtain the complete work. Strictly speaking, only a combination of objects can be called a combination.

The relationship between composite classes is "has ". Note: In Objective-C, composite is implemented through object pointers that contain instance variables.

Eg: a car has 4 tires and an engine.

@ Interface Car: NSObject

{

Engine * engine;

Tire * tire [4];

}

Note: When memory is allocated to the new Car object, these pointers are initialized to nil (zero value)

 

Vi. id

Id shapes [2];

Shapes [0] = [[Circlealloc] init];

Shaoes [1] = [[Egg alloc] init];

It is a pointer to any type of object, which is equivalent to a normal type.

 

VII. Access Method Name

1. the setter method needs to add the set prefix before the property name. For example: setEngine

2. The getter method is named based on the name of the property returned by the getter method. For example, if the getEngine method violates the Naming Convention, do not use get as the prefix of the getter method, get has a special meaning in Cocoa. If get appears in the Cocoa method name, it means that this method uses the parameter you passed as a pointer to return a value.

 



Good Book for learning objective-c

1. the basic Objective-C tutorial combines theoretical knowledge with sample programs to comprehensively and systematically describe the related content of Objective-C programming, this includes the features introduced by Objective-C on the basis of C and the functions of the Cocoa toolkit and their frameworks, as well as inheritance, composition, source file organization, and many other important object-oriented programming technologies. The appendix also describes how to transition from other languages to Objective-C. Objective-C basic tutorial is suitable for various developers.

2. Getting started with Cocoa-using Objective-C helps you easily experience Cocoa development, not only reading, but also hands-on practice. After introducing Xcode and Interface Builder, you will soon be exposed to Objective-C's object-oriented programming concept, which is the preferred language for creating Mac OS X applications. Each chapter provides different sample programs for you to build, and provides step-by-step guidance to teach you the basics of Cocoa programming. The skills learned in each chapter are used as the foundation for the more advanced skills and concepts introduced in the next chapter.

3. "Objective-C2.0 programming (2nd)" is a classic book in the field of Objective-C, Objective-C is systematically and comprehensively elaborated, authority is beyond doubt. The book consists of four parts: The first part comprehensively describes the basic knowledge of Objective-C language, including classes, objects, methods, data types, expressions, program structures, inheritance, polymorphism, dynamic types and dynamic binding, functions, arrays, structures, and pointers; the second part describes the Foundation framework in detail, covering numbers, strings, sets, file operations, memory management, object replication and archiving, and the third part briefly introduces Cocoa and iPhone SDK; the fourth part is the appendix, which lists the quick reference of Objective-C.
"Objective-C2.0 programming (original book 2nd edition)" reasonable structure, informative content, simple and easy to learn, not only suitable for beginners and all programmers to read, but also can be used as a programming language subject of entry materials.

I have read the entity version of these three books, and they all write well. The language itself is not very complex. In contrast, the Cocoa framework needs to be familiar with and understand in a large number of practices.

These three books are available on Amazon.

Who have the notes for the basic Objective-c tutorial? Besides this book, what other books are suitable for beginners of objective-c?

Another article titled Objective-C 2.0 programming (version 2nd)
If you have other language basics, familiarize yourself with its syntax and development capabilities.
Familiarize yourself with the cocoatouch framework as early as possible, and find a real project to have a deeper understanding of OC during the development process.

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.