Objective-C学习笔记(一)------Foundation Kit快速学习

Objective-C学习笔记(一)------Foundation Kit快速学习,第1张

概述这个学习笔记基本上就是例子的集合,讲解的还是需要多看看相关的书籍,以下所有代码都是在xcode4.5下编译通过。 一、常用的数据类型 NSRange用来表示相关事物的范围,通常是字符串里的字符范围或者是数组里的元素范围。其定义如下 老版本定义: typedef struct _NSRange{    unsigned int location;    unsigned int length; }N

这个学习笔记基本上就是例子的集合,讲解的还是需要多看看相关的书籍,以下所有代码都是在xcode4.5下编译通过。

一、常用的数据类型

NSRange用来表示相关事物的范围,通常是字符串里的字符范围或者是数组里的元素范围。其定义如下

老版本定义:

typedef struct _NSRange{

   unsigned int location;

   unsigned int length;

}NSRange;

新版本定义

typedef unsigned long NSUInteger;

typedef struct _NSRange {

    NSUInteger location;

    NSUInteger length;

} NSRange;


下面这个是NSRange结合Nsstring的一个例子:

int main(int argc,const char * argv[]){        Nsstring *string = @"Objective-C is a cool language.";        NSRange range = {17,4};            NSLog([string substringWithRange:range]);     }

其他常用的结构体:NSPoint,NSRect,NSSize。。。。

二、字符串Nsstring和NSMutableString

Nsstring是不可变的,NSSMutableString是不可变的,NSSMutableString是Nsstring的子类。

下面的例子展示了他们的一些基本用法:

int main(int argc,4};            NSLog([string substringWithRange:range]);           Nsstring *str;    str = [Nsstring stringWithFormat:@"you height is %d feet,%d inches",5,11];    NSLog(str,nil);    Nsstring *str2 = @"this is a string!";    if(![str isEqualToString: str2]){        NSLog(@"The strings are not same!");    }    if([str hasPrefix:@"you"]){        NSLog(@".....");    }    NSRange range2;    range2 = [str rangeOfString:@"feet"];    NSLog(@"range.location:%ld,range.length:%ld",range2.location,range2.length);    NSMutableString *mstr;    mstr = [NSMutableString stringWithCapacity:22];    [mstr appendString:@"hello "];    [mstr appendFormat:@"world!%d",1];    NSLog(mstr,nil);    [mstr deleteCharactersInRange:NSMakeRange(12,1)];    NSLog(mstr,nil);      return (0);}

三、集合类

集合类只能存储Objective-C的对象,不能储存C语言中的基本数据类型,如int,float

1.NSArray和NSMutableArray

NSArray是不可变数组,NSMutableArray是可变数组。不可变的意思是在初始化之后不能添加和删除任何元素,但是数组中包括的对象是可以改变的。

int main(int argc,const char * argv[]){    NSArray *array;    array = [NSArray arrayWithObjects:@"one",@"two",@"three",nil];    //根据索引取数组中的对象    int i;    for(i = 0;i < [array count];i++){        NSLog(@"index %d has %@",i,[array objectAtIndex:i]);    }
    return (0);
}
2.NSEnumberator和快速迭代

NSEnumberator需要先向数组请求枚举器:-(NSEnumberator *)objectEnumberator;

int main(int argc,[array objectAtIndex:i]);    }    //通过使用NSEnumberator遍历数组元素    NSEnumerator * enumerator = [array objectEnumerator];        ID thingIE;    while(thingIE = [enumerator nextObject]){        NSLog(@"I found %@",thingIE);    }    //使用快速迭代遍历元素    for(Nsstring *string in array){        NSLog(@"I found %@",string);    }}

3.NSDictionary和NSMutableDictionary

字典就是关键字及其定义的集合,在可变字典中关键字须唯一,如果插入的新记录的关键字存在,那么新值会替换原有数值。
int main(int argc,const char * argv[]){    Nsstring *str1 = @"one";    Nsstring *str2 = @"two";    Nsstring *str3 = @"three";        NSDictionary *dict;    dict = [NSDictionary dictionaryWithObjectsAndKeys:str1,@"one",str2,str3,nil];    //使用快速迭代    for(Nsstring *string in dict){        NSLog(@"the key is %@,the value is %@",string,[dict objectForKey:string]);    }    //使用枚举器遍历    Nsstring *string;    NSEnumerator *enumerator;    while(string = [enumerator nextObject]){        NSLog(@"the value is %@",string);    }    return (0);}
4.NSNumber

使用NSNumber类来包装基本数据类型。

int main(int argc,const char * argv[]){    NSMutableArray *array;    NSMutableDictionary *dict;    NSNumber *number = [NSNumber numberWithInt:20];    //将对象放入数组和字典中    [array addobject:number];    [dict setobject:number forKey:@"Bork"];        //获得基本类型数据        NSLog(@"the number is %d",[number intValue]);    return (0);}

5.NSValue

NSValue是NSNumber的父类,可以包装任意值

int main(int argc,const char * argv[]){    NSMutableArray *array;        NSRect rect = NSMakeRect(1,1,30,40);        NSValue *value1 = [NSValue valueWithBytes:&rect objCType:@encode(NSRect)];        [array addobject:value1];        //    NSValue *value2 = [array objectAtIndex:0];    [value2 getValue:&rect];    return (0); // NSValue *value3 = [NSValue valueWithRect:rect]; [array addobject:value3];    return (0);}

6.综合小例子:查找文件

查找用户主目录下的所有.jpg文件

int main(int argc,const char * argv[]){    //创建一个文件管理器,用它来返回一个目录迭代器    NSfileManager *manager;    manager = [NSfileManager defaultManager];        //~表示当前用户的主目录,stringByExpandingTildeInPath可以接受~并转换成Nsstrng    Nsstring *home;    home = [@"~" stringByExpandingTildeInPath];    //获得目录迭代器    NSDirectoryEnumerator *direnum;    direnum = [manager enumeratorAtPath:home];    //创建保存文件所需的可变数组,指定长度为30,这个长度并不限制容器的大小,只是个最优值    NSMutableArray *files;    files = [NSMutableArray arrayWithCapacity:30];    //迭代查找后缀为jpg的文件,将文件名储存到filename中    //pathExtension输出文件的扩展名,不包括前面的“.”    Nsstring *filename;    while(filename = [direnum nextObject]){        if([[filename pathExtension] isEqualTo:@"jpg"]){            [files addobject:filename];        }    }    //使用迭代器输出文件名    NSEnumerator *fileenum;    fileenum = [files objectEnumerator];    while(filename = [fileenum nextObject]){        NSLog(@"%@",filename);    }    return (0);}


下面这个程序实现了同样的功能,只是简化了

int main(int argc,const char * argv[]){    NSfileManager * manager = [NSfileManager defaultManager];        Nsstring *home = [@"~" stringByExpandingTildeInPath];        NSMutableArray * files = [NSMutableArray arrayWithCapacity:20];            for(Nsstring *filename in[manager enumeratorAtPath:home]){        if([[filename pathExtension] isEqualTo:@"jpg"]){            [files addobject:filename];        }    }        for(Nsstring *filename in files){        NSLog(@"%@",filename);    }    return (0);}
总结

以上是内存溢出为你收集整理的Objective-C学习笔记(一)------Foundation Kit快速学习全部内容,希望文章能够帮你解决Objective-C学习笔记(一)------Foundation Kit快速学习所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1053717.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存