Objective-C Knowledge Summary (1), objective-c Knowledge Summary

Source: Internet
Author: User

Objective-C Knowledge Summary (1), objective-c Knowledge Summary

Objective-C (OC) is a language for developing Mac OS X and iOS applications. Currently, the latest swift language can also be used to develop applications on the above two platforms.

OC is the language of the target object. (For more information about the features of the target language, see the previous blog. ---> click me! <---) The OC code can be mixed into the C language code, even the C ++ code, or mixed with swift.

Okay, that's all about introduction >_<

Here is a small question:Why does OC use import to introduce header files? What is the difference between import and include?

Both include and import introduce header files.

Import header file to prevent repeated inclusion

Include: it uses pre-processing commands to prevent repeated inclusion. If no pre-processing commands are written, the problem of Repeated inclusion cannot be prevented.

Remember how to use preprocessing commands to prevent repeated inclusion

# Ifndef Zhy # define Zhy // function declaration void test (); # endif

Use NSLog to output built-in line breaks

NSLog(@"Hello World!");

In OC, a bool type is added to C to save the logical value. YES (true 1) NO (false 0)

Exception capture mechanism in OC

@ Try {// Code that may cause errors <# Code that can potentially throw an exception #>}@ catch (NSException * exception) {// error handling method <# Handle an exception thrown in the @ try block #> NSLog (@ "% @", exception ); // print the error message} @ finally {// execute whatever the error is. <# Code that gets executed whether or not an exception is thrown #>}

Class creation in OC and where to define member variables, class methods and object Methods declaration and implementation

// Declaration @ interface Person: NSObject {// member variable NSString * _ name; int _ age;} // declaration of object method-(void) duixiangfangfa; // declaration of class methods + (void) leifangfa; @ end // implementation of the class @ implementation Person // implementation of the Object method-(void) duixiangfangfa {NSLog (@ "this is the implementation of object methods");} // Implementation of class methods + (void) leifangfa {NSLog (@ "this is the implementation of class methods ");} @ end

Statements for creating objects

Person *p = [Person new];

[Person new] has done three things

Small problem: in which area of the memory is the applied space?

When new, the requested space is in the heap area of the memory (the memory space dynamically allocated by the Program)

Instance variables are saved in the heap area, pointer p is saved in the stack area, and object methods are saved in the code area.

The heap area has a _ isa pointer pointing to the code area. When an object wants to call a method, first find the space in the heap area corresponding to p and then find the _ isa pointer, find the space in the code area pointed to by _ isa, and then find the method in the space.

Design a "student" Class

1> member variables

* Name

* Birthday

1 # import <Foundation/Foundation. h> 2 // Date struct 3 typedef struct {4 5 // year 6 int year; 7 // month 8 int month; 9 // day 10 int day; 11 12} MyDate; 13 14 // Student class Declaration 15 @ interface Student: NSObject16 {17 @ public18 // Student name 19 NSString * _ name; 20 21 // MyDate _ birthday; // struct variable 23} 24 25 @ end26 27 28 // Student class implementation 29 @ implementation Student30 31 @ end32 33 34 int main (int argc, const char * argv []) {35 @ autoreleasepool {36 37 Student * stu = [Student new]; 38 stu-> _ name = @ ""; 39 40 // Method 41 stu-> _ birthday = (MyDate) {1983,12, 12}; 42 // NSLog (@ "% d, % d, % d ", stu-> _ birthday. year, stu-> _ birthday. month, stu-> _ birthday. day); 43 44 // Method 2: Define a struct variable 45 MyDate d1 = {1981,11, 11 }; // initialize 46 stu-> _ birthday = d1; 47 48 at the same time while defining the struct variable. // The third method is to assign 49 stu-> _ birthday values one by one. year = 2014; 50 stu-> _ birthday. month = 12; 51 stu-> _ birthday. day = 11; 52 53 NSLog (@ "% d, % d, % d", stu-> _ birthday. year, stu-> _ birthday. month, stu-> _ birthday. day); 54} 55 return 0; 56}

The method used to call the struct is described here.

NSString usage

NSString is a string processing class in OC

NSString *s = @"zzzzzzzzzzzzzz "; NSString *s1 = [NSString stringWithFormat:@"%d",i];

Length of an NSString

NSString * s1 = @ "zzz"; NSUInteger len = [s1 length]; // 3 // Chinese characters also occupy 1 byte in NSString

 

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.