Objective-C basic notes (1) Basic concepts and first procedures, concepts first features

Source: Internet
Author: User

Objective-C basic notes (1) Basic concepts and first procedures, concepts first features
I. Basic Concepts

Objective-C (OC) is the core language for iOS development. Apple maintains and works with C and C ++ during the development process. OC is mainly responsible for the UI interface, C language and C ++ can be used for graphic processing. C language is a process-oriented language, and OC adds an object-oriented syntax (removing complex object-oriented syntax) on the basis of C language ). We can use OC to develop Mac OS X and IOS applications.

October 2014 programming language ranking (http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html)


From this ranking list, we can see that c, java, object-c, c ++, c #, php, python, and javascript are the top ones.

Note:

1. Objective-C does not have a package concept, that is, it does not have a namespace mechanism. Instead, it adds a prefix to the class name to prevent class name conflicts.

2. Keywords in Objective-C start with @ to prevent conflicts with keywords in c/c ++.

3. the string in Objective-C starts.

Syntax:


Note the red text above:

BOOL type (YES \ NO) is actually 1 and 0

Nil is equivalent to null in Java

Self is equivalent to this in Java

For (xx in xx) and javascript fast traversal are the same, which is equivalent to for... each in java.

Same as Java, inheritance is a single inheritance.

Ii. First OC Program
Select the command line program
Select the language location Objective-C. The created project is as follows:
//// Main. m // firstOCProj /// Created by lixiaoqiang on 14/11/11. // Copyright (c) 2014 lixiaoqiang. all rights reserved. // # import <Foundation/Foundation. h> int main (int argc, const char * argv []) {@ autoreleasepool {// insert code here... NSLog (@ "Hello, World! ");} Return 0 ;}
Console output:

23:44:36. 052 firstOCProj [1649: 99047] Hello, World!

The above @ autoreleasepool is related to memory management. # Import <Foundation/Foundation. h>: the import and include functions the same. The only difference is that header files can be repeatedly included. NSLog (): outputs logs to the console, which is equivalent to printf () in C language, and Log 3 in android. classes in the basic syntax OC are divided into two files, one is. h file, and the other is. m file (the suffix of the OC file ). h: class declaration file, used to declare member variables and methods .. M: Method declared in. h. Create the Student. h and Student. m files. The project is as follows:
Student. h file
//// Student. h // firstOCProj /// Created by lixiaoqiang on 14/11/11. // Copyright (c) 2014 lixiaoqiang. all rights reserved. //// this file is only used to declare the member variables and methods of the Student class. // it contains the header file where NSObject is located # import <Foundation/Foundation. h> // @ interface @ end indicates declaring a class. // unlike Java, NSObject's default inheritance cannot be omitted in OC. //: Indicates inheritance @ interface Student: NSObject {// define the member variable (must be defined in this bracket) int age ;} // get method of age // if it is a dynamic method-// if it is a static method + (-and + must be added) // The parameter types in OC are enclosed in brackets //. all methods in the hfile are public methods-(int) age; // One of the set Methods of age // OC: corresponds to a parameter-(void) setAge :( int) newAge; @ end
Student. m file
//// Student. m // firstOCProj /// Created by lixiaoqiang on 14/11/11. // Copyright (c) 2014 lixiaoqiang. all rights reserved. //// use double quotation marks for custom header files # import "Student. h "@ implementation Student-(int) getAge {return age; // age in parent class}-(void) setAge :( int) newAge {age = newAge;} @ end
Main. m file
//// Main. m // firstOCProj /// Created by lixiaoqiang on 14/11/11. // Copyright (c) 2014 lixiaoqiang. all rights reserved. // # import <Foundation/Foundation. h> # import "Student. h "int main (int argc, const char * argv []) {@ autoreleasepool {// create a Student object // 1. Call a static method alloc to allocate memory // call the method by class name in OC [class name method name]/[Student alloc] returns a pointer, in OC, stu is called an object. // this is actually the same (reference) as the object (this) in Java. // Student * stu = [Student alloc]; // 2. Call a dynamic method init for initialization // stu = [stu init]; // connect to Student * stu = [[Student alloc] init]; [stu setAge: 100]; NSLog (@ "setAge method called"); int age = [stu age]; NSLog (@ "Student's age is % I ", age); // release the object // [stu release];} return 0 ;}
Running result:

00:25:03. 661 firstOCProj [1707: 110472] called the setAge Method

00:25:03. 663 firstOCProj [1707: 110472] Student's age is 100



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.