Objective-C syntax. Super Memory Management

Source: Internet
Author: User
Tags vars

Xcode: You can regard it as a development environment, just like Visual Studio, netbeans, or sharpdevelop.

Meaning. You can separate the functions that interface builder considers to be used to draw interfaces in Visual Studio.Program.
Objective-C: This is a language, just like C ++ is a language, Java is a language, C # is a language, and the history of Yingge is also

The same language.
Cocoa: It's a bunch of function libraries, just like MFC,. net, and swing. People have already written a bunch of ready-made stuff.

You only need to know how to use it.

Subtraction indicates the start of a function, method, or message.
Brackets can be thought of as how to call the method you just wrote. In objective-C, we usually say "message ".
Different objects can be saved in the same array.

Use NULL for C and nil for OC
Objective-C has a type called ID, which sometimes operates like void *, but it strictly specifies that it can only be used on objects.
Basic Category library import nsobject. h; bool type: yes or no
# Import vs # include: # import. # Import is supported by the GCC compiler. I do not recommend using # include, # import Base

This is basically the same as # ifndef # define # endif at the end of the. h file.
The default extension of objective-C is. M.

Class: @ interface... @ end
Implementation starts with @ implementation classname and ends with @ end. It is used to define functions in the class.
The inheritance (inheritance) is represented by class: parent, just like the above fraction: nsobject
In objective-C, the method for calling methods is [object method], just like the object-> method () of C ++ ()
[Object init] is a constructor call that initializes all variables in the object.
[Frac setnumerator: 1] is very simple. It calls the setnumerator Method on frac and passes 1 as the parameter.
Objective-C also has a way to release memory: Release
Set .. And: [frac2 setnumerator: 1 anddenominator: 5] is used to transmit multiple parameters.
The method to add other parameters is the same as when the second parameter is added, that is, method: label1: label2: label3:, and the call method is obj.

Method: param1 label1: param2 label2: param3 label3: param4]

Constructors)
@ Interface is like a normal function.
@ Implementation a New Keyword is used: Super
Use [Super init] to access super constructor. This action requires proper inheritance design.

Access permission
The default permission is @ protected.
Like the private: [list of vars] public: [list of vars] format in C ++, it is changed to @ private,

@ Protected, etc.

Class level access
Static int COUNT = 0; this is how class variable declares. + (INT) initcount; this is the return value of Count

Actual method. + (Void) initialize method is called when objective-C starts executing your program, and it also

Called by each class.

Exceptions)
Note: exception handling is only supported by Mac OS X 10.3 or above.
When exceptions are thrown out, you do not need to expand the (extend) nsexception object. You can simply use ID to represent it:

@ Catch (ID e ){...}

Inheritance, multi-type (inheritance, polymorphism), and other object-oriented functions

Objective-C is different from Java and C ++. You do not need to know the type of an object when calling the method of an object.

. Of course, this method must exist, which is called the message transmission of objective-C. You don't need to know the method you call.

The type of things. If this object reacts to this message, it will call this method.

Inheritance)
When you expand your super class (so there is only one parent), You want to customize the method of this super class, as long

Simply add a new implementation content in your child class implementation.

Dynamic Identification (dynamic types)
For example,-(bool) iskindofclass: classobj, that is, is object a descendent or member of classobj

Categories
When you want to add methods to a class, you usually expand (extend, that is, inherit) it. However, this is not necessarily a perfect solution.

Method, especially when you want to override a function of a class, but you do not have the original code. Categories allows you

Class adds new features, but does not need to be expanded. The ruby language has similar functions. Including-(fraction *) Add: (fraction *)

F;-(fraction *) Mul: (fraction *) F;-(fraction *) Div: (fraction *) F;-(fraction *) Sub:

(Fraction *) F;

Posing
Posing is a bit like categories, but not quite the same. It allows you to expand a class and comprehensively assume (pose) This

Super class. If you want nsarraychild to play nsarrayCodeAll nsarray

Instead of nsarraychild. [Fractionb poseasclass: [fraction class];

Protocols
The protocol in objective-C is the same as the Java interface or the purely virtual class in C ++.
Protocol is very simple, basically @ protocol protocolname (methods you must implement) @ end
To follow a conform protocol, put the protocols to be followed in <> and separate them with commas. For example:

@ Interface someclass <Protocol1, Protocol2, protocol3>.
Protocol does not need to be placed in the methods list in the header file.
You can use @ protocol to test whether the object complies with the interface. If the object complies with this interface, [object conformstoprotocol:
@ Protocol (someprotocol)] returns a yes-type bool object.

Memory Management

Retain and release (retain and release)
Retain and release are the methods that all objects inherited from nsobject have. Each object has an internal counter.

, Which can be used to track the number of reference objects. If the object has three references, dealloc is not required. However

If the counter value reaches 0, the object must be dealloc. [Object retain] adds the counter value to 1 (the value starts from 1 ).

), [Object release] reduces the counter value by 1. If [object release] is called and the counter reaches 0

Dealloc.
You can call [OBJ retaincount] to obtain the int value of the counter.

Dealloc
When your objects contain other objects, You have to release them when you dealloc yourself. One advantage of objective-C is that you can pass

Message to nil, so you do not need to release an object through a bunch of Error-proof tests.
The sequence of the three actions in each set method is very important. Suppose you pass yourself as a parameter to your method (a bit strange

). If you first release and then retain, you will deconstruct yourself (destruct, relative

Construction )! This is why we need 1) Retain 2) Release 3) set the value.

Autorelease pool
When you want to use nsstring or other foundation framework classes for more program design work, you need

An elastic system, that is, using autorelastpools. When developing Mac cocoa applications, autorelease pool will

Help you set it dynamically.
# Import <Foundation/nfutoreleasepool. h>
There are two ways to manage memory in objective-C: 1) Retain and release or 2) Retain and

Release/autorelease. Each retain must correspond to a release "or" autorelease.

Foundation framework classes

The foundation framework status is similar to the standard template library of C ++. However, objective-C is true.

State recognition language (dynamic types), so it does not need to be as cool as C ++ (templates ). This ramework

Including object groups, networks, execution threads, and more.

Nsarray
There are two types of arrays (usually the most data-oriented part of foundation classes): nsarray and nsmutablearray

Mutable (mutable) indicates that it can be changed, but nsarray does not. This means you can create an nsarray,

The length cannot be changed.
Nsarray * arr = [[nsarray alloc] initwithobjects: @ "me", @ "Myself", @ "I", nil];
Sorting shows how to use selector to sort an object
Nsenumerator is similar to Java's listing system.

Nsdictionary
# Import <Foundation/nsdictionary. h>)

Note: materials are from the Internet

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.