IOS Study Notes-Implementation of variable attributes
1,CodeNote:
Person. h
Person. h
# Import <Foundation/Foundation. h> @ Interface Person: nsobject { Int Age, Sex; // Variable definition Int Height, width;} @ Property Int Age, Sex; // Attribute Definition @ Property Char Height; // -(Void) setage; -( Int ) Setage1 :( Int ); -( Int ) Setwh :( Int ) W :( Int ) H; /* Method Definition Format-(returned data type) method name: (Data Type of parameter 1) parameter 1 Name: (Data Type of parameter 2) parameter 2 Name */ @ End
Person. m
Person. m
# Import " Person. h " @ Implementation Person @ Synthesize Age, Sex; // Accessors // @ Synthesize height; /* [My annotation] @ synthesize reference @ Property Association @ interface cannot be referenced, or cannot be associated, an error is thrown. */ # Pragma Mark ------ setage ---- // -(Void) setage; // { // Age = 20; // } # Pragma Mark ------ setage1 -------( Int ) Setage1 :( Int ) A {age = A; Return Age ;} # Pragma Mark ------ setwh -------( Int ) Setwh :( Int ) W :( Int ) H // Method implementation {Width = 100 ; Height = 175 ; Return Age * Height ;} @ End
Main. m
Main. m
# Import <Foundation/Foundation. h> # Import " Person. h " Int Main ( Int Argc, Const Char *Argv []) {@ autoreleasepool {person * Person = [Person alloc]; [person init]; person. Age = 1 ; // Attribute Nslog ( @" Person. Ag = % I " , Person. Age ); // Output attribute. Pay attention to type matching; otherwise, an error is thrown. Nslog ( @" Person = % @ " , Person ); // Output object [Person setwh: 6 : 10 ]; // Method [Person release]; // If the arc mechanism is used, release cannot be used. } Return 0 ;}
2. My annotations (For details, see the following three figures.):
@ Synthesize reference @ Property Association @ Interface
If no reference or association is found, an error is thrown.