Objective-c syntax (2), objective-c syntax

Source: Internet
Author: User

Objective-c syntax (2), objective-c syntax

Common Errors in the description and definition of oc classes

1. There is only a class declaration and no class implementation.

2. @ end missing

3. @ interface and @ implementation are nested, that is, @ interface or @ implementation and @ end must appear in pairs! The two cannot be nested.

4. member variables are not written in brackets

5. The method declaration is written in the braces of the statement, or the implementation of the method is written in the statement. This is not true.

6. Forgot: NSObject

7. If you want to directly modify the class member variable, but it is not set to @ public

8. instance variables in the class declaration cannot be initialized during the declaration.

9. member variables cannot be used as C language variables, such as static. No! Only the member variable names of the class can be written in the declaration of the class.

10. Remember: methods are methods, functions are functions, and methods such as static cannot be added. No such thing

11. member variables cannot be initialized in {} and cannot be accessed directly by default.

12. methods cannot be called or declared as functions. Object methods can only be called as objects.

13. The implementation of the class can be written after the main function, as long as the class declaration is followed.

14. Remember: The object method name in oc contains a colon. oc does not allow the same method name.-Or + is not the method name.

Differences between OC methods and functions

1. The OC method can only be declared between @ interface and @ end, and can only be implemented between @ implementation and @ end. That is to say, the OC method cannot exist independently of the class.

2. C functions do not belong to the class and are not associated with the class. C functions are only owned by the file defining the function.

3. C functions cannot access members of OC objects.

4. Low-level errors: methods are declared, but functions are written during implementation.

5. oc functions can be written in any position of the file (except between @ interface and @ end, which can be written in the implementation of classes). All functions belong to the file.

OC usage instructions

1. methods are declared and not implemented (this is a classic error)

2. methods are not declared and only implemented (compiler warning, but can be called, weak OC syntax)

3. During Compilation: an error is reported directly when no member variables are accessed. methods that are not accessed are only warnings and weak syntax.

4. A class can be successfully defined without @ interface and only @ implementation

@ Implementation Car: NSObject {@ public int wheels; // wheel int speed; // speed} // you do not need to declare the method, write the definition directly in the class implementation.-(void) run {NSLog (@ "% I wheel, % I speed car ran up", wheels, speed );} @ end

Class involves declaring gender, time, etc.

If a boolean type is used to define the gender, it is not very good, and it is easy for outsiders to confuse it, which is not conducive to the team's code sharing. If the attribute has only a few fixed values, it is best to use enumeration.

Do not write the statement for the year, month, and day. It's silly and tiring,

  //int year;    //int month;    //int day;

Use struct

Date birthday; // birthday structure double weight; // weight Color favourColor; // favorite Color Enumeration
Sex sex; // gender Enumeration

The program code is as follows:

1/* design a Student Category 2 class name: Student 3 member variable: name, gender, birthday, weight, favorite color, each student raises a dog (the dog is also an object) 4 Methods: eat, exercise, walk the dog (let the dog run), feed the dog (let the dog eat) 5 */6 # import <Foundation/Foundation. h> 7 typedef enum 8 {9 // standard syntax. The enumerated constant name contains the enumerated type name, which is clear at a glance and easy to read. 10 SexMan, // 0 11 SexWoman // 1 12} Sex; 13 14 typedef struct {15 int year; 16 int month; 17 int day; 18} Date; 19 20 typedef enum {21 ColorBlack, // 0 22 ColorWright, // 1 23 ColorRed // 2 24} Color; 25 26 // Dog class declaration 27 // Member variable: weight, fur 28 // method: Eat, run 29 @ interface Dog: NSObject 30 {31 @ public 32 double hairColor; // fur 33 double weight; // weight 34} 35-(void) eat; 36-(void) run; 37 @ end 38 // Dog class implementation 39 @ implementation Dog 40-(void) eat 41 {42 43 weight + = 1; 44 NSLog (@ "Dog feed, weight % f", weight); 45} 46-(void) run 47 {48 weight-= 1; 49 NSLog (@ "weight % f", weight ); 50} 51 @ end 52 // Student declaration 53 @ interface Student: NSObje Ct 54 {55 @ public 56 char * name; // name 57 // If a boolean type is used to define a gender, it is not very good and is easy for outsiders to confuse, which is not conducive to team code sharing, BOOL sex; either male or female, YES or NO 58 // If the attribute has only a few fixed values, it is best to enumerate 59 Sex; // gender enumeration 60 // do not write it like this. It is silly and tiring. Use the struct 61 // int year; 62 // int month; 63 // int day; 64 Date birthday; // birthday structure 65 double weight; // weight 66 Color favourColor; // The favorite Color enumeration 67 // each student raises a dog and must use a pointer, all oc objects are operated by pointers! 68 Dog * dog; 69} 70 // in the class declaration, any data type can be declared. The object method declaration is not in curly brackets, however, 71-(void) eat; 72-(void) exercise; 73-(void) print; // print all information 74-(void) within the scope of the class declaration) define thedog; // walk the dog 75-(void) feedTheDog; // feed the dog 76 @ end 77 // The class has not been completed yet. If there is no class implementation, compilation is okay, but the link reports an error. Must have class implementation 78 @ implementation Student 79-(void) eat 80 {81 82 weight + = 1; // each meal, weight Increase by NSLog (@ "after dinner, weight is % f", weight); 84} 85-(void) exercise 86 {87 weight-= 1; // each exercise, weight loss: (@ "% f", weight); 89} 90-(void) print 91 {92 NSLog (@ "Gender = % d, favorite color = % d, name = % s, birthday = % d-% d ", sex, favourColor, name, birthday. year, birthday. month, birthday. day); 93} 94-(void) invoke thedog 95 {96 // call the run method of a dog-type object, which is an embodiment of Object-oriented Thinking 97 [dog run]; 98 99} 100-(void) feedTheDog101 {102 [dog eat]; 103} 104 @ end105 106 int main () 107 {108 Student * student = [Student new]; 109 student-> weight = 50; // weight 110 student-> sex = SexMan; // gender 111 // The following statements are incorrect, the initialization is valid only when the struct variable is defined. Because the struct variable already exists (in the class Declaration), the following method cannot be used to initialize it again, otherwise, the error 112 // student-> birthday = {1990, 11, 11} is reported; // The birthday 113 is 114. // you can use the following method to redefine the new variable of the birthday structure, initialize at the same time, and assign the value to birthday115 // Date date = {1990, 11, 11}; 116 // student-> birthday = date; 117 118 // or use the following method, use a pointer to access the struct variable birthday, and then use the struct variable to access the struct members in it. assign a value of 119 student-> birthday. year = 1990; 120 student-> birthday. month = 5; 121 student-> birthday. day = 28; 122 123 // favorite color 124 student-> favourColor = ColorBlack; 125 // name 126 student-> name = "dashuai "; 127 // pointer members inside the student class point to an object of the Dog class (that is, a Dog) 128 Dog * d = [Dog new]; // create an object for the dog class, and point it to 129 d-> hairColor = ColorWright; 130 d-> weight = 20; // access the dog class member 131 student-> dog = d; // assign the dog Class Object to the pointer of the dog class in the student class, students can operate on dogs by 132 [student into thedog]; 133 [student feedTheDog]; 134 [student print]; 135 136 // [student eat]; 137 // [student eat]; 138 // [student exercise]; 139 // [student exercise]; 140 return 0; 141}

Disable and enable the code prompt function of Xcode

Again, the methods, method types, return value types, and method parameters in oc

 

Develop IOS to a certain extent and get used to reading Apple's official documentation

 

Data Types in oc

Object-c provides basic data types: int, float, double, and char.

 

Int:

Octal, the first integer is 0, and the NSLog format is % o. The displayed octal contains no leading 0.

Hexadecimal, an integer starting with 0x. The format character of NSLog is: if the hexadecimal format displayed (% X or % # X) is in uppercase

% # Octal Band leading 0 displayed by o

% X displays hexadecimal without leading 0x

% # X displays the hexadecimal leading 0x

 

 

Float:
NSLog format: % f
NSLog format OPERATOR: % e scientific notation display value
NSLog format: % e is used if the value of the % g index is smaller than-4 or greater than 5; otherwise, % f is used.

The hexadecimal floating point constant contains the leading 0x or 0X followed by one or more decimal or hexadecimal numbers followed by p or P. Finally, it is a signed Binary Index. For example, 0x0. 3p10 indicates 3/16*2 ^ 10.

Note: Unless otherwise specified, Object-c treats all floating point constants as double values. To display double values, use the same format character as float.

 

Char:

NSLog format: % c

Long double constants are written as floating point constants with letters l or L at the end. 1.234e + 7L


Note: The id type can be converted to a specific object by using a type conversion character.

_ Bool; process Boolean (0 or 1)

_ Complex; process plural numbers
_ Imaginary; process abstract numbers

 

Keyboard input:

int number;scanf("%i",&number); 

 

 

The default instance variable initialization value is 0.

 

Instance variable scope commands:

@ Protected; instance variables can be directly accessed by the class and methods defined in any subclass (default ).

@ Private; instance variables can be directly accessed by methods defined in this class, and cannot be accessed directly by methods defined in the quilt class.

@ Public; instance variables can be accessed directly by the methods defined in the class, or by methods defined in other classes or modules. So that other methods or functions can access instance variables through (->) (not recommended ).

@ Package; for a 64-bit image, you can access this instance variable wherever the image of this class is implemented.

 

 

Defining static variables in a class is the same as that in C.

The voaltile specifier is the opposite of the const, clearly telling the compiler that the value of the specified type variable will change. (I/O port)

For example, store the output port address in The outPort variable.

volatile char *outPort;*outPort = 'O';*outPort = 'N';

This prevents the compiler from deleting the first value assignment statement from the program.

The conversion of enumerated data types, typedef syntax, and data types is the same as that of C.

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.