关于寒假学习objective-c的感想

本文分享了作者寒假期间学习Objective-C的体会,强调了C语言基础、Xcode使用以及新语法的重要性。深入讲解了协议(正式与非正式)、属性列表(如NSArray、NSDictionary等)的概念,并介绍了谓词NSPredicate的应用及其常用运算符。
摘要由CSDN通过智能技术生成

买了本《learn objective-c on Mac》,一开始买来看起来还蛮薄的,250页,看了后,发现语言都有很多共同的地方,新的特性都从C++和java类似过来,就是换了个名字,感觉看了以后感觉收获蛮大,虽然只接触了点皮毛。

1.C语言知识

2.了解了xcode使用

3.懂得了一些新语法和新特性。

 ---------------------------------------------------------------------------------------------

1.正式协议 与 非正式协议(被@optional的正式协议代替) 与文件加载保存

 

@required 一定要实现

@optional 选择性实现

 

例1.

@protocol NSCopying   //深度复制复制

-(id) copyWithZone:(NSZone*)zone;

@end

 

NSArray的copy为浅复制

 

@protocol NSCoding  //编码与解码

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

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

 

NSCoding可用于将任何对象编码为NSData    NSData可用 [data writeToFile:"tmp.txt" atomically:true] 存入硬盘

 

例1:

 

通过NSKeyedArchiver类进行编码为NSData

NSData*data=[NSKeyedArchiver archivedDataWithRootObject:对象];自动调用encodeWithCoder

 

A *a=[NSKeyedUnarchiver unarchiveObjectWithData:data];  //解码

 ------------------------------------------------------------------------------------------------------------

属性列表 NSArray NSDictionary NSString NSNumber NSDate NSData

1.NSArray

+(id)arrayWithObject:...  以nil结尾

-(unsigned)count;

-(id)objectAtIndex:(unsigned) index;

 

  NSMutableArray

+(id)arrayWithCapacity:(int);

-(void)addObject:(NSObject);

-(void)removeObjectAtIndex:(int)index;

2.NSDictionary

+(id)dicitonaryWithObjectsAndKeys:(id)firstObject,(NSString*)firstkey...;

-(id)objectForKey:(id)key;

 

NSMutableDictionary

+(id)dicitonaryWithCapacity:(int)n;

-(void)setObject:(id) object  forKey:(id)key;

-(void)removeObjectForKey:(id)key;

 

3.NSNumber

封装标准类型

NSNumber*a=[NSNumber numberWithInt:10];

4.NSDate

+(NSDate*)date;

+(NSDate*)dateWithTimeIntervalSinceNow:(int)a;

5.NSData

+(NSData*)dataWithBytes:(NSObject) length:(int);

-(int)length;

-(NSObject) bytes;

 

 

--------------------------------------------------------------------------------------------------------

谓词:

谓词实际上就是用简单的方式给出限制条件   可以用for 和if来替代

NSPredicate:

+(NSPredicate)predicateWithFormat:....

-(NSArray*)filteredArrayUsingPredicate:predicate;

例:

 

谓词还给了很多的运算符(可以沿用C的运算符)

例:沿用上了例子的类

NSPredicate*predicate=[NSPredicate predicateWithFormat:@"(name=='A')AND(name=='B')"];

 

数组运算符  name BETWEEN{MIN,MAX};

                 name IN{"A","B"};

SELF的运用:

NSArray*a=[NSArray arrayWithObject:@"A","B",nil];

NSPredicate*predicate=[NSPredicate predicateWithFormat:@SELF IN " 'A','B' "];

 NSArray* b=[a filteredArrayUsingPredicate:predicate];

 

字符串运算符:

BEGINSWITH

ENDSWITH

CONTAINS

 

[c]忽略大小写

[d]忽略重音符

[cd]都忽略

例: BEGINSWITH[c]

 

 

LIKE运算符

LIKE '*A*' 中间包含A即可

LIKE '??A*'前面有两个字符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值