[IOS learning]-objective-C basic learning

Source: Internet
Author: User

Cocos2d-x learning exchange group: 140326755

Email: hahayacoder@gmail.com


Background

I have been interested in mobile development and mobile game development, but has been in the soy sauce status, recently also learning cocos2d-x, and then by the way learn how to transplant the program to iOS platform, however, because I am not familiar with the iOS development environment, porting finally ended in failure, so I am ready to invest in learning iOS development and cocos2d-x in my spare time.

When I first learned about iOS development, I went directly to the visual interface development and found some difficulties, because the basic objective-C syntax was not clear, so I first read the objective-C syntax.

Objective-C is the most important language in Mac software development. Objective-C is a superset of the ansi c language and supports the basic C syntax. Class Design and syntax are mainly based on smalltalk, one of the earliest object-oriented languages.

In objective-C, the header file ends with. h, and the source file ends with. m or. Mm, usually ending with. M. When you need to include a header file in the source file, you can use the standard # include option, but objective-C provides a better way to use the # import option. The # include option is the same as the # import option. The knowledge # import option ensures that the header file is only included once. Therefore, it is best to use the # import option in objective-C.


Ii. Basic Syntax 1> method call

[Object method] (a method without parameters is called. An object is a method of an object)

[Object method: Input] (the input parameter is used to call a method with parameters)

Output = [object methodwithoutput] (an object's method call can return a value. output is the return value)


2> string variables

As a superset of C, objective-C supports the conventions related to strings in C: a single character is enclosed by single quotes, and a string is enclosed by double quotes. However, in objective-C, strings in the C language are generally not used, because objective-C provides an nsstring class that better processes strings. Nsstring contains your expected advantages, including saving strings of any length and supporting printf-style formatting tools...

Nsstring is frequently used in objective-C. Objective-C provides a enable to easily create nsstring objects from constants. To use this mnemonic, you only need to add a @ symbol before the regular double quotation mark string.

Nsstring * mystring = @ "hahaya ";

3> ID type

The ID type means that the variable can point to any type of variable. For example, the following myobject object can point to any type of variable. The ID type is weak.

Id myobject = [nsstring string];

Note: all objects in objective-C are pointer-type. The ID type has been pre-defined as a pointer type, so no asterisks need to be added.


4> nested method call

The nested methods and functions in C/C ++ are used as follows. The return value of function2 is passed into function1 when the input parameter

Function1 (function2 ());

In objective-C, nested methods, functions, and messages are used as follows. The return value of [prefs fromant] is passed into [nsstring stringwithformant: Input] as an input parameter, similar to C/C ++

[Nsstring stringwithformant: [prefs fromant]

Note: Avoid calling more than two nested calls in one line of code. This will cause reduced readability of the Code.


5> class

Like other object-oriented languages, objective-C uses classes to encapsulate data. Objective-C class specifications include two parts: interfaces and implementations. The Interface part includes the class declaration, instance variable definition, and class-related methods. The implementation part includes the actual code of the class method. The following is an example of a class declaration.


(1) class declaration always starts with the @ interface compilation option and ends with the @ end compilation option.

(2) After the class name, the colon separates the name of the parent class.

(3) instance (member) variables of the class are declared in the code block included in braces.

(4) The instance variable block is followed by the declaration list of class methods (of course, attributes may also exist)

Note: although the previous class only declares methods, the class can declare attributes.


6> Method

Classes in objective-C can declare two types of Methods: instance methods and class methods. Before calling the instance method, you must first create an instance of the class and then call the instance method through the instance of the class. The class method belongs to the entire class and is called through the class without creating an instance. (Similar to the member method and static member method in C ++)

We have already explained the methods without input parameters and single input parameters. The following describes the methods for multiple input parameters. The method declaration includes the method type identifier, return value, one or more method names, parameter types, and parameter names. For details, see the description.


(1) The Declaration starts with "-". This is a method type identifier, indicating that this method is an instance method.

(2) The actual method name is the cascade of all method names, including the colon (insertobject: atindex :)

In the header file, we can define multiple input parameters as follows:

-(Bool) writetofile: (nsstring *) path atomically: (bool) useauxiliaryfile;

Writetofile and atomically are method names, and path and useauxiliaryfile are input parameters.

I already know how to declare and define a method with multiple input parameters. How can I call this method when using it? The call method is as follows:

Bool result = [mydata writetofile: @ "/tmp/log. Text": atomically: No];

Note: No matter when square brackets are seen, messages are sent to an object or a class.


7> attributes

Attribute is a shortcut used to replace the declared access method (get and set methods. The property does not create a new instance variable (member variable) in the class. The property is just a stenographer that defines the instance variable (member variable) in the class of the method example. Classes that expose instance variables (member variables) can use Attribute marks instead of get and set methods.

Because attributes allow classes not to write get and set methods of instance variables (that is, member variables), attributes allow you to write less code. The property avoids exposing the get and set methods of the instance variables (member variables) in the class. Instead, it uses the property declaration to specify the behavior you want, then, combine the get and set methods based on the attribute declaration during the declaration.

Attribute declaration is placed in the class interface declaration (that is, the method declaration. The basic attribute Declaration uses the @ property compilation option, followed by the type information and attribute name. Of course, you can also customize compilation options to configure attributes. These compilation options determine the behavior of the access method. See the following example.

@ Property bool flag;

@ Property (readonly) uiview * rootview; (read only, create only get method)

Another advantage of using properties is to use the dot syntax to access them in code. See the following example.

Myobject. Flag = yes;

Cgrect viewframe = myobject. rootview. frame;

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.