IOSApplication DevelopmentObjective-CLearning is what we will introduce in this article,Objective-CIt is the main language for writing Mac software. If you adapt to basic object-oriented and C language, it will show you a lot of such content. If you do not know C, read the C Guide [English] first.
This guide is written and typeset by Scott steven son.
1. Calling Methods
To get started as soon as possible, let's take a look at some simple examples. Some basic syntaxes for calling methods of an object are as follows:
- [object method];
- [object methodWithInput:input];
Method returns a value:
- output = [object methodWithOutput];
- output = [object methodWithInputAndOutput:input];
You can also call the class method to create an object. In the following example, the string method of the NSString class is called and a new NSString object is returned:
- id myObject = [NSString string];
Type id indicates that the myObject variable can be referenced by any type of object. Therefore, during compilation, it does not know the classes and methods actually implemented.
In this example, the object type is obviously NSString, so the type can be changed:
- NSString* myString = [NSString string];
Now this is an NSString variable, so that the compiler will warn when it calls methods not supported by NSString in common sense.
Note that there is an asterisk on the right of the object type. All Objective-C object variables are pointer types. The id type is predefined as the pointer type, so asterisks are not required.
Nested messages
In many languages, nested methods or function calls are in the following form:
- function1 ( function2() );
The result of function 2 is used as the input of function 1. In Objective-C, nested messages are in the following format:
- [NSString stringWithFormat:[prefs format]];
Avoid nesting more than two calls in the migration, so that the code is more understandable.
Multiple Input Methods
Some methods accept multiple input values. In Objective-C, a method name can be divided into multiple segments. The multi-input method is as follows:
- -(BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
You can call this method as follows:
- BOOL result = [myData writeToFile:@"/tmp/log.txt" atomically:NO];
This is not just a name parameter. In the runtime system, the method name is actually writeToFile: atomically :.
2. accessors
In Objective-C, all instance variables are private by default. In most cases, the accessors should be used to obtain or set values. There are two types of syntax. This is the traditional 1.x Syntax:
- [photo setCaption:@"Day at the Beach"];
- output = [photo caption];
The second part of the Code does not directly access instance variables. It actually calls the caption method. In most cases, in Objective-C, you do not need to add the "get" prefix to getter.
When you see the code in square brackets, it means to send messages to objects or classes.
Point syntax
In Objective-C 10.5 of Mac OS X 2.0, point syntax supports getter and setter:
- photo.caption = @"Day at the Beach";
- output = photo.caption;
You can use either of the two syntaxes, but you can only use one of them in each project. The dot syntax can only be used in setter and getter, and cannot be used in methods for other purposes.
3. Create an object
You can create an object in two ways. The following is the first type:
- NSString* myString = [NSString string];
This is a more convenient form of automatic creation. In this example, an automatically released object is created. We will learn more details later. In many cases, you need to create an object in manual mode:
- NSString* myString = [[NSString alloc] init];
This is a nested method call. First, NSString itself calls the alloc method. This is a relatively low-level call for allocating memory and instantiating objects.
The second part is to call the init of the new object. Init usually implements some basic settings, such as creating instance variables. For users who use classes, the implementation details are unknown.
In some cases, you can use different init versions with inputs:
- NSNumber* value = [[NSNumber alloc] initWithFloat:1.0];
4. Basic Memory Management
When writing a Mac OS X program, you can enable garbage collection. In general, this means that you do not need to focus on memory management before you encounter a very complex situation.
However, it is impossible to always work in an environment that supports garbage collection. In this case, you need to know some key points.
If you create an object in the form of alloc manually, you need to release the object after use. You should not manually release any automatically released objects. If you do so, the application will exit unexpectedly.
There are two examples:
- // String1 will be automatically released
- NSString * string1 = [NSString string];
- // After use, the must release this when done must be released
- NSString * string2 = [[NSString alloc] init];
- [String2 release];
In this guide, we can think that automatic objects will always be released after the current function is complete.
There are still many things to learn about memory management, but we need to wait until we know more about some other things.