从头开始-07.Foundation框架常用结构体
一、Foundation框架常用结构体NSRange\CGRange、NSPoint\CGPoint、NSSize\CGSize、 NSRect\CGRect 的使用
1. 基本使用:
//NSRange的使用
NSRange r1 = NSMakeRange(, ); //第一个参数为.location 第二个参数为.length NSString *str = @"学习OC"; NSRange range = [str rangeOfString:@"学习"]; //查找某个字符串在str字符串中的位置和长度 NSLog(@"loc = %ld, len = %ld",range.location, range.length); NSRange range1 = [str rangeOfString:@"找不到"]; //查找某个字符串在str字符串中的位置和长度 NSLog(@"loc = %d, len = %ld",range1.location, range1.length); //如果找不到,location 的返回值为-1,要用%d输出location 的值才会有负号,默认的%ld是没有符号 //CGPoint的使用
CGPoint point1 = NSMakePoint(, );
NSPoint point2 = CGPointMake(, ); //平常经常使用这种方法定义一个CGPoint 对象 //NSSzie的使用
NSSize size1 = CGSizeMake(, );
CGSize size2 = NSMakeSize(, ); //CGRect 的使用
CGRect rect1 = CGRectMake(, , , );
CGRect rect2 = {point1, size1}; //打印结构体的方法
NSString *s = NSStringFromPoint(point1);
NSLog(@"%@",s); NSString *s1 = NSStringFromRect(rect1);
NSLog(@"%@",s1);
2. 相关的其他方法
//判断结构体NSPoint\CGPoint、NSSize\CGSize、 NSRect\CGRect 包含的值是否相同的方法
CGPointEqualToPoint(point1, point2);
CGRectEqualToRect(rect1, rect2);
CGRectEqualToRect(rect1, rect2); //判断某个rect是否包含某个点,必须引用框架CoreGraphic.framework,这样才能调用 CGRectContainsPoint方法
BOOL b1 = CGRectContainsPoint(rect1, point1);
NSLog(@"%d",b1);
return ;
二、Foundation框架常用类
1.NSString
//创建字符串
NSString *s1 = @"test";
NSString *s2 = [[NSString alloc]initWithFormat:@"test is %c",'c']; //此方法可以将c语言字符串转化为OC字符串
NSString *s3 = [[NSString alloc]initWithUTF8String:"name"]; //OC字符串转化为C语言字符串
const char *cs = [s3 UTF8String]; NSString *s4 = [[NSString alloc] initWithContentsOfFile:@"/Users/lpmac/Desktop/my.text" encoding:NSUTF8StringEncoding error:nil];
[NSString stringWithContentsOfFile:@"/Users/lpmac/Desktop/my.text" encoding:NSUTF8StringEncoding error:nil]; NSURL *url = [[NSURL alloc]initWithString:@"file:///Users/lpmac/Desktop/my.text"];
//类方法
[NSURL URLWithString:@"file:///Users/lpmac/Desktop/my.text"]; NSString *s5 = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
//类方法
[NSString stringWithContentsOfFile:@"file:///Users/lpmac/Desktop/my.text" encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",s5);
2.NSMutableString的使用(动态字符串)
NSMutableString * mutableString1 = [NSMutableString stringWithFormat:@"test"];
//拼接字符串
[mutableString1 appendString:@"append str"]; //删除
NSRange range = [mutableString1 rangeOfString:@"test"];
[mutableString1 deleteCharactersInRange:range];
NSLog(@"%@",mutableString1);
3.NSArray 和 NSMutableArray
//数组的创建
NSArray *array = [NSArray arrayWithObject:@"test"];
NSArray *array1 = [NSArray arrayWithObjects:@"test1",@"test2",@"test2",nil];
NSArray *array2 = @[@"test1",@"test2",@"test2"]; //提倡快速写法
//数组访问
[array1 objectAtIndex:];
array1[]; //提倡
//数组的遍历
for (int i=; i<array.count; i++) {
NSLog(@"%@",array[i]);
}
//快速遍历
for (id obj in array2) { NSUInteger loc = [array2 indexOfObject:obj];
NSLog(@"%@,%ld",obj,loc); } [array2 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%@,%ld",obj,idx);
if (idx == ) {
*stop = YES;
}
}];
NSMutableArray 是动态数组不可使用语法@[] 来初始化
从头开始-07.Foundation框架常用结构体的更多相关文章
- 二十四、V4L2框架主要结构体分析和虚拟摄像头驱动编写
一.V4L2框架主要结构体分析 V4L2(video for linux version 2),是内核中视频设备的驱动框架,为上层访问视频设备提供统一接口. V4L2整体框架如下图: 图中主要包括两层 ...
- 13.Object-C--浅谈Foundation框架常用的结构体
------- android培训.iOS培训.期待与您交流! ---------- 昨天学习了Foundation框架中常用的结构体,下面我简单的总结一下,如果错误麻烦请留言指正,谢谢! Found ...
- iOS Foundation框架简介 -1.常用结构体的用法和输出
1.安装Xcode工具后会自带开发中常用的框架,存放的地址路径是: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.plat ...
- iOS Foundation框架 -1.常用结构体的用法和输出
1.安装Xcode工具后会自带开发中常用的框架,存放的地址路径是: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.plat ...
- [OC Foundation框架 - 1] 常用结构体
底层封装是使用了typedef定义的结构体 typedef struct _NSString{ xxx xxx } NSString; 1. NSRange 结构体 #注意结构体不是对象 3种定义 ...
- Foudation框架之一些常用结构体和常用类
表示范围作用的结构体:NSRange: 有三种方式创建新的NSRange: 1.NSRange range: range.location = 17; ...
- 八、Foundation -常用结构体
一.NSRange 在foundation/NSRange.h中对NSRange的定义 typedef struct _NSRange{ NSUInteger location; NSUInteger ...
- Objective-C:Foundation框架-常用类-NSString全解
Foundation框架中常用的类有字符串.集合.字典等,这里介绍字符串NSString.本文分别介绍了NSString的创建.从文件里读取NSString字符串.通过函数改变外部的NSString变 ...
- Objective-C:Foundation框架-常用类-NSValue
NSNumber是NSValue的子类,前者只能包装数字,后者可以包装任意值.NSArray.NSDictionary只能存储OC对象,不能存储结构体.因此,如果想要在NSArray.NSDictio ...
随机推荐
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)(转)
向量(vector) <vector> 连续存储的元素<vector> Vector<int>c; c.back() 传回最后一个数据,不检查这个数据是否存在 ...
- 使用disqus搭建comment时一件非常二的事
近期在github 上面搭建自己的博客,搭建comment部分的时候出现了一个问题:配置都配置好了,可是comment就是不成功.昨天为这个问题折腾了了半晚上没找出原因,今天晚上我突然发现一个地方设置 ...
- 伪元素::before和::after
有时候我们的页面里面有不少其他网站的名字,而且还要求网站名后面还要有网站的链接,类似这样:百度(http://www.baidu.com).这个时候如果网站多的话写起来就很麻烦了 <a href ...
- js判断是否安装flash
<script type="text/javascript"> (function () { var noFlash = "你的浏览器没有安装Flash,会影 ...
- 【SQL语句】 - 在所有存储过程中查找关键字,关键字不区分大小写 [sp_findproc]
USE [EShop]GO/****** Object: StoredProcedure [dbo].[sp_findproc] Script Date: 2015/8/19 11:05:24 *** ...
- [原创] Assistant editor 取消拖拽 binding 的 UI 元素
1. press up-right button "show the utilities" 2. press "show the Connections inspecto ...
- Method to fix "Naming information cannot be located because the target principal name is incorrect." for AD replication failure
Assume Failure DC FP01 and Working DC DC01 1. Stop the Key Distribution Center (KDC) service on FP01 ...
- myeclipse内存不足有关问题
myeclipse内存不足有关问题 myeclipse内存不足问题 使用myeclipse8.5出现如下问题:MyEclipse has detected that less than 5% of t ...
- study notes: high performance linux server programming
1:linux网络API分为:socker地址API,socker基础API,网络信息API 1,socker地址API:包含IP地址和端口(ip, port).表示TCP通信的一端. 2,socke ...
- git 常用命令总结。
引用:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137396284551 ...