OBJECTIVE-C Study Notes
1.cocoa object initialization generally uses ALLOC and init two methods, not new, where alloc is used to allocate memory, Init is used for initialization, because the initialization method returns objects that may be different from the assigned object, as follows
[[Class name alloc] init]
2. Initialization method init is typically written with the Super keyword, because the parent class must be initialized before the object is initialized, and the initialization of the parent class is checked for success, as follows
if (self = [super init]) {dosomething}//This sentence indicates that the return is not nil and can continue
else return self;//returns nil, not directly.
Many classes of 3.MAC contain convenience initialization functions, remember to call these initialization methods when you alloc
[[Class name Alloc] Convenient initialization method]
Attributes (@property) are introduced in 4.oc2.0, simplifying the amount of code written (sensing an automatic access method similar to C #)
5. Defining an interface using attributes
@interface
{
type1 var;
Type2 var2;
}
@property type1 var;
@property type2 var2;
@end
6. Variables defined with attributes can be accessed through an automatically established access method within the compiler.
7. Before xcode4.5, after using the @property definition, in the corresponding implementation file @implementation to use @synthesize to declare the corresponding variable, 4.5 since then do not need
8. If a variable can be accessed in a subclass, declare @property in @interface and define the variable, if you do not want the subclass access, define the @property in @interface, do not define the variable, define the variable in @implementation, As follows
@implementation
{
variable definition
}
@synthesize variables
@end
9. The point expression is actually called the property access method in the compiler, rather than directly accessing the property, the point expression appears to the left of the equal sign, or to the right, the read
10. The @synthesize directive allows you to define aliases for variables that use @property, such as externally exported name1, where the actual variable is name2, then @property declares name1, defined in @synthesize @synthesize name1 = name2
11. You can define the read and write permissions for a variable, declare it using the @porperty definition, for example
@porperty (ReadWrite) type Var
@porperty (readonly) type Var
12. When using @porperty to declare properties but do not want to use the system automatically generated by the method, using @dynamic can block the system automatically generated property methods, to define their own
13. You can also rename the access method using @porperty
14. The way to add a new method to an existing class is called a category, defined by
@interface Source class name (category name)
New method name to add
@end
15. Categories can only add new methods, cannot add new variables, and the category has the hidden danger of name conflict
16. class extensions, class extensions are a special form of a class that can only be placed in the relevant file of a class,. h. m, which can extend the class's variables and also modify the access properties of the class, but the class extension is not a name, that is, the class extension can only be used internally, which is a convenient way to maintain the code
17. Categories can implement decentralized management of the source code, access to the instance variables of their inherited classes
18.cocoa does not have a true private method, even if the object does not declare the method, as long as the implementation can be called
19. A delegate is an object that is requested by another class to perform some work, such as having a service class that has a delegate object A, and the service class asks if object A is going to do some work
20. The delegate object only needs to implement the method intended to be invoked
21. The method that is sent to the delegate object can be declared as a category of NSObject
22. Response Selector (not clear)
23. A formal agreement is a list of names that contain methods and attributes that must be explicitly called
24. The agreement is made by listing the name of the agreement in the interface of the class and following the agreement, the class is subject to the agreement, and the adoption of the Agreement implies the commitment to implement all the methods of the Agreement
25. The Agreement statement uses the @protocol keyword, as follows
@protocol Agreement Name
Method definition
@end
26. The agreement may be inherited, as follows
@protocol Agreement name < parent agreement name >
Method definition
@end
27. When the inherited protocol is used by the class, the class must implement all the methods of the parent and sub-protocols, and the NSObject class conforms to the NSObject protocol, so the protocol can or should best be inherited NSObject protocol
28. Use the <> in the class with the agreement, as follows
@interface Class Name: Parent class name < protocol 1, Protocol 2>
@end
29.objective-c object replication is divided into shallow copy and deep copy, where shallow copy only copies pointers, deep copy copy content
30. You can specify the protocol name in the data type and method parameters used, if the data type, then the class of the data should conform to the specified protocol, if the method return value, then the return value of the class should also conform to the specified protocol as follows
-void function: (id< protocol name >) var;
31.objectivec2.0 added two new features, @option and @required, mainly used in the definition of the protocol, using @optional defined methods, classes can be used when using the Protocol optional implementation, using @required method, Class must be implemented when using the protocol
32. A delegate refers to a class that contains a delegate object that must implement a specified protocol that constrains the behavior of the delegate object by the protocol.
OBJECTIVE-C Study notes 2