博客> Foundation框架大概总结
Foundation框架大概总结
2017-11-21 07:11 评论:0 阅读:541 小肥刀
Foundation
Foundation框架是两类UI框架的基础,是面向数据的简单类和数据类型。Foundation框架是一另一个框架CoreFoundation为基础创建的。而CoreFoundation框架是用纯C语言写的,其中的NSRange,CGPoint,CGSize,CGRect等都是结构体。

1.表示范围的NSRange typedef struct { unsigned int location; unsigned int length; }NSRange;

创建NSRange对象有三种方法 1)NSRange range; range.location = 12; //location是存放此范围的起始位置 range.length = 4;//此范围的元素个数 2)NSRange range1 = {12,4}; 3) NSRange range2 = NSRangeMake(12,4);

2.几何数据类型 1)CGPoint struct CGPoint { float x; float y; }; 2) CGSize struct CGSize { float wight; float height; }; 3) CGRectstruct CGRect { CGPoint point; CGSize size; }; 创建这些几何类型快捷函数为CGPoint,CGSize,CGRect

3.字符串NSString 1)字符串其实就是一串字符序列。oc语言表示为@"字符串"; 类方法:此类所有对象共享的方法,一般通用的功能都定义为类方法 创建字符串:可用+(id)stringWithFormat: (NSString )format, ...;类方法创建 NSString string = @"hello"; NSString *string1 = [NSString stringWithString:string]; 2)字符串一些方法

  • (NSUInteger)length;

    NSString string = @"hello"; NSString string1 = [NSString stringWithString:string];

    NSString *string2 = [NSString stringWithFormat:@"hello"];

    if ([string isEqualToString:string1]) { NSLog(@"string and string1 are the same"); } 字符串比较,返回值为BOOL if ([string compare:string1]) { NSLog(@"string and string1 are the same"); }

    NSString fileName = @"test.sh"; 判断字符串的前缀 if ([fileName hasPrefix:@"test"]) { NSLog(@"the filename has 'test' prefix "); } 判断字符串的后缀 if ([fileName hasSuffix:@"sh"]) { NSLog(@"the filename has 'sh' suffix"); } 可变的字符串 NSMutableString mString = [NSMutableString stringWithCapacity:42]; 在字符串尾部追加字符串 [mString appendString:@"hello"]; 在字符串尾部以一定格式追加字符串 [mString appendFormat:@"i am %d boy",25]; 字符串的范围 NSRange helloRange = [mString rangeOfString:@"hello"]; helloRange.length++; 以一定范围删除字符数 [mString deleteCharactersInRange:helloRange];

4.NSDictionary字典 字典是关键字及其定义组成集合 字典一般用关键字(一般是NSString)标记存储一个数值(一般是OC对象),可以根据关键字查找对应的数值 NSString one = @"one"; NSString two = @"two"; NSDictionary stringDict = [NSDictionary dictionaryWithObjectsAndKeys:one,@"first",two,@"second",nil]; NSMutableDictionary stringDict2 = [NSMutableDictionary dictionary]; [stringDict2 setValue:@"third" forKey:@"three"]; 5.NSArray数组 数组和字典是用来存储oc对象的集合 创建数组对象: NSArray array = [NSArray arrayWithObjects:@"one", @"two" ,@"three", nil]; 字面值创建数组: NSArray array2 = @[@"one", @"two",@"three"]; 遍历数组 for (int i=0; i < [array count]; i++) { NSLog(@"the array index %d has %@", i, [array objectAtIndex:i]); NSLog(@"the array2 index %d has %@", i, array2[i]); } 创建可变的数组: NSMutableArray mArray = [NSMutableArray arrayWithCapacity:50]; for (int i = 0; i < 4; i++) { [mArray addObject:array[i]]; } NSInteger mArrayCount = mArray.count; 快速枚举数组: for (NSString str in array) { NSLog(@"the array has %@", str); }

收藏
0
sina weixin mail 回到顶部