2013年8月29日星期四

ios study notes (two) of the Objective

 

two: Objective-C classes and inheritance, and protocols

 

already mentioned in the initialization of an object, where the first talking about variables.

 

2.1 Variables

 
      
  1. local variables (internal variables): local variables within the method as defined in the description, and its scope is limited to the method, the method used after leaving this variable is illegal.
  2.   
  3. global variables (external variables): its scope is the entire source code. Specifier extern. For example:

  4.  
 

extern int intX;

 If the same source file

global variables and local variables with the same name, in the scope of local variables, global variables do not work.

 

3. instance variables: In the instance variables defined in the class that can be used within each method.

 

4. static variables (static): it is a method similar to global variables defined within a scope of the current class for all objects. Save memory, a static variable in memory, shared by all objects can be updated, the updated, all object access the updated value.

 

5. auto: This is an automatic local variable declaration is a method of internal variables are declared, is the default setting, generally omit it.

 

For example: auto int intX with int intX equivalent.

 

6. const: immutable variables.

 

7. volatile: and const Instead, the variable's value will change.

 

2.2 @ property and @ synthesize

 

in the interface file (. h), use @ property to identify the attributes (instance variables); in the implementation file (. m), use the @ synthesize statement to identify the attributes of the system Set method for automatic generation methods (getter / setter).

 

declare property syntax is: @ property (parameter 1, parameter 2) type name;

 

where the parameters are divided into three categories: read-write properties: (readwrite / readonly) setter semantics: (assign / retain / copy) Atomicity: (atomicity / nonatomic)

 

meaning of each parameter is as follows:

 

readwrite: generate setter \ getter method.

 

readonly: only generate a simple getter, no setter.

 

assign: default type, setter method of direct assignment, without retain operation.

 

retain: setter method parameters release the old value, and then retain the new value.

 

copy: setter method Copy operation, and retain the same.

 

nonatomic: non-atomic access . Without this keyword, will self as a mutex, thus ensuring atomicity.

 

"." can only operate in the setters and getters.

 

For example: In the interface file: @ property int count;

 

equivalent to declare two ways:

 
  
   - (int)count;    
- (void)setCount:(int)newCount;
 
 

implementation file (. m) in: @ synthesize count;

 

equivalent to the implementation file (. m) to implement two methods - the default assign:

 
  
  - (int)count     {    
return count;
}
-(void)setCount:(int)newCount {
count
= newCount;
}
 
 

2.3 Exception Handling

 
      
  1. @ try: code that might throw an exception block uses.
  2.   
  3. @ catch: @ chunk used to capture errors thrown, you can use multiple @ catch block.
  4.   
  5. @ finish: either case a block of statements to be executed.
  6.   
  7. @ throw: their own throw an error.
  8.  
 

2.4 based framework class

 
      
  1. NSString: string type, font and size did not carry information, can not change the contents of a string NSString objects, but there can be many ways to generate another string consists of a string. If you want to change the string itself, need to use NSString subclass NSMutableString.
  2.   
  3. NSDate: represents the time and date.
  4.   
  5. NSNumber: contains a numeric value (including bool) objects. NSNumber itself is not a number, it is not used in arithmetic expressions.
  6.   
  7. NSData: sequence of bytes, and can not be modified. Would need to be modified to use NSData subclass NSMutableData. NSData is mainly used for downloading data from the Internet (to NSData object return data); the object stored in the file; reads file data (NSData object as a buffer zone will).
  8.   
  9. NSSet: unordered collection of different objects, immutable, can be added or deleted by calling the method to get a collection of new NSSet object. Its subclasses are NSMutableSet.
  10.   
  11. NSDictionary: its unordered collection of key-value pairs, keys are usually NSString, the value can be any one object, NSDictionary is a fixed length, it becomes firstborn class is NSMutableDictionary.
  12.   
  13. property list
  14.   
  15. NSObject: Each class is inherited from NSObject, which uses NSObject protocol that achieved with the NSCopying, NSMutableCopying and NSCoding agreements related methods, detailed analysis see 2.5.3 categories, part of the agreement with the 2.5.4 source NSObject.h.
  16.  
 

2.5 Inheritance and protocols

 

2.5.1object-c inherit the base

 
      
  1. object-c does not support multiple inheritance, only single inheritance, a class can have only one parent.
  2.   
  3. method overrides: If the subclass methods and a method of the parent class has the same method name, return type and parameter list, the new method overwrite the original method.
  4.   
  5. method overloading: class, you can create multiple methods, they have the same method name, but with different parameters and different definitions, the method is called by passing them different numbers and types of parameters to be used sleep which method. Necessarily the same method name; method parameter table must be different, including the number and types of parameters, in order to distinguish different method body; method's return type and modifiers may be the same or different.
  6.   
  7. self: a class with a class method calls another method is to use the self, on behalf of itself, the equivalent of this.
  8.   
  9. super: represents the parent class, you can access the parent class using the super class quilt hidden or overridden method.
  10.  
 

2.5.2 Access Control

 
      
  1. @ protected-Methods defined in the class and any subclasses can directly access the instance variables that follow.This is the default case. class and any subclass methods Such direct access to the variable, which is the default.
  2.   
  3. @ private-Methods defined in the class can directly access the instance variables that follow, but subclasses cannot. methods in this class can access such a variable, but not the methods defined subclasses direct access.
  4.   
  5. @ public-Methods defined in the class and any other classes or modules can di-rectly access the instance variables that follow. besides himself and subclass methods can also be used by other classes or methods in other modules accessed. Openness biggest, but best to avoid using this scope. Other classes should use getter / setter methods to access or set other class instance variables to ensure encapsulation.
  6.  
 

2.5.3 Category

 

types of effects: the expansion of existing classes, without affecting the original kind of situation, Object-C provides a category dynamic run-time distribution, add new methods for the class. Implementation of the class will be distributed to many different file or several different frameworks.

 

categories of characteristics: First: category Unlike subclasses can not define new instance variables. Second: the name conflict, or class methods in the same name with the existing methods. When a name conflict occurs when the class has a higher priority. For example:

 
  
@interface NSObject (NSCoderMethods) //表示类别的名称为NSCoderMethods,要扩展的对象为NSObject 
+ (NSInteger)version;
+ (void)setVersion:(NSInteger)aVersion;
- (Class)classForCoder;
- (id)replacementObjectForCoder:(NSCoder *)aCoder;
- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder;
@end
 
 

class standard syntax is as follows:

 
  
#import "类名.h" 
@interface 类名 (类别名)//新方法的声明
@end

#import "类名类别名.h"
@implementation 类名 (类别名) //新方法的实现
@end
 
 

 

2.5.4 Protocol

 

protocol functions similar to c + + abstract base class in multiple inheritance. Protocol is a list of multiple classes shared methods. Protocol methods listed in this class and no corresponding implementation, but by other classes to implement these methods. If a class to comply with an agreement, the class must implement all the methods of a particular protocol (except optional method).

 

defines a protocol requires the use of @ protocol directive, followed by the protocol name, and then some of the ways is to declare, before the command @ end statement of all methods are part of the agreement.

 

agreements can have a static function.

 

@ optional means that you can not choose method.

 

@ required vowed to implementation.

 

while using multiple protocols only need <> list multiple protocols within and with "." separated:

 

@ interface Car: NSObject .

 

following NSObject.h: Each class is inherited from NSObject, which uses NSObject protocol that achieved with NSCopying, NSMutableCopying NSCoding protocol and related methods.

 
  
/*  NSObject.h */   
#import <Foundation/NSObjCRuntime.h>
#import <Foundation/NSZone.h>

@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
@class Protocol;

/*************** Basic protocols ***************/

@protocol NSObject //NSObject协议
//@protocol是声明协议的标志。后面的NSObject是协议的名称
//协议中的方法是每个采用者必须实现的方法,这里有19个方法
- (BOOL)isEqual:(id)object; // 用于对象比较
- (NSUInteger)hash;

- (Class)superclass;
- (Class)class;
- (id)self;
- (NSZone *)zone;

- (id)performSelector:(SEL)aSelector;
- (id)performSelector:(SEL)aSelector withObject:(id)object;
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
/* 延迟调用参数提供的方法,参数所需参数用withObject传入(类似于ActionScript3.0中的setTimeout函数) delay单位:秒 */
- (BOOL)isProxy;

- (BOOL)isKindOfClass:(Class)aClass; /* 用于判断对象是不是参数提供的类型 */
- (BOOL)isMemberOfClass:(Class)aClass; /* 用于判断对象是不是参数提供的类型*/
- (BOOL)conformsToProtocol:(Protocol *)aProtocol; /* 用于判断对象是否遵守了参数提供的协议*/

- (BOOL)respondsToSelector:(SEL)aSelector; /* 用于判断对象是否拥有参数提供的方法*/

- (id)retain; //增加对象的计数器
- (oneway void)release; //减少对象的计数器
- (id)autorelease; //自动减少对象的计数器,但是以推迟的方式来实现
- (NSUInteger)retainCount; //返回一个对象当前的计数器

- (NSString *)description; //允许一个对象返回一个字符串来描述它的内容;这个常用于调试debugging

@end //表示结束

@protocol NSCopying //NSCopying协议
- (id)copyWithZone:(NSZone *)zone; //协议中的方法是每个采用者必须实现的方法,这里只有一个方法

@end //表示结束

@protocol NSMutableCopying//NSMutableCopying协议
- (id)mutableCopyWithZone:(NSZone *)zone;

@end //表示结束

@protocol NSCoding //NSCoding协议

- (void)encodeWithCoder:(NSCoder *)aCoder;

- (id)initWithCoder:(NSCoder *)aDecoder;

@end

/*********** Base class ***********/

@interface NSObject <NSObject> { //告诉编译器,NSObject正在遵守NSObject协议
Class isa;
}

+ (void)load;

+ (void)initialize;
- (id)init; //对象的属性初始化

+ (id)new;
+ (id)allocWithZone:(NSZone *)zone;
+ (id)alloc;
- (void)dealloc; //用于类来释放对象实例变量并释放动态内存

- (void)finalize AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER;

- (id)copy;
- (id)mutableCopy;

+ (id)copyWithZone:(NSZone *)zone;
+ (id)mutableCopyWithZone:(NSZone *)zone;

+ (Class)superclass;
+ (Class)class;
+ (BOOL)instancesRespondToSelector:(SEL)aSelector;
+ (BOOL)conformsToProtocol:(Protocol *)protocol;
- (IMP)methodForSelector:(SEL)aSelector;
+ (IMP)instanceMethodForSelector:(SEL)aSelector;
- (void)doesNotRecognizeSelector:(SEL)aSelector;

- (id)forwardingTargetForSelector:(SEL)aSelector;
- (void)forwardInvocation:(NSInvocation *)anInvocation;
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;

+ (NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)aSelector;

+ (NSString *)description;

+ (BOOL)isSubclassOfClass:(Class)aClass AVAILABLE_MAC_OS_X_VERSION_10_2_AND_LATER;

+ (BOOL)resolveClassMethod:(SEL)sel AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;
+ (BOOL)resolveInstanceMethod:(SEL)sel AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;

@end

@interface NSObject (NSCoderMethods) //类别 见2.5.3

+ (NSInteger)version;
+ (void)setVersion:(NSInteger)aVersion;
- (Class)classForCoder;
- (id)replacementObjectForCoder:(NSCoder *)aCoder;
- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder;

@end

#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
@interface NSObject (NSDeprecatedMethods)

+ (void)poseAsClass:(Class)aClass DEPRECATED_IN_MAC_OS_X_VERSION_10_5_AND_LATER
#if __OBJC2__
UNAVAILABLE_ATTRIBUTE
#_endif
;

@end
#_endif


#if MAC_OS_X_VERSION_10_6 <= MAC_OS_X_VERSION_MAX_ALLOWED
 

没有评论:

发表评论