object-c最基础语法快速入门笔记
2016-07-30 09:12
726 查看
能力要求:能看、能改对写不作要求(暂时)
通过类比法学习,应该会比较快吧。
解释:
‘-’ 表示实例化方法
‘+’ 表示静态方法,类似static
oc木有public
‘(void)’ 表示返回值
‘initInfo:’注意有个冒号,是函数名
‘mName’表示形参,前面加空格隔开
‘(NSString *)’是形参类型,写在括号中,NSxxx是一套函数库(NextStep)
NSLog、NSString、NSInteger、NSURL、NSImage… …
内部参数外部参数暂时没搞懂
[self getName:@”xxx”]
‘[]’ 看来要加个[ ] 中括号
‘空格’ 用类名 方法名调用,java是xx.xx
后边表示实参,第一个参数不用写名字,其他可以写名字,类似py咯
@ 是用来标识NSString类型的,双引号之前要加上,字符串作为Cocoa的NSString处理
然后是实现:
定义完就使用吧!
实例化是[xxx new]这样的,但是还可以
A *A = [[xx alloc]init] 这是alloc申请内存空间,然后init初始化
区别在于可以自定义构造函数了,new默认使用init,但是alloc/init可以自己定义
关于getter和setter
oc直接通过@property 和 @synthesize 实现了
练习:
1.定义一个接口文件(头文件),然后定义3个方法,参数类型1~n个
2.定义3个成员变量,其中两个使用@property定义get/set
3.实现这个接口
4.在main.m中使用两种方法实例化这个类,并且调用,结果通过NSLog打印出来
字符串类型:NSString* char* 格式化用[NSString stringwithFormat:xxx]
布尔:YES NO
容器类:NSArray不可变,不可以存基本数据类型 NSMutableArray可变化的数据
通过类比法学习,应该会比较快吧。
函数的定义:
- (void) initInfo:(NSString *) mName mAge: (int) value mSex: (NSString *)sex { NSLog(@"my name is %@ ,my age is %d,my sex is %@",mName,mAge,mSex); }
解释:
‘-’ 表示实例化方法
‘+’ 表示静态方法,类似static
oc木有public
‘(void)’ 表示返回值
‘initInfo:’注意有个冒号,是函数名
‘mName’表示形参,前面加空格隔开
‘(NSString *)’是形参类型,写在括号中,NSxxx是一套函数库(NextStep)
NSLog、NSString、NSInteger、NSURL、NSImage… …
内部参数外部参数暂时没搞懂
函数的调用:
[GetInfo initInfo: @”cloudhuan” mAge: “25” mSex:@”男3”][self getName:@”xxx”]
‘[]’ 看来要加个[ ] 中括号
‘空格’ 用类名 方法名调用,java是xx.xx
后边表示实参,第一个参数不用写名字,其他可以写名字,类似py咯
@ 是用来标识NSString类型的,双引号之前要加上,字符串作为Cocoa的NSString处理
类的定义和使用:
先写头文件:#import <Foundation/Foundation.h> // <>这是导入系统的意思? @interface Fraction : NSObject //oc先定义头文件,然后.m写具体实现,这里的@interface很像java的定义接口了 -(void) print; // 嘿嘿,接口都是不写具体实现的 -(void) setNumerator: (int) n; -(void) setDenominator: (int) d; -(int) numerator; -(int) denominator; -(double) convertToNum; @end //定义完成用@end结束,java是大括号
然后是实现:
#import "Fraction.h" // ""这玩意表示是引入自己定义的接口文件、头文件 @implementation Fraction // implment?就是实现,和j一致 { int numerator; int denominator; } -(void) print { NSLog(@"%i/%i",numerator,denominator); } -(void) setNumerator:(int)n { numerator=n; } -(void) setDenominator:(int)d { denominator=d; } -(int) numerator { return numerator; } -(int) denominator { return denominator; } -(double) convertToNum { if(denominator!=0){ return (double)numerator/denominator; }else{ return NAN; } } @end
定义完就使用吧!
#import "Fraction.h" // 还是import到头文件中去 Fraction *fractionA = [Fraction new]; [fractionA setNumerator:1];
实例化是[xxx new]这样的,但是还可以
A *A = [[xx alloc]init] 这是alloc申请内存空间,然后init初始化
区别在于可以自定义构造函数了,new默认使用init,但是alloc/init可以自己定义
关于getter和setter
oc直接通过@property 和 @synthesize 实现了
@property int numerator,denominator; // 在头文件定义 @synthesize numerator,denominator; // 实现类中系统自动实现,调用直接setX getX [xxx setXXX:xxx]
练习:
1.定义一个接口文件(头文件),然后定义3个方法,参数类型1~n个
2.定义3个成员变量,其中两个使用@property定义get/set
3.实现这个接口
4.在main.m中使用两种方法实例化这个类,并且调用,结果通过NSLog打印出来
@interface PeopleInfo:NSObject NSString name; @property int age; - (NSString)getName; - (void)setName:(NSString) mName; @end ------------------------------------------ @implementation PeopleInfo - (NSString)getName{ return name; } - (void)setName:(NSString) mName{ name = mName; } @end ------------------------------------------ PeopleInfo info = [[PeopleInfo alloc]init] [info getName]
基础数据结构
基本数据类型:int short long float double字符串类型:NSString* char* 格式化用[NSString stringwithFormat:xxx]
布尔:YES NO
容器类:NSArray不可变,不可以存基本数据类型 NSMutableArray可变化的数据
相关文章推荐
- Objective-C UIWebview JS 交互
- JAVA学习日志(8-3-object类)
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- 101.You want to move all objects of the APPS user in the test database to the DB_USER schema of the
- 无法确定 <E>E 的类型参数;对于上限为 int,java.lang.Object 的类型变量 E,不存在唯一最大实例
- 通用的查询接口DynamicObjectFactory
- Object Detection_1_级联分类器
- The method update(String, Object...) from the type JdbcTemplate refers to the missing type DataAcces
- PyGobject(三十九)布局容器之PlacesSidebar
- PyGobject(三十八)布局容器之ScrolledWindow
- PyGobject(三十七)布局容器之Dialog
- Object类中定义实现的方法
- NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds
- boolean equal(Object o1,Object o2)
- Effetive Objective - C 1.Objective 14 类对象
- PyGobject(三十六)布局容器之OffscreenWindow
- Effetive Objective - C 1.Objective 消息转发
- Objective-C中的BOOL类型各种转换
- Objective-C中的Block
- NSString 字符串处理:截取字符串、匹配字符串、分隔字符串