LHF Objective-C语法(10)常用类型
来源:互联网 发布:windows xp 默认主题 编辑:程序博客网 时间:2024/06/05 17:57
1、字符串
//======================================================================== /*字符串*/ NSString *s1 = [NSString stringWithFormat:@"You height is %d weight is %d",168,68]; NSLog(s1); NSLog(@"The str length is %lu",[s1 length]); NSString *s2 = @"You Height Is 168 weight is 68"; if([s2 isEqualToString:s1]==YES){ NSLog(@"equal yes"); }else{ NSLog(@"equal no"); } // 详细比较 NSComparisonResult cr = [s2 compare:s1 options:NSCaseInsensitiveSearch|NSNumericSearch]; if(cr == NSOrderedSame){ NSLog(@"Compare YES"); }else{ NSLog(@"Compare NO"); } //判断前缀 if([s1 hasPrefix:@"You"]){ NSLog(@"hasPrefix YES"); }else{ NSLog(@"hasPrefix NO"); } //判断s1中是否含有字符串height NSRange range = [s1 rangeOfString:@"height"]; if(!(range.location ==NSNotFound)){ NSLog(@"The \"height\" is located in %lu",range.location); NSLog(@"The \"height\" length is %lu",range.length); } //======================================================================== //可变字符串 NSMutableString *ms1 = [NSMutableString stringWithCapacity:100]; [ms1 appendString:@"You height is "]; [ms1 appendFormat:@"%d weight is %d",168,68]; NSLog(@"%@",ms1); NSRange r = [ms1 rangeOfString:@"weight is 68"]; [ms1 deleteCharactersInRange:r]; NSLog(@"%@",ms1); [pool release]; return 0;
2、数组
不能存储基本数据类型,如enum struct nil 。 只能存储对象
NSArray *array = [NSArray arrayWithObjects:@"One",@"Two",@"Three",nil]; int count = [array count]; for(int i=0;i<count;i++){ NSLog(@"%@",[array objectAtIndex:i]); } NSString *s = @"iphone,Android,windows7"; array = [s componentsSeparatedByString:@","]; s = [array componentsJoinedByString:@" "]; NSLog(s); //可变数组 NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:10]; [mArray addObject:@"Apple"]; [mArray addObject:@"Google"]; [mArray addObject:@"MicroSoft"]; [mArray removeObjectAtIndex:2]; s = [mArray componentsJoinedByString:@","]; NSLog(s); //数组的迭代器 NSEnumerator *e = [mArray objectEnumerator]; id obj; while(obj=[e nextObject]){ NSLog(@"---%@---",obj); } for(NSString *ms in mArray){ NSLog(@"===%@===",ms); }3、字典(哈希表),与JAVA中的Map类似
//字典 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"Apple", @"A",@"Google",@"G",nil]; NSLog(@"%@",[dic objectForKey:@"A"]); NSMutableDictionary *mDic = [NSMutableDictionary dictionaryWithCapacity:10]; [mDic setObject:@"Apple" forKey:@"A"]; [mDic setObject:@"Google" forKey:@"G"]; [mDic setObject:@"Window7" forKey:@"W"]; [mDic removeObjectForKey:@"w"]; for(id key in mDic){ NSLog(@"%@:%@",key,[mDic objectForKey:key]); } NSEnumerator *keyEnum = [mDic keyEnumerator]; id key; while(key=[keyEnum nextObject]){ NSLog(@"---%@:%@---",key,[mDic objectForKey:key]); }
4、哈希Set,与JAVA中的HashSet是一致的
#import <Foundation/Foundation.h>@interface Person : NSObject{ int pid; NSString *name;}-(void) setPid:(int)pid;-(void) setName:(NSString*) name;-(id) pid;-(NSString*) name;@end@implementation Person-(void) setPid:(int)p{ pid = p;}-(void) setName:(NSString*) n{ [n retain]; [name release]; name = n;}-(id) pid{ return pid;}-(NSString*) name{ return name;}-(NSUInteger)hash{ return pid+[name hash];}-(BOOL)isEqual:(id)p{ if(pid==[p pid] && [name isEqualToString:[p name]]){ return YES; }else{ return NO; }}-(void) dealloc{ [name release]; [super dealloc];}@end
//哈希SetPerson *p1 = [[Person alloc] init]; [p1 setPid:1]; [p1 setName:@"Name1"]; Person *p2 = [[Person alloc] init]; [p2 setPid:1]; [p2 setName:@"Name1"]; Person *p3 = [[Person alloc] init]; [p3 setPid:3]; [p3 setName:@"Name3"]; NSSet *set = [NSSet setWithObjects:p1,p2,p3,nil]; NSEnumerator *e = [set objectEnumerator]; Person *p; while (p=[e nextObject]) { NSLog(@"%d",[p pid]); }p1 和 p2实例相同,输出两个person的pid5、封装类
前面的几个容器类都不能存放基本数据结构、enum、struct、nil 在JAVA中我们知道有封装类,例如:int--Integer boolean--Boolean
Cocoa 使用NSValue作为封装类
NSNumber *n1 = [NSNumber numberWithChar:'A'];NSNumber *n2 = [NSNumber numberWithInt:100]; NSNumber *n3 = [NSNumber numberWithFloat:99.9F]; NSNumber *n4 = [NSNumber numberWithBool:YES]; NSLog(@"%d",[n4 intValue]);
6、日期类型//获取当前时间 NSDate *date = [NSDate date]; //格式化一个字符串为日期 NSCalendarDate *date2 = [NSCalendarDate dateWithString:@"26 Apr 2008" calendarFormat:@"%d %b %Y"]; //当前时间的前一天时间 NSDate *date3 = [NSDate dateWithTimeIntervalSinceNow:-24*60*60];7、数据缓冲区
NSData(NSMutableData)类型来实现缓冲区,类似与Java的字节数组,用于存储二进制的数据类型
const char *cs = "He is very height";NSData *data = [NSData dataWithBytes:cs length:strlen(cs)+1]; NSLog(@"%@",data);
- LHF Objective-C语法(10)常用类型
- LHF Objective-C语法(3)布尔类型 nil
- LHF Objective-C语法(2)类
- LHF Objective-C语法(6)继承
- LHF Objective-C语法(8)协议
- LHF Objective-C语法(12)多线程
- LHF Objective-C语法(14)谓词
- LHF Objective-C语法(1)简介
- LHF Objective-C语法(7)id类型、动态判断与选择器
- LHF Objective-C语法(4)对象初始化
- LHF Objective-C语法(5)异常处理
- LHF Objective-C语法(9)内存管理
- LHF Objective-C语法(13)KVC KVO
- LHF Objective-C语法(11)写入和读取属性 对象的复制
- Objective-C语法之动态类型常用方法总结
- Objective-C语法之常用方法说明(不断补充)
- Objective-C语法之动态类型
- Objective-C 语法之常用数据结构
- 客户端验证的极品--jQuery.validator
- openx XMLRPC client示例
- ORACLE常用——整理中,未完成
- Chrome、Safari下焦点事件中的select()自动全选
- HBase bulkload的一个bug定位
- LHF Objective-C语法(10)常用类型
- 解决pl/sql developer中数据库表中显示中文乱码问题
- COM组件设计与应用(十五) 连接点(vc6.0)
- android对话框
- 栅格那点儿事(四C)
- dmstool的用法
- jquery操作xml
- SQL重复记录查询的一些方法
- Mybatis+Spring3.0