Objective C, the core language for iOS development-language introduction and features, iosobjective

Source: Internet
Author: User

Objective C, the core language for iOS development-language introduction and features, iosobjective

This article is intended for developers who are interested in iOS development or who are already engaged in iOS development. If you are very interested in iOS development, you can discuss with me about iOS development, learn together, and make progress together. If you have no basic knowledge, we recommend that you first read the C language series in minutes for iOS development I shared earlier, and then start learning Objective C language. If you have any problems, you can also discuss it with me, in addition, I will share about 400 GB of iOS learning videos and learning materials I have prepared for free! You can follow James on Sina Weibo's private letter and look forward to learning and exploring with you !! Due to limited time, it is inevitable that there are deficiencies in the daily study and sharing after work, and I hope you can correct them!

Made by: James

1. Introduction to Objective-C

Ii. Comparison between OC and C
1. source file comparison
(1) common source file. h header file in c language,. c file
(2) source file. h header file in OC, implementation file of. m and. mm

2. Keyword comparison

3. Comparison of Data Types

4. Comparison of process control statements

5. Comparison of function (method) Definition and Declaration
Rules for compiling C-language functions: 1. Determining the function name 2. Determining the form parameter 3. Determining the return value 4. Determining the return value type
The OC writing method is also regular, and the rules are exactly the same as those in C.

6. New Features of the face object
(1) Encapsulation
(2) Inheritance
(3) polymorphism

7. Added object-oriented syntax
(1) Property Generator
@ Property
@ Synthesize

(2) category categroy
Classification and inheritance
Class extension without subclass

(3) Agreement
Use protocol declaration Methods
The Protocol is similar to the interfaces in C # and java.

(4) Fundation framework
Create and manage collections, such as arrays and dictionaries
Access images and other resources stored in applications
Create and manage strings
Publish and observe notifications
Creation date and time object
URL Stream Control
Asynchronous Code Execution

8. New Exception Handling

Iii. OC syntax Preview

1. Keywords
Basically, all keywords start @.
The following lists some common keywords:
@ Interface, @ implementation, @ end
@ Public, @ protected, @ private, @ selector
@ Try, @ catch, @ throw, @ finally
@ Protocol, @ optional, @ required, @ class
@ Property, @ synthesize, @ dynamic
Self, super, id, _ cmd, _ block, _ strong, _ weak,

2. OC Program Development Process

Iv. Object-oriented Thinking
Object-oriented is relative to process-oriented. Both object-oriented and process-oriented are an idea. process-oriented means that we must do everything ourselves, but object-oriented is only concerned about the steps needed to solve the problem.

Object-oriented features
(1) It is an idea that conforms to people's habits of thinking.
(2) simplify complicated things
(3) Switch programmers from executors to commanders
When the requirement is met:
First, you need to find the objects with the required functions for use.
If this object does not exist, create an object with the required functions.
This simplifies development and improves reuse.

Relationship between classes and objects
Creating objects in OC is complicated. First, you must understand a concept called class.
In real life, an object is created based on a description, and the programming language is the same as a template. A description must be provided first, in this description, the attributes and behaviors of the objects to be created in the future are clearly stated.

How to declare a class
Note:
1. It must start with @ interface and end with @ end
2. The declaration of member variables must be written in Braces Between @ interface and @ end.
3. The method declaration must be under {} and cannot be written in {}.

How to implement a class

Note:
1. It must start with @ implementation and end with @ end
2. The class name must be consistent with the declared

How to create an object
Features
Object Methods start with-, such as-(void) xx;
Object methods can only be called by objects.
You can access the member variables of the current object in the object method.
Call format [Object Name object method name];

Class Method description and implementation
1. Class method declaration
Format
Convert object method-number to + number

Features

Class method starts with +, for example, + (void) put;
Class methods can only be called by classes
Class methods cannot access instance (member) variables, because class methods are called by classes, and no buckets are created to store member variables in the class.

Benefits of class methods:
Memory saving
The execution efficiency is higher because it does not depend on objects;
If the problem can be solved using the class method, try to use the class method;

Class method application scenarios:
When you do not need to use member variables (attributes) inside a method, you can change them to class methods.
Class methods are generally used to compile tool Methods

Differences between object methods and class methods
1. Object Method
Object Methods belong to objects
Start with minus sign-
This method can only be called by objects without objects.
Object methods can access instance variables (member variables)
The object method of the current object can be called in the object method.
Object methods of other objects can be called.
Class methods can be called in the middle of object Methods

2. Class Methods
Class Methods belong to the class
Starting with a plus sign +
It can only be called by class name, and the object cannot be called
Instance variables (member variables) cannot be directly accessed in class methods)
Object methods cannot be called directly in class methods. To call object methods, you must create or import objects.

Usage:
When you do not need to access member variables (attributes or member variables), use the class method whenever possible.
Class methods and object methods can have the same name

Isa pointer
Each object contains an isa pointer (isa is a pointer, which occupies 8 bytes). This Pointer Points to the class to which the current object belongs.
[P eat]; indicates to send an eat message to the object pointed to by p, and call the eat method of the object. At this time, the object will find the method stored in the class along the internal isa pointer and execute it.
Isa is a hidden pointer in an object, pointing to the class that creates this object.
Using the isa pointer, we can know the Class that the current object belongs to during running.

Common Errors
1. There is only a declaration of classes, and there is no implementation of classes.
2. Missing @ end
3. @ interface and @ implementation nesting
4. The member variables are not written in brackets.
5. The method declaration is written in braces.
6. The member variables cannot be initialized in {}, and cannot be directly retrieved for access.
7. methods cannot be called as functions.
8. The OC method can only be declared between @ interface and @ end, and can only be implemented between @ implementation and @ end. That is to say, the OC method cannot exist independently of the class.
9. C functions do not belong to the class and are not associated with the class. C functions are only owned by the file defining the function.
10. The C function cannot access members of the OC object.
11. Low-Level Error: methods are declared, but functions are written during implementation.
12. OC can define a class without @ interface

Copyright Disclaimer: this is an original article by bloggers. To promote and learn from each other, please pay attention to Sina Weibo: James

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.