object c基础, 基本类型(NSString,char*NSDate,NSData),集合NSArray,NSMutableArray,NSDictionary,NSMutableDictionary,NSSet,NSMutableSet
1)。object c 基本类型
ID:指向OC对象的指针;
CLASS;
SEL:是OC来定义方法的关键字,你可以把他当做一个函数指针来使用;
BOOL:YES,NO;TRUE,FLASE;
空值处理不当是引起程序错误的重要原因。
nil:给对象赋值
NULL:给指针赋值
NSNULL: 用于集合操作;
2)常量的声明和预处理宏
常量的声明1
#define kdetailkey @"detail text"
#define dd 30.0
#define cc pi*3/180.0
常量的声明2
typedef enum
{
aa=100,
bb,
cc ,
}TagSystemViews ;
常量的声明3
静态常量声明:static NSString *lc=@"test";
预处理宏
预处理宏声明:
#define network 1
#define NSLog()
3).#import包含头文件
4)#pragram mark分隔代码块
#pragram mark -
#pragram mark network api
5) 内存管理retain,release,引用计数进行自动内存管理;在alloc,retain,copy 的时候对象引用计数增加一,在release时减一,当引用计数为0时,对象的内存就被回收,占用内存释放。
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// create two objects my MyClass.
SomeClass *object = [[SomeClass alloc] init];
NSLog(@"1,object retain count is : %d",[object retainCount]); // counter is 1
[object retain]; // increment the count, now count is 2
NSLog(@"2,object retain count is : %d",[object retainCount]);
[object release]; // decrement the count, now count is 1
NSLog(@"3,object retain count is : %d",[object retainCount]);
object = nil; // set to nil, now count is 0, now object will deallocate.
NSLog(@"4,object retain count is : %d",[object retainCount]);
NSLog(@"5,object: %@",object);
[pool release];
return 0;
6>字符串操作NSString
私有字符串常量:static NSString *const k=@"myconstant";
公有字符串常量:.h 定义:extern NSString *const k;/ #define NSString *const k;
.m 实现:k=@"info.png";
获取子字符串: rangOfString 获取小字符串在大字符串中的长度范围;
substringToIndex:i 获取开始到i长度的子字符长度;
substringFromIndex:i 获取i开始到末尾的子字符长度;
substringWithRange:i,J ,获取i开始到J的子字符长度;
NSString *filename=@"config.ini";
NSString *path=@"users/dev/desktop/config.ini";
NSRange iStart=[path rangeOfString:filename];
NSRange iStart2=[path rangeOfString:filename options:NSCaseInsensitiveSearch];
if (iStart.length>0) {
NSString *substr=[path substringToIndex:iStart.location-1];
NSString *ex=[path substringFromIndex:iStart.location+1];
合格,格式化字符串
stringWithFormat
NSString *re=[NSString stringWithFormat:@"id is %d name is %@,id,name];
NSMutableString *re=[[NSMutableString alloc] init]; //stingbuffer.app();
[re appendString:string1];
[re appendString:string2];
分隔字符串
NSSting *a=@"dog#cat";
NSArray *b=[a componentsSeparatedByString:@"#"];
获取程序运行时目录
安行读取文件
NSstring *temp;
NSArray *readline=[[NSString stringWithContentsOfFile:@"tests.txt"] componentsSeparatedByString:@"\"];
NSEnumerator *nse=[readline objectEnumerator];
while(temp=[nse nextObject])
{
NSLog(@"%@",temp);
}
NSString char*之间的转换
NSString --> char*:
NSString *ban=@"test a string";
char *pre=[ban cStringUsingEncoding:NSASCIIStingEncoding];
char * -->NSString
char encode_buf[1024];
NSString *enstr=[[NSString alloc] initWithCString:(const char*) encode_buf:NSASCIIStringEncoding];
字符串比较
1)判断是否为空;
if([strobject length] >0)
{
//do something
}
if([strobject isEqualToString:@"some string"])
//do something
7 处理数值对象(NSNumber-->int;CGFloat,NSDecimalNuber])
NSNumber -->int
NSNumber *numobj=[NSNumber numberWithInt:2];
NSInteger myint=[numobj integerValue];
int a=[myint intValue];
8 处理日期时间NSDate
获取当前时间:
NSDate * dateToDay=[NSDate date];
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:@yyyy-mm-dd hh:mm:ss"];
NSLocale *locale=[[NSLocale alloc] initWithLocaleIdentifiler:@"en_US"];
[df setLocale:locale];
从字符串生成日期对象:
NSString *mydateString=@"2012-07-13 10:20:00";
NSDate *mydate=[df dateFromString:mydateString];
日期比较:
switch([dateToDay compare:mydate])
{
case NSOrderedSame:
NSLog(@"these dates are the same");
break;
case NSOrderedAscending:
NSLog(@"dateToDay is earlier than mydate");
break;
case NSOrderedDescending:
NSLog(@"mydate is earlier than dateToDay");
break;
default:
NSLog(@"bad time. invalid enum value returned.");
break;
demo:
void dateTimeTest(){
//获取当前日期时间
NSDate *dateToDay = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[df setLocale:locale];
NSString *myDateString = @"2009-09-15 18:30:00";
//从字符串生成日期对象
NSDate *myDate = [df dateFromString: myDateString];
//日期比较
switch ([dateToDay compare:myDate]) {
case NSOrderedSame:
NSLog(@"These dates are the same!");
break;
case NSOrderedAscending:
NSLog(@"dateToDay is earlier than myDate!");
break;
case NSOrderedDescending:
NSLog(@"myDate is earlier than dateToDay!");
break;
default:
NSLog(@"Bad times. Invalid enum value returned.");
break;
}
}
int
main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
dateTimeTest();
[pool release];
return 0;
}
9 NSData 处理
NSData --> NSString
NSMutableData *data; //NSData *data;
NSString *tmpdata=[[NSString alloc] init WithData:data encoding:NSACIIStringEncoding];
NSLog(@"[***] DATA:%@",tmpdata);
NSSting --> NSData
NSString * str=@"test string";
NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];
//demo:
void NSDatatTest(){
//NSData 转换为NSString
NSMutableData *data = nil;
NSString *tmpdata = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"[***] DATA:%@",tmpdata);
[tmpdata release];
//NSString 转换为 NSData
NSString* str= @"teststring";
NSData* tdata=[str dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"[***] tdata:%@",tdata);
}
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSDatatTest();
[pool release];
return 0;
}
10 集合操作:包含,比较,循环,获取,删除,复制,排序,过粒
NSArray,NSDictionary,NSSet及动态集合
将多个对象装入数组,数组的最后一个元素必须是nil;
NSArray *arry=[NSArray arrayWithObjects:@"hennry",@"alex",nil];
NSMutableArray *arry=[[NSMutable alloc] arrayWithCapacity:3];
NSArray *newarray=[NSArray arrayWithArray:array];
数组操作 :containsObject:(id)anObject
NSUintegerCount
lastObject
objectAtIndex:(NSUInteger)index
NSMutableArray *arry=[NSMutable arry];
[arry addObject:[NSColor blackColor]];
NSSet:不要求顺次,要求唯一性,用集合NSSet;交集,重复