首先配置MAC OS 系统 装机也好 虚拟机也好
本人是WIN7下 虚拟机VM 上安装 MAC OS 10.92 +xcode5
OC 基础:oc是苹果的OS X 和 IOS 及其相关API,Cocoa 和 Cocoa Touch的主要编程语言
Cocoa 是 苹果的面向对象开发框架,用来生成
Mac OS X 的应用程序。主要的开发语言为 Objective-c
1 NSLog(@"字符串"); 输出字符串 跟C的 printf差不多 NSLog(@"%d %lu",5,strlen(buffer));
2
NSString Cocoa用来处理字符串的类 不可改变 NSString *a ; a = [NSString stringWithFormat:@"hello Panda!"]; NSLog(@"%@ length is %d ",a,[a length]); //length和stringWithFormat都是NSString的类方法 if([str isEqualToString: str2]){ NSLog(@"the same!"); } [str compare: str2 options: xxx] 选择性比较 [str hasPrefix: @"draft"] 看看是否包含另一个字符串 修改用 NSMutableString这个子类 NSMutableString *str = [NSMutableString stringWithCapacity:50]; [str appendString: @"Hello "]; [str appendFormat: @"human %d",39]; //最后弄完后是 Hello human 39
删除字符串: NSMutableString *str = [NSMutableString stringWithCapacity:50];// NSMutableString 是NSString的子类 [str appendString:@"1 2 3 4"]; NSRange Range = [str rangeOfString:@"3"]; Range.length++; [Range deleteCaractersInRange: Range]; 最后就剩下 "1 2 4" 可以直接 NSMutableString *str = [NSMutableStringstringWithFormat:@"%d = 1",1];
NSArray 一个Cocoa类,用来存储对象的有序列表,可以在其中放置任意类型的对象 限制是只能存储OC的对象 不可改变 NSArray * array = [NSArray arrayWithObjects:@"one",@"two",@"three", nil]; NSArray * array2 = @[@"one",@"two",@"three"]; for (NSInteger i =0; i<[array count]; i++) { NSLog(@"index %d is %@",i,[array objectAtIndex:i]); } for (NSInteger i2 =0; i2<[array2 count]; i2++) { NSLog(@"index %d is %@",i2,[array2 objectAtIndex:i2]); }
MSMutableArray 可以随意添加 NSArray *array = @[@"1",@"2",@"3"]; NSEnumerator *enumerator = [array objectEnumerator]; id object; while (object = [enumerator nextObject]) { NSLog(@"I found %@",object); } for (NSString *string in array) { NSLog(@"I found %@",string); }
NSNumber *number; number = @'x'; number = @12345; number = @12345ul; number = @12345ll; number = @123.45f; number = @123.45; number = @YES; NSRect rect = NSMakeRect(1, 2, 30, 40); NSValue *value = [NSValue valueWithBytes:&rect objCType:@encode(NSRect)];//@encode 用来描述C语言基础类型的方法
下面是查询MAC上主目录下.jpg文件打印到列表 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSFileManager *manager; manager = [NSFileManager defaultManager]; NSString *home; home = [@"~" stringByExpandingTildeInPath];//当前用户的主目录 NSMutableArray *files; files = [NSMutableArray arrayWithCapacity: 42]; for (NSString *filename in [manager enumeratorAtPath: home]) { if ([[filename pathExtension] isEqualTo: @"jpg"]) { [files addObject: filename]; } } for (NSString *filename in files) { NSLog (@"%@", filename); } [pool drain];
//下面为介绍 计数器的原理: @interface RetainTracker : NSObject @end // RetainTracker @implementation RetainTracker - (id) init//初始化 { if (self = [super init]) { NSLog (@"init: Retain count of %d.", [self retainCount]);//获得保留计数器的值 } return (self); } // init - (void) dealloc //结束 { NSLog (@"dealloc called. Bye Bye."); [super dealloc]; } // dealloc @end // RetainTracker //main RetainTracker *tracker = [RetainTracker new]; // count: 1 [tracker retain]; // count: 2 NSLog (@"%d", [tracker retainCount]); [tracker release]; // count: 1 NSLog (@"%d", [tracker retainCount]); [tracker release]; // count: 0, dealloc it
3 .m 文件 .mm 文件扩展名-- c++ and oc编程
类的说明
//@class 提前告知是类 //下面为两个实例变量+3个方法 @interface Circle : NSObject //@interface 是定义新类Circle 基于 NSObject类 { @private ShapeColor fillcolor; ShapeRect bounds; - (void) setFillColor: (ShapeColor) fillColor; // :告诉我们后面有参数 参数类型为圆括号中指定 fillColor为参数名 - (void) setBounds: (ShapeRect) bounds; //有一个矩形区域参数 - (void) draw; //方法不需要参数就不需要冒号 } @implementation Circle //编译器指令,表明为某个类提供 代码, //可以定义没在@interface中没有声明过的方法,看做仅能在当前类实现中使用的私有方法 - (void) setFillColor: (ShapeColor) c //给fillColor重命名 { fillColor = c; } // setFillColor - (void) setBounds: (ShapeRect) b { bounds = b; } // setBounds //如果这里的 是 + 在头 那么就是类方法 - (void) draw { NSLog (@"drawing a circle at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(fillColor)); } // draw
//main: id shapes[3]; ShapeRect rect0 = { 0, 0, 10, 30 }; shapes[0] = [Circle new]; [shapes[0] setBounds: rect0]; [shapes[0] setFillColor: kRedColor]; int i; for(i=0;i<3;i++) {id shape = shapes[i]; [shape draw]; }
//下面为类函数重写过程: @implementation Circle // I'm new! - (void) setFillColor: (ShapeColor) c { if (c == kRedColor) { c = kGreenColor; } [super setFillColor: c]; //这行重要 } // setFillColor - (void) draw { NSLog (@"drawing a circle at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(fillColor)); } // draw @end // Circle
//下面展示一个 自己初始化的类 @interface Car : NSObject { Engine *engine; //另外的两个类 Tire *tires[4]; } - (void) print; @end // Car @implementation Car - (id) init { if (self = [super init]) { //这里就是指代的本身 engine = [Engine new]; tires[0] = [Tire new]; tires[1] = [Tire new]; tires[2] = [Tire new]; tires[3] = [Tire new]; } return (self); } // init
xcode基础
快捷键 我的MAC OS command 就是 win键 option = alt
xcode生成执行文件
在工程的target目录下,找build seeting选项卡,然后找到Per-configuration Build Products Path ,将值设置为 ./appdir
“打开我的电脑” command+n -> command +shift+c
创建快照 command+ctrl+ s 好修改
编辑:
ctrl+e 本行末尾 ctrl+a 本行前端
ctrl + i 重新排列代码
隐藏 command + h
解除导航器 command+0
提示:
ctrl + . 向后翻页 ctrl+ shift + . 向前翻页
按住 alt+ 某个符号就直接查询API
调试:
command+b 生成
command+r 调试 command+\ 下断
command+ctrl+y continue f7 步入 f6 步过 f8 跳出函数
还有双页显示: