Objective-c Basic Grammar Quick Start

Source: Internet
Author: User
Tags null null


Objective-c is the most important development language in the field of Mac software development, if we are already familiar with C language or have the base of object-oriented language, it will be very useful for us to learn objective-c.


Method invocation (Calling Methods)
To get started as soon as possible, let's take a look at some simple examples. The basic method invocation in the OBJECTIVE-C syntax is this:


[Object method];

[Object Methodwithinput:input];

The method of the object can return a value:


output = [object Methodwithoutput];

output = [object Methodwithinputandoutput:input];

We can also invoke methods of how to create objects inside a class. In the following example, we call the string method of the NSString class:


ID myObject = [nsstring string];

The type of ID means that the MyObject variable can point to any type of variable. When we compile this application, we don't know the real classes and methods that he implements.


In this case, it is obvious that the object's type should be nsstring, so we can change his type:


nsstring* myString = [NSString string];

Now mystring is a variable of type NSString. This time if we try to use a method that NSString does not implement, the compiler warns us.


Be sure to note that there is an asterisk on the right side of the object type. All Objective-c object variables are pointer-type. The ID type has been pre-defined as a pointer type. So we don't need to add asterisks.


Nested message calls (Nested Messages)
Nested messages in many programming languages, or nested functions that look like this:


Function1 (function2 ());
The return value of the function2 is passed to function1 when the input parameter is entered. Inside the objective-c, nested message calls are like this:


[NSString stringwithformat:[prefs format];
We should try to avoid nesting more than two calls in one line of code. Because of this, the readability of the code is not very good.


Method of multi-parameter input (Multi-Input Methods)
Methods for multiple input parameters. In Objective-c, a method name can be divided into several paragraphs. In the header file, you should define a method for multiple input parameters:


-(BOOL) WriteToFile: (NSString *) path atomically: (bool) useauxiliaryfile;

We'll call it this way:


BOOL result = [MyData writetofile:@ "/tmp/log.txt" atomically:no];

The parameter does not have to be named for it. In the run-time system, the real name of this method is called writetofile:atomically:.


Accessors (Getter & Setter)

All instance objects in Objective-c are private by default. All in most cases we need to use accessors to read or set the value of the variable. There are two grammars that support such an operation, this time the traditional old syntax:


[Photo setcaption:@ "Day at the Beach"];
output = [photo caption];

The second line of code is not a straightforward way to read the variables of an object instance. In fact, it calls a method called caption. In Objective-c most cases we don't need to prefix getters with get.


Whenever we see square brackets, we actually send a message to an object or to a class.


Dot Syntax
In Objective-c 2.0, a new "." was added. The syntax of the operation. The OBJECTIVE-C 2.0 syntax is used in Mac OS X 10.5:


Photo.caption = @ "Day at the Beach";
output = photo.caption;

We can use both of these methods. But in a project it's best to keep the style consistent and use only one type. "." The operation can only be used in setters and getters, not in the general meaning of the method.


Creating objects
There are two main ways to create an object. The first approach looks like this:


nsstring* myString = [NSString string];

It's a very habitual style. In this way, we are creating an object of the system auto-release (autoreleased) type. About the automatic release type autoreleased, we will discuss it in depth later. In many cases, however, we need to manually create the object:


nsstring* myString = [[NSString alloc] init];

This is a nested method call. The first call to the NSString own Alloc method. This is a relatively low-level call because he creates the content and instantiates an object.


The second block of code calls the Init method of the newly created object. This init method implements more common basic settings, such as creating parameters for an instance object. For the general developer, the specifics of implementing the customer's class are unclear.


In some cases, we can assign the initial value in a non-initialized way:


nsnumber* value = [[NSNumber alloc] initwithfloat:1.0];

Basic Memory Management
If we are developing an application for Mac OS X, we can choose whether to enable the garbage collection mechanism. This means that we don't need to think about memory management, except for a particularly complex situation that we need to deal with.


However, there are times when our development environment does not have a garbage collection mechanism, such as when the iphone is developed without a garbage collection mechanism. In this case, we need to understand some basic concepts of memory management.


If we create an object manually through Alloc, we need to use this object and release it. We do not need to manually release a autoreleased type object, and if we do, our application will be crash.


Here are two examples:


String1 'll be released automatically

nsstring* string1 = [NSString string];

Must release this if done

nsstring* string2 = [[NSString alloc] init];

[string2 release];

For this tutorial, we can artificially autoreleased objects that are freed after the current function method call is complete.


Of course, there's a lot of memory management that we just need to learn, but that requires us to learn more about the basic concepts before we go into it.


Design a interface of a class
In the case of Objective-c languages, creating a class is very simple. It is very typical divided into two parts.


The interface of the class is usually stored in the ClassName.h file, which defines the parameters of the instance, as well as some public methods.


The implementation of the class is in the CLASSNAME.M file. It contains real-running code and those methods. It also often defines a few private methods. These private methods are not visible to subclasses.


Here is an overview of the interface file. Class name photo, so the file is named Photo.h:


#import

@interface Photo:nsobject {

nsstring* caption;

nsstring* photographer;

}

@end

First, let's import the Cocoa.h into the door. Most of the basic classes of cocoa applications are done in this way. #import Macros automatically avoid including the same file multiple times


@interface symbol indicates that this is a declaration of the photo class. The colon specifies the parent class. In this example, the parent class is nsobject.


Inside the braces, there are two variables: caption and photographer. All two are of the nsstring type. Of course, they can also be of any other type including the ID type.


Finally @end ends the entire declaration.


Add method
Let's add some getters to the member variables:


#import

@interface Photo:nsobject {

nsstring* caption;

nsstring* photographer;

}

-caption;

-Photographer;

@end

Don't forget that the Objective-c method does not need to add a get prefix. A separate small crossbar indicates that it is a method of an instance. If it is a plus, it means that it is a class method.


The return type of the compiler default method is ID. There are also the default types of parameters for all methods that are also ID types. So the above code is technically right. But it's rarely used. Let's add the return type to it:


#import

@interface Photo:nsobject {

nsstring* caption;

nsstring* photographer;

}

-(nsstring*) caption;

-(nsstring*) photographer;

@end

Here we add setters:


#import

@interface Photo:nsobject {

nsstring* caption;

nsstring* photographer;

}

-(nsstring*) caption;

-(nsstring*) photographer;

-(void) Setcaption: (nsstring*) input;

-(void) Setphotographer: (nsstring*) input;

@end

Setters does not need to return any value, so we specify its type as void.


Implementation of the class
We create an implementation of a class by implementing getters:


#import "Photo.h"

@implementation Photo

-(nsstring*) caption {

return caption;

}

-(nsstring*) photographer {

return photographer;

}

@end

This part of the code starts with @implementation plus the class name, ending with @end. Just like the interface definition of a class, all methods are the same as in the interface definition. All objects need to be defined and implemented as well.


If we had written the code before, the getters in Objective-c looked similar to the others. So we're going to introduce setters, and it needs a little clarification.


-(void) Setcaption: (nsstring*) input

{

[Caption Autorelease];

Caption = [input retain];

}

-(void) Setphotographer: (nsstring*) input

{

[Photographer Autorelease];

Photographer = [input retain];

}

Each setter handles two variables. The first is an app that currently has objects. The second one is the new input object. In a development environment that supports garbage collection, we just need to assign a new value directly:


-(void) Setcaption: (nsstring*) Input {

caption = input;

}

But if we can not use garbage collection mechanism, we need to retain the old object first and then retain the new object.


There are two ways to dispose of a reference object: Release and Autorelease. The standard release will delete the reference directly. The Autorelease method will release it at some point in the future. It will undoubtedly exist before the end of its declaration cycle. In this case, the photographer object in Setphotographer above will be freed at the end of the function.


It is safe to use autorelease in the setter, because the new object and the old object are likely to be the same object that the same object might point to. For an object we are about to retain, we should not release it immediately.


This may seem confusing now, but as we learn, it will be more and more understandable. Now we don't need to fully understand it at once.


Initialization
We can create an initialization method to assign an initial value to a member variable of an instance of a class:


-(ID) init

{

if (self = [super init])

{

[Self setcaption:@ "Default Caption"];

[Self setphotographer:@ "Default photographer"];

}

return self;

}

The code above doesn't feel good to explain, although the second line of code doesn't seem to work. This is a single equals sign that assigns the value of [Super init] to self.


It basically calls the parent class to implement its initialization. This if code snippet is to verify that initialization was successful before setting the default value.


Releasing Resources Dealloc
This dealloc method is called when an object wants to be deleted from the content. This is the best time for us to release a reference to a member variable inside a subclass:


-(void) dealloc

{

[Caption Release];

[Photographer release];

[Super Dealloc];

}

Start two lines we sent the release notification to two member variables. We don't want to use autorelease here. Use standard release a little faster.


The last line of [Super Dealloc]; very important. We have to send a message to let the parent class clear itself. If you do not, the object is not clean, there is a memory leak.


The dealloc is not invoked under the garbage collection mechanism. Instead, we need to implement the Finalize method.


More about memory management
OBJECTIVE-C's memory management system is based on reference counts. All we need to be concerned about is tracking our references and whether the memory is actually freed during the run time.


In the simplest terms, when we alloc an object, we should retain it at some point. Every time we call alloc or retain, we have to call release.


This is the reference counting theory. But in practice, there are only two cases in which we need to create an object:


1. Become a member variable of a class


2. Only temporary is used in a function


At more times, the setter of a member variable should simply autorelease the old object and then retain the new object. We just need to call release on Dealloc.


So what really needs to be done is a local reference within the management function. The only rule is that if we alloc or copy an object, we need to release or autorelease it at the end of the function. If we create it in another way, it doesn't matter.


Here is an example of managing a member object:


-(void) Settotalamount: (nsnumber*) input

{

[TotalAmount Autorelease];

TotalAmount = [input retain];

}

-(void) dealloc

{

[TotalAmount release];

[Super Dealloc];

}

Here is an example of a local reference. We just need to release the objects we created with Alloc:


nsnumber* value1 = [[NSNumber alloc] initwithfloat:8.75];

nsnumber* value2 = [NSNumber numberwithfloat:14.78];

Only release value1, not value2

[Value1 release];

Here is an example of using a local reference object to set a member variable:


nsnumber* value1 = [[NSNumber alloc] initwithfloat:8.75];

[Self settotal:value1];

nsnumber* value2 = [NSNumber numberwithfloat:14.78];

[Self settotal:value2];

[Value1 release];

Note that how to manage local references is actually the same. Whether or not you set it to a member variable. We do not need to consider the internal implementation of setters.


If we understand these things well, we basically understand the 80% objective-c memory management.


Property Properties
Before we write the accessors of caption and author, you can already notice that the code is very concise and should be abstracted by abstraction.


Properties are a new feature in Objective-c. He can let us generate accessors automatically, and there are other advantages. We can turn the class of photo above to be implemented with attributes:


The original implementation of the above class is this:


#import

@interface Photo:nsobject {

nsstring* caption;

nsstring* photographer;

}

-(nsstring*) caption;

-(nsstring*) photographer;

-(void) Setcaption: (nsstring*) input;

-(void) Setphotographer: (nsstring*) input;

@end

If the property is used to achieve this:


#import

@interface Photo:nsobject {

nsstring* caption;

nsstring* photographer;

}

@property (retain) nsstring* caption;

@property (retain) nsstring* photographer;

@end

@property is objective-c to declare the compiler directive for the property. The "retain" inside the parentheses indicates the object the setter needs to retain input. The other parts of this line specify the type and name of the property.


Let's take a look at the implementation of this class:


#import "Photo.h"

@implementation Photo

@synthesize caption;

@synthesize photographer;

-(void) dealloc

{

[Caption Release];

[Photographer release];

[Super Dealloc];

}

@end

@synthesize instructions are automatically generated by our setters and getters. So we just need to implement the Dealloc method of the class.


Accessors are generated only when they are not. So you can be confident to use @synthesize to specify properties. And you can freely implement your own getter and setter. The compiler will find out for itself which method is not.


Attribute declarations have other options, but are limited to the space level, we will introduce next time.


Logging


In Objective-c, it's very easy to write a diary to the console. In fact, NSLog () is almost identical to the C-language printf () two functions except that NSLog uses an extra "%@" to get the object.


NSLog (@ "The current date and time is:%@", [NSDate Date]);

We can log an object to the console. The NSLog function calls the description method to output the object, and then prints the returned nsstring. We can rewrite the description method in our own class so that we can get a custom string.


Method for calling a Nil object (calling Methods on nil)
In Objective-c, the nil object is designed to be associated with a NULL null pointer. The difference is that nil is an object, and null is just a value. And we're not going to generate crash or throw exceptions for the nil call method.


This technique is used by the framework in many different ways. The main thing is that we don't have to check whether this object is nil before invoking the method. If we have a method that has a return value for the Nil object, then we get a nil return value.


We can make our Dealloc function implementations look better with the nil object:


-(void) dealloc
{
Self.caption = nil;
Self.photographer = nil;
[Super Dealloc];
}

The reason we can do this is because we set the nil object to a member variable, the setter retain the Nil object (of course, the nil object doesn't do anything at this time) and then release the old object. It's better to release the object in this way, because if you do, the member variables don't even have the chance to point to random data, and in other ways, the opportunity to point to random data is unavoidable.


Notice the self that we called. The syntax of Var, which means that we are using a setter without causing any memory problems. If we go directly to set the value, there will be a memory overflow:


Incorrect. Causes a memory leak.
Use Self.caption to go through setter
Caption = nil;

Categories
Categories is one of the most commonly used functions in objective-c. Basically category allows us to add a method to an already existing class without having to add a subclass. And there is no need to know the specific implementation within it.


This is very effective if we want to add a method for a class that comes with a framework. If we want to add a method to the nsstring of our program engineering, we can use category. You don't even need to implement a nsstring subclass of your own.


For example, we want to add a method in NSString to determine if it is a URL, then we can do this:


#import

@interface NSString (Utilities)

-(BOOL) Isurl;

@end

This is very similar to the definition of a class. The difference is that category does not have a parent class, and there are category names in parentheses. Names can be taken casually, but the habit of the name will make people understand the category of what the function of the method.


Here is the concrete implementation. Note, however, that this is not a good implementation of the URL to judge. We primarily aim to understand the concept of category as a whole.


#import "Nsstring-utilities.h"

@implementation NSString (Utilities)

-(BOOL) Isurl

{

if ([Self hasprefix:@ "/http"])

return YES;

Else

return NO;

}

@end

Now we can call this method in any NSString class object. The following code prints the "string1 is a URL" in the console:


nsstring* string1 = @ "http://www.CocoaDev.cn/";

nsstring* string2 = @ "Pixar";

if ([string1 Isurl])

NSLog (@ "string1 is a URL");

if ([string2 Isurl])

NSLog (@ "string2 is a URL");

Unlike subclasses, category cannot increase member variables. We can also use category to rewrite the original method of existence of the class, but this needs to be very, very careful.


Remember, when we modify a class by category, it works on all objects in the application.


Postscript
The above objective-c of the comparative basis of the approximate said a bit. Objective-c is still better. No special grammar is needed to learn. And some concepts are used repeatedly in objective-c.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.



Objective-c Basic Grammar Quick Start


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.