OC_Foundation笔记

来源:互联网 发布:淘宝申诉留言怎么写 编辑:程序博客网 时间:2024/05/21 22:50

1.常用结构体

 NSRange (location length)

 NSPoint\CGPoint---->double 开发中使用CGPoint 坐标

 NSSize\CGSize -------> UI元素的宽度

 NSRect\CGRECT (CGPoint CGSize)

使用这些函数的前提是添加CoreGraphics框架


如果查找不到某个字符串在str中的范围,则该字符串的范围为:length= 0,location = NSNotFound == -1


原点:CGSizeZero, CGRectZero,CGPointZero == CGRectZero(0,0,0,0),CGPointZero(0,0),CGPointMake(0,0)


将结构体转为字符串:NSString *str = NSStringFromRect();



2.Foundation框架中常用的类:NSString

 NSString : 不可变字符串

 NSMutableString : 可变字符串


1>字符串的创建方式

*NSString *s1 = @"jack";

*NSString *s2 = [[NSString alloc] initWithString:@"jack"];

*NSString *s3 = [[NSString alloc] initWithFormat:@"age is %d",10];

*  C字符串 --> OC字符串

    NSString *s4 = [[NSString alloc] initWithUTF8String:"jack"];

    OC字符串 --> C字符串

    const char *cs = [s4 UTF8String];

*NSString *s5 = [[NSString alloc] initWithContentsOfFile:@"file url" encoding:NSUTF8StringEncoding error:nil];

*NSString *s6 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

*对象方法:NSURL *url = [[NSURL alloc] initWithString:@"url"];

 类方法:[NSURL fileURLWithPath:@"/Users/Mac/Desktop/1.txt"];

一般不用对象方法,用类方法

一般都会有一个类方法跟对象方法配对  有initWithFormat 就有相应的 stringWithFormat

都以方法名开头


字符串的导出

void strintExport()

{

    //  字符串的导出

    [@"jack\njack" writeToFile:@"url" atomically

                              :YES encoding:NSUTF8StringEncoding error:nil];

    

    NSString  *str = @"34234";

    

    NSURL *url = [NSURL fileURLWithPath:@"url"];

    [str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil ];

    

}


3.NSArray

NSArray (OC数组:以面向对象思维操作) :不可变数组,数据当时创建是有多少个元素 就只能是多少个元素 不可变

NSMutableArray:可变数组 不可以直接放基本数据类型(struct,enum,nil) 当存储oc对象的时候


4.NSSet

OC中想把基本数据类型塞到集合中的时候先把数据类型包成对象,这时候使用NSNumber

NSNumber之所以能包装基本数据类型为对象,是因为继承了NSValue

NSNumber的缺点:只能包括数字,想要包装结构体就只能用NSValue

NSValue能把任何东西都能包装成对象


 NSSet和NSArray的对比

 1 > 公共点

 *都是集合,都能存放多个OC对象

 *只能存放OC对象,不能存放非OC对象类型(基本数据类型:int char float等,结构体,枚举)

 *本身都不可变,都有一个可变的子类 NSMutableSet,NSMutableArray

 

 2>不同点

 *NSArray有顺序,NSSet无顺序


5.NSDictionary

字典:key----->value

     索引----->文字内容

     里面储存的东西都是键值对


   Dictionary创建方法一:

   NSDictionary *dict = [NSDictionary dictionaryWithObject:@"jack" forKey:@"name"];

    

   NSArray *key = @[@"name",@"address"];

   NSArray &objects = @[@"jack",@"北京"];

    

   NSDictionary &dict = [NSDictionary dictionaryWithObjects:objects dorKeys:keys];

    

   Dictionary创建方法二:

   NSDictionary *dict = [NSDictionaryWithObjectsAndKeys:

   @"jack",@"name",

   @"北京",@"address",

   @"34325325",@"qq",nil];


6.NSDate

作用:创建一个时间对象

特点:

 打印出来的是格林治时间(0时区的时间),与我们(北京-8区)相差8个小时


NSDate转为字符串:

 int main()

{   NSDate *date = [NSDate date];

    //日期格式化

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    // y M d

    // m s H24)时 h12)时

    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";

    NSString *str = [formatter stringFromDate:date];

    NSLog(@"%@",str);

}


把字符串转为NSDate:

int main()

{

    //把字符串转为NSDate

    //要求掌握时期格式化

    NSString *time = @"2001/09/10 18:56";

    //想要在手机中显示时间,必须先转为字符串,

    //把时间转为字符串,日期格式化类

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    formatter.dateFormat = @"yyyy/MM/dd HH:mm";

    NSDate *date = [formatter dateFromString:time];

    NSLog(@"%@",date);

    return 0;

}




0 0
原创粉丝点击