Object C基础语法
1.关键字
基本上所有关键字都是以@开头
@interface @implementation @end
@public @protected @private @selector
@try @catch @throw @finally
@protecal @optionl @required @class
@property @synthesize @dynamic
self super id cmd _block _strong _week
2.字符串以@开头
比如@"hello“是oc中的字符串,而“Hello"则是c语言中的字符串
3.其它语法
基本数据类型 char int float double BOOL(YES\NO)
nil 相当于c语言NULL,也就是0
基本语句 循环语句(do while,while,for),条件语句(if if-else,switch)
注释 //和/*....*/
屏幕输出NSLog(@"Hello");NsLog(@“age is %i",29);
面向对象
#import <Foundation/Foundation.h> @interface Car :NSObject{ int wheels; int speed; } @end //类的实现 @implementation Car @end int main() { //使用new来创建对象 [Car new]; return 0; }
方法:
#import <Foundation/Foundation.h> @interface Car :NSObject{ //@public 让外部指针直接访问对象内的成员变量 @public int wheels; int speed; // oc方法中任何类型都必须用小括号()括注 // oc方法中的小括号():括注数据类型 } - (void)run; @end //类的实现 @implementation Car - (void)run { NSLog(@"跑起来了"); } @end int main() { //使用new来创建对象 Car *p=[Car new]; //给p指向的对象赋值 p->wheels=4; [p run]; NSLog(@"wheels:%d",p->wheels); return 0; }
bogon:ios asange$ cc -c class.m
bogon:ios asange$ cc class.o -framework Foundation
bogon:ios asange$ ./a.out
2017-06-14 13:09:32.180 a.out[3402:448299] 跑起来了
2017-06-14 13:09:32.180 a.out[3402:448299] wheels:4
bogon:ios asange$
匿名对象
#import <Foundation/foundation.h> @interface Car:NSObject { @public int speed; } - (void) run; @end @implementation Car - (void) run{ NSLog(@"跑起来啦...."); } @end int main() { [Car new]->speed=300; [[Car new] run]; return 0; }
运行结果:
bogon:ios asange$ cc -c test5.m bogon:ios asange$ cc test5.o -framework Foundation bogon:ios asange$ ./a.out 2017-06-20 12:17:41.712 a.out[15692:2446756] 跑起来啦.... bogon:ios asange$
#import <Foundation/foundation.h> @interface Person : NSObject + (void)print; @end @implementation Person + (void)print { NSLog(@"这是类方法"); } @end int main() { [Person print]; return 0; }
运行结果:
bogon:ios asange$ cc -c test7.m bogon:ios asange$ cc test7.o -framework Foundation bogon:ios asange$ ./a.out 2017-06-21 18:12:53.393 a.out[8105:1320528] 这是类方法 bogon:ios asange$
继承结构:
#import <Foundation/Foundation.h> @interface Person : NSObject { @public int name; } - (void)say; @end @implementation Person - (void)say{ NSLog(@"im saying"); } @end @interface User : Person @end @implementation User @end int main() { User *u=[User new]; [u say]; return 0; }
bogon:ios asange$ cc -c test8.m
bogon:ios asange$ cc test8.o -framework Foundation
bogon:ios asange$ ./a.out
2017-06-22 23:53:53.204 a.out[15664:2162356] im saying
bogon:ios asange$
Nstring 的用法
占位符号 ”%@“
#import <Foundation/Foundation.h> int main() { NSString *str=@"hhh"; char *name="你好"; NSLog(@"我在吃饭,%@",str); return 0; }
运行结果:
ios icourt$cc nstring.m -framework Foundation
ios icourt$./a.out
2017-07-12 21:55:59.435 a.out[35531:4095080] 我在吃饭,hhh
ios icourt$
#import <Foundation/Foundation.h> int main() { NSString *str=@"hhh"; char *name="你好"; NSLog(@"我在吃饭,%@",str); int length=[str length];//计算字符串的长度 NSLog(@"字符串长度:%d",length); NSString *newStr=[NSString stringWithFormat:@"my age is %d",28]; NSLog(@"-----%@",newStr); return 0; }
ios icourt$cc nstring.m -framework Foundation
ios icourt$./a.out
2017-07-12 22:11:43.491 a.out[36121:4115883] 我在吃饭,hhh
2017-07-12 22:11:43.491 a.out[36121:4115883] 字符串长度:3
2017-07-12 22:11:43.492 a.out[36121:4115883] -----my age is 28
ios icourt$
@property的作用: 生产get set方法
@synthesize 自动生成get set 并且会访问这个带下滑线的变量 如:@synthesize age 访问的是_age变量
id 万能指针 指向任何oc对象 等同于NSObject
构造方法 模版
- (id)init
{
if(self=[super init])
{
self.age=10;
}
return self;
}
NSObject 默认description方法 返回的类名+内存地址 . 跟java类似
NSString的简单用法
NSString *str=@"hello";
NSLog(@"开始吧,%@",str);
NSString *na=[NSString stringWithFormat:@"hello:%@",@"xx"];
NSLog(na);
//字符转换成字串 char *c="yes"; NSString *string= [NSString stringWithUTF8String:c]; NSLog(@"字符转字符串%@",string); //字符串转换成字符 char *c2=[string UTF8String]; NSLog(@"字符串转字符%s",c2);