Using the member variable {} Within a class, use the attribute @property outside the class
/***********---Person.h */@interface person:nsobject {nsstring *_name;} @property (nonatomic, copy) NSString *sex; @property (nonatomic, assign) int age;-(void) Getpropertyandivar; @end/******* ---person.m */@implementation person-(void) Getpropertyandivar {unsigned int count = 0; objc_property_t *propertylist = Class_copypropertylist ([self class], &count); for (int i = 0; i < count; i++) {const char *propertyname = Property_getname (Propertylist[i]); NSString *name = [NSString stringwithutf8string:propertyname]; NSLog (@ "%@", name); } NSLog (@ "-------------------Split line------------------"); Ivar *ivarlist = Class_copyivarlist ([self class], &count); for (int i=0; i<count; i++) {const char *ivarname = Ivar_getname (Ivarlist[i]); NSString *name = [NSString stringwithutf8string:ivarname]; NSLog (@ "%@", name); }}/***********---Log */2016-07-01 15:53:00.449 demo[24420:690011] sex2016-07-01 15:53:00.449 demo[24420:690011] age2016-07-01 15:53:00.449 demo[24420:690011]------------------- Split Line------------------2016-07-01 15:53:00.449 demo[24420:690011] _name2016-07-01 15:53:00.450 demo[24420:690011] _ age2016-07-01 15:53:00.450 demo[24420:690011] _sex
Based on the above code and output, mainly around the OC class variables, class accessor methods, variables related to the keyword, memory management, the following is a summary of the relevant knowledge points:
First, attributes and member/instance variables
1, what are attributes and member variables?
Property: An element with a property of "@property" decoration
Member Variable: member variable is an element inside "{}"
2, the essence of OC method invocation?
The essence of the method call "[]" is to send a message to the object
3, dot syntax (dot syntax)?
The point in OC "." is a method call that sends a message to an object, different from the Java "point" to access the property
4, LLVM changes after the compiler?
@property, @synthesize automatically generate member variables and methods
5, an idea of object-oriented programming?
Cohesion Poly, low coupling. Encapsulation becomes the object of a function, only to expose the interface and property list of corresponding function to the outside world, the implementation of function-independent properties and methods should be privatized as much as possible.
Second, @property
1, the role of @property
A. Auto-generate member variable XXX
B. Automatic declaration of setter and getter methods, following the semantics of memory management
2, accessor method and point syntax
a. setter method : method name must be set start, The return value is VOID&NBSP;&NBSP;
B. Getter method: Method name is variable name, no get start, no arguments, return value
C. Accessor methods of the class commonly used point syntax to invoke, the assignment is called the setter, when the value is called getter
D. Any method that conforms to the characteristics of a tired accessor method can be manipulated using dot syntax
3, @property property modifier
A. Read- write modifier: readwrite | ReadOnly
B. Assignment- related modifiers: assign | Retain | Copy | Strong | Weak
c. atomic modifier: Atomic | Nonatomic
D. getter and Setter modifiers: getter | Setter
The 4-class fix for @property property
4, @property in the category
using @property in categories/categories only declares that methods do not declare properties, and you can use the OC runtime to create
Third, @synthesize
1, the role of @synthesize
A. Automatically specify attributes and member variables corresponding to the @synthesize age=_age, Sex=_sex;
B. setter and Getter methods that implement @property declarations automatically , also follow the semantics of memory management
2, Xcode 4.3, new clang compiler/LLVM compiler
A. clang will be added by default.@synthesize xxx=_xxx,建立属性和成员变量的关系
B. You can also manually add @synthesize xxx=_bbb, which is more flexible and does not have to be the same on both sides of the underscore
3, similarities and differences of @dynamic and @synthesize
A. Telling the compiler to generate Getter/setter methods during compilation by using the @synthesize directive
B. Through the @dynamic instruction, tells the compiler that the property setter and getter method is implemented by the user itself, not automatically generated, if not automatically generated at runtime call will crash
C. Some of the accesses are created dynamically at run time, such as those used by the Nsmanagedobject class in CoreData. If you want to declare and use attributes in these cases, but to avoid missing methods at compile-time warnings, you can use the @dynamic dynamic instruction instead of the @synthesize composition directive
Iv. Specifications
The private member variables of a class are created in many ways, and there are several ways to access variables within a class. Do not violate the principle of memory management, reduce hidden bugs, take the road of code cleanliness, and sort out the following information
References (stamp here):
> member variables and properties of classes in Objective-c
> categories, attributes, member variables, ARC learning Summary
> Recruitment on a reliable iOS
> Ios-runtime Knowledge Point Finishing
> The controversy in iOS development (i)
Property and member variable (IVAR)