Object C

1.减号表示一个函数、或者方法、或者消息的开始
   private void hello(bool ishello){} 相当于 -(void) hello:(BOOL)ishello{}
2.Objective-C里面没有public和private的概念,你可以认为全是public。
3.加号(+)的意思就是其他函数可以直接调用这个类中的这个函数,而不用创建这个类的实例。
4.C#中方法调用:this.hello(true);  ob-c里:[self hello:YES]; 注:ob-c里没有true或false,只有yes或no.
5.#import 你可以把它认为是#include,一样的。但是最好用#import,记住这个就行了。
6.
@interface 定义类的关键字,@implementation 实现类的关键字。
  public class Kids : System{}  相当于 @interface Kids: NSObject{}...@end //以@end结束
  @implementation Kids  //无分号';'
  - (NSString *) description
  {
      return (@"I am Kids's description function.");
  } //description 实现方法
  @end //Kids 也以@end结束
7.方法调用:[[[MyClass alloc] init:[foo bar]] autorelease];  等同于 MyClass.alloc().init(foo.bar()).autorelease(); 
8.同一个数组可以保存不同的对象。  (这个好 像PHP一样灵活)
9.nil表示Objective-C里的NULL(空)就这么写,表示空指针.
10.为什么是@”字符串”而不是”字符串”:
   "字符串"是C的字符串,@""是把C的字符串转成NSString的一个简写.另,@""这个转换是不支持中文的.例如NSLog(@"字符串");
   是一定输出不了中文的.
11.强类型和弱类型
   MyClass *myObject1; // 强类型 
   id myObject2; // 弱类型 
   弱类型‘id’类似js里面的var,无类型区分。实际上'id'本身就是一个声明指针类型的关键字。
12.实例化对象
   id ins = [MyClass new];
13.调用父类方法
   [super 方法名:参数]
14.定义getter和setter规则
   以成员变量 MyClass * myclass 为例:
   getter: -(MyClass *) myclass { return myclass; } // 与成员变量名相同(与C#不同,不加'get')。
   setter: -(void) setMyClass: (MyClass *) newMyClass { myclass = newMyClass; } // 在成员变量名前加'get'(与C#相同,都加'get')。
15.#import <xxx/xxx.h> 导入系统头文件(只读)。#import "xxx.h" 导入项目本地头文件(可编辑)。
16.@class 类名;
   当类A中的成员含有类B,且类B的实例又是指针,那么就可以把#import "B.h"换成@class B;,这样会避免集联关系带来的大量编辑时间。
 
NSString相关
   [NSString length] // 字符串个数。
   [NSString stringWithFormat] // 格式化字符串
   [str1 isEqualToString: str2] // 判断str1和str2是否相同
  [str1 hasPrefix: @"draft"] // str1是否以"draft"开头
  [str1 hasSuffix: @".mov"] // str1是否以".mov"结尾
  // 字符串切分成数组
  NSString *str = @"aa:bb:cc:dd:ee:ff";
  NSArray *arr = [str componentsSeparatedByString:@":"];
  // 数组转换成字符串
  str = [arr componentsJoinedByString:@":"];
 
  initWithFormat写法
  // 不正确
  NSString *sss = @"haha";
  sss = [sss initWithFormat:@"hellow,%@",sss];
  // 正确
  NSString *sss = @"haha";
  NSString *str = [[NSString alloc] initWithFormat:@"hellow,%@",sss];
 
NSArray相关
  两个限制:能存储任意objective-c的对象,
  而不能存储C语言中的类型(如int float enum struct)以及nil(NULL值,因为数组最后一位用它来做结束符)。
  // 循环数组 
  for(i=0;i<[array count];i++)[array objectAtIndex:i];
  // 创建可变数组
  MSMutableArray *array = [MSMutableArray arrayWidthCapacity: 17];
  // 添加数组
  [array addObject: [MyClass new]];
  // 删除某个下标对象
  [array removeObjectAtIndex: 1];
  // 利用枚举遍历数组
  NSEnumerator *em = [arr objectEnumerator]; // objectEnumerator从前往后遍历,reverseObjectEnumerator从后往前遍历
   id = obj;
   while(obj = [em nextObject]){...} // 数组以nil作为结束符,当obj=nil时,循环结束
  // Objective-C 2.0版本中的快速枚举遍历数组方法(类似PHP中的foreach)
  for(NSString *str in array){...}
 
NSDictionary 存储键值对儿对象
  // 初始化(一个值一个键,最后以nil结束)
  NSDictionary *objs = [NSDictionary dictionaryWidthObjectsAndKeys: [MyClass new], @"class1", [MyClass new], @"class2", ..., nil];
  // 引用 如果"class1"不存在 则返回nil
  Myclass *class1 = [objs objectForKey: @"class1"];
  // 添加元素
   NSMutableDictionary *objs = [NSMutableDictionary dictionary];
   [objs setObject: [MyClass new] forKey: @"class1"]; // 若class1已经存在,则新值替换已存在的
   // 删除元素
   [objs removeObjectForKey: @"class1"];
   切记千万不要为NSString、NSArray、NSDictionary创建子类
 
NSNumber
  // 包装基本类型
  + (NSNumber *) numberWithChar: (char)value;
  + (NSNumber *) numberWithInt: (int)value;
  + (NSNumber *) numberWithFloat: (float)value;
  + (NSNumber *) numberWithBool: (BOOL)value;
 // 重新获取
 - (char) charValue;
 - (int) intValue;
 - (float) floatValue;
 - (BOOL) boolValue;
 - (NSString *) stringValue;
 
 NSNull(与nil不同,NSNull可以被放到数组及字典里)
 [objs setObject: [NSNull null] forkey: @"class1"];
 id obj = [objs objectForKey: @"class1"];
 if (obj == [NSNull null]){...}
 

当前日期
 NSDateFormatter *df = [[NSDateFormatter allocinit];
 [df setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
 NSString *strDate = [df stringFromDate:[NSDate date]];

 
 p:134
 
 
 
构造函数
-(类名 *) init{return [super init];}
// new 的时候就会调用构造函数init,若init有参数则不会执行,必须new过之后显示调用
类名 *p = [类名 new]; 
多个参数的方法
个人理解除第一个参数外其它的参数都需要加一个别名,别名随便起,当然在调方法时也要一一对应
// 方法方法实现
-(void) fun1:(int)parm1 and1:(int)parm2 and2:(NSString *)parm3 and3:(NSString *)parm4  {
       //  如果出现 local declaration of xxx hides instance variable 的警告,则说明其中一个参数名跟成员变量重名
       NSLog(@"p1:%d,p2:%d,p3:%@,p4:%@",parm1,parm2,parm3,parm4);
}
// 调方法
[实例名 fun1:10 and1:11 and2:@"param3" and3:@"param4"];
@public @private @protected
用来修饰成员变量,但不能修饰成员方法,不写默认为@protected
// 定义
@public
   int numb;
// 引用
实例->numb; 或类方法内 self->numb;
// 获取所属于类名
[类名/实例名 class];
 
@property,@synthesize
成员变量的getter和setter方法已过时,应用@property和@synthesize替换。
  1.在【.h】文件里用@property + 类型 + 属性名
    @property (readonly'表示只读') int intVal;
  2.在【.m】文件里用@synthesize + 属性名
    @synthesize intVal;
    @synthesize intVal = _intVal; // 表示getter和setter是对成员变量_intVal进行操作。
    // 当一个类里 ‘@property(retain) 类名 *实例;’时,则该类需要重写dealloc方法
    // 当OC对象的retainCount为0的时候,dealloc会被调用
    
- (void) dealloc{self.实例 = nil; [super dealloc];}
应用方法:
  1.
    [实例 setIntVal:100];
    NSLog(@"%d", [实例 intVal]);
  2.
    实例.intVal = 100;
    NSLog(@"%d",实例.intVal);
类别(category)
类别不能添加成员变量;当类别方法与原类方法同名,那么类别方法会取代原类方法;其它部分与类一样
@interface 原类名 (类别名)       @implementation 原类名 (类别名)
// 定义部分                             // 实现部分         
@end                                   @end
 

// 重载(方法重载不是根据方法的参数决定的 而是根据参数的标签决定的)
[a ov1:1];
[a ov1:1 y:2];
[a ov1:1 yy:2];

内存
如果alloc或[muTable]copy或retain了一个对象,那么必须使用relase或autorelase进行释放。
当对象被alloc时,其retainCount属性会加1,relase是会减1,当为0时才会被真正释放。

协议(@protocol)
类似C++里的纯虚函数或java里的接口(OC里只有一个.h文件)
@required 表示必须被实现的协议方法
@optional 表示可以不被实现的协议方法
定义协议
@protocol 协议名 <NSObject>  // <>里为父协议
   协议方法
@end
使用协议
@interface 类名 : NSObject <协议名>

实例化协议
id<类名> 对象 = [[类名 alloc] init];
// 判断某个协议方法是否被实现
if (对象 respondsToSelector:@selector(协议方法名:)) { 已经实现 }

blocks应用
oc里的blocks和C里的函数指针一样,唯一不同是把*换成了^
例1
void (^myblocks) (void) = NULL; // 声明
myblocks = ^(void){NSLog(@"in blocks");}; //赋值一个函数
myblocks();// 调用执行
例2
__block int sum = 0;// 若要在block函数里引用上层变量 需要加上__block声明
int (^myblocks2) (int a, int b) = ^(int a, int b){sum = a+b;return a+b;};
int c = myblocks(1,2);

posted @ 2013-11-26 20:32  关桐  阅读(394)  评论(0)    收藏  举报