Foundation框架

1.Foundation框架介绍

框架是由许多类、方法、函数以及文档按照一定的规则组合的起来的集合。

cocoa程序编写主要用到2个框架Foundation和Application(UIKit)。Foundation主要定义的是一些基础类,供程序员使用,Application Kit是一些用户界面设计的类,用于Mac开发使用。Foundation框架所有类都继承自NSObject对象。

Foundation(通用的面相对象的函数库)提供字符串,数值的管理,容器及其枚举,以及一些其他的与图形用户界面没有直接关系的功能。其中用于类和常数的“NS”前缀来自于Cocoa的来源,NextSTEP。可以在Mac OS X和iOS中使用。

2.NSObject常用方法

2.1 介绍:一切类的根类(基类),无父类。所有oc对象都必须直接或者间接的继承NSObject类。

2.2 常用方法

(1)比较两个对象是否为同一个对象(指针是否指向同一个内存区域)

- (BOOL)isEqual:(id)object;

(2)调用一个方法

- (id)performSelector:(SEL)aSelector;

(3)调用一个方法并传递1个参数

- (id)performSelector:(SEL)aSelector withObject:(id)object;

(4)调用一个方法并传递2个参数

- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2; 

(5)某一个对象是否派生或属于某一类

- (BOOL)isKindOfClass:(Class)aClass;

(6)某一个对象是否属于某类

 - (BOOL)isMemberOfClass:(Class)aClass;

(7)某对象是否响应指定的方法

 - (BOOL)respondsToSelector:(SEL)aSelector;

(8)返回指定对象的父类和本类

 -(Class)superclass; -(Class)class;

3. 字符串的基本概念和常用处理方法

3.1 字符串基本概念

在Foundation框架中,提供NSString类用于处理字符串对象。oc语言是建立在c语言基础上的,故为区别两者字符串,oc语言的字符串必须以@开头,引号中则是字符串的内容,如@"abc"。而且NSString对象一旦被创建不可以再修改,如需修改字符串对象,则需创建NSMutableString实例。

3.2字符串的创建

1).创建一个字符串常量  

NSString *string=@"一个字符串常量";

2).创建一个空的字符串  

NSString *string=[[NSString alloc] init];  或者  NSString *string=[NSString string];

3).快速创建一个字符串  

NSString *string=[[NSString alloc] initWithString:@"快速创建字符串"];  或者     

NSString *string=[NSString stringWithString:@"快速创建字符串"];(创建的是字符串常量,不推荐使用)

4).快速创建一个格式化字符串  

int number=2;  NSString *string=[[NSString alloc] initWithFormat:@"%d",number];

float number=2.5  NSString *string=[NSString stringWithFormat:@"浮点数%f",number];

3.3 比较字符串

1)测试字符串内容是否相同

NSString *string1=[[NSString alloc] initWithFormat:@"test"];

NSString *string2=[[NSString alloc] initWithFormat:@"test"];

if([string1 isEqualToString:string2]){NSLog(@"测试连个字符串是否相等");}

2)测试字符串内容是否相同

NSString *string1=[[NSString alloc] initWithFormat:@"test"];

NSString *string2=[[NSString alloc] initWithFormat:@"test"];

if([string1 == string2]){NSLog(@"测试连个字符串是否为同一个对象");}

3)比较字符串的先后顺序

NSString *string1=[[NSString alloc] initWithFormat:@"a"];

NSString *string2=[[NSString alloc] initWithFormat:@"b"];

NSLog(@"[string1 caseInsensitiveCompare:string2]:%ld",[string1 caseInsenstiveCompare:string2]);

4)求字符串长度

NSString *string=[[NSString alloc] initWithFormat:@"string length"];

NSUInteger *length=[string length];

3.4字符串的转换

1)改变字符大小写

NSString *hello=@"hello WORLD";

NSLog(@"%@",[hello uppercaseString]);  //全部大写

NSLog(@"%@",[hello lowercaseString]);  //全部小写

NSLog(@"%@",[hello capitalizedString]);  //首字母大写,其他均小写

2)将字符串转换成基本数据类型

NSString *string=@"3.68";

NSLog(@"%d",[string boolValue]);  //转换成BOOL类型

NSLog(@"%f",[string floatValue]);  //转换成浮点型

NSLog(@"%f",[string doubleValue]);  //转换成双精度型

NSLog(@"%d",[string intValue]);  //转换成整型

3)将字符串转换成数组

NSString *string=@"one two three four";

NSArray *array=[string componentsSeparatedByString:@""];

3.5 字符串的截取于拼接

1)截取字符串

NSString *string=[NSString stringWithFormat:@"asdfdf"];

NSString *string1=[string substringToIndex:2];//从字符开头一直截取到指定位置,不包括选中位置字符

NSString *string2=[string substringFromIndex:2];//以指定位置开始(包括指定位置字符),并包括之后的全部字符

2)根据范围截取字符串

NSRange rang;

rang.location=2;

rang.length=3;

NSString *string3==[string substringWithRange:rang];

3)拼接字符串

NSString *str1=@"1",*str2=@"2";

NSString *string=[[NSString alloc] initWithFormat:@"拼接:%@ and %@",str1,str2];

NSString *string1=[str1 stringByAppendingFormat:@"%@",str2];

NSString *string2=[str1 stringByAppendingString:str2];

3.6 查询、比较字符串对象

1.查询字符串

NSString *link=@"khoihsdfnovi";

NSRange range=[link rangeOfString:@"sdfnovi"];

if(range.location!=NSNotFound){NSLog(@"找到了");}

2.比较字符串

NSString *content=@"Hello";

NSComparisonResult result1=[content compare:@"hello"];

NSComparisonResult result2=[content compare:@"hello" options:NSLiterakSearch];//区分大小写

NSComparisonResult result3=[content compare:@"hello" options:NSCaseInsensitiveSearch  range:NSMakeRange(0,5)];//不区分大小写

3.7可变字符串

1)插入字符串

NSMutableString *str=[NSMutableString stringWithString :@"字符串"];

[str insertString:@"可变" atIndex:0];

2)删除字符串

NSMutableString *str=[NSMutableString stringWithString :@"字符符串"];

[str deleteCharactersInRange:NSMakeRange(1,2)];

3)替换字符串

NSMutableString *str=[NSMutableString stringWithString :@"字符串"];

[str replaceCharactersInRange:NSMakeRange(0,2)withString:@"猪肉"];

集合、数字对象常用处理方法和日期的常用处理方法以及异常处理内容由下一章笔记给出

oc语言的Foundation框架(学习笔记1)的更多相关文章

  1. oc语言的Foundation框架(学习笔记2)

    紧接上文…… 4.集合对象 4.1数组 1.基本概念 Foundation中的数组(NSArray,NSMutableArray)是一组有序的对象集合,通过索引下标获取到数组中的各个元素,也分可变和不 ...

  2. 【iOS】Foundation框架 学习笔记

    1.数组 OC数组不能存放nil值OC数组只能存放OC对象.不能存放非OC对象类型,比如int.struct.enum等 ====================================== ...

  3. Cocoa Foundation框架学习笔记 - NSCalendar

    + (void)beginTest { /* FOUNDATION_EXPORT NSString * const NSGregorianCalendar; //公历(常用) FOUNDATION_E ...

  4. JavaSE中Collection集合框架学习笔记(3)——遍历对象的Iterator和收集对象后的排序

    前言:暑期应该开始了,因为小区对面的小学这两天早上都没有像以往那样一到七八点钟就人声喧闹.车水马龙. 前两篇文章介绍了Collection框架的主要接口和常用类,例如List.Set.Queue,和A ...

  5. SSM框架学习笔记_第1章_SpringIOC概述

    第1章 SpringIOC概述 Spring是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架. 1.1 控制反转IOC IOC(inversion of controller)是一种概念 ...

  6. phalcon(费尔康)框架学习笔记

    phalcon(费尔康)框架学习笔记 http://www.qixing318.com/article/phalcon-framework-to-study-notes.html 目录结构   pha ...

  7. Yii框架学习笔记(二)将html前端模板整合到框架中

    选择Yii 2.0版本框架的7个理由 http://blog.chedushi.com/archives/8988 刚接触Yii谈一下对Yii框架的看法和感受 http://bbs.csdn.net/ ...

  8. JavaSE中Collection集合框架学习笔记(2)——拒绝重复内容的Set和支持队列操作的Queue

    前言:俗话说“金三银四铜五”,不知道我要在这段时间找工作会不会很艰难.不管了,工作三年之后就当给自己放个暑假. 面试当中Collection(集合)是基础重点.我在网上看了几篇讲Collection的 ...

  9. JavaSE中Map框架学习笔记

    前言:最近几天都在生病,退烧之后身体虚弱.头疼.在床上躺了几天,什么事情都干不了.接下来这段时间,要好好加快进度才好. 前面用了三篇文章的篇幅学习了Collection框架的相关内容,而Map框架相对 ...

随机推荐

  1. opencv学习之路(24)、轮廓查找与绘制(三)——凸包

    一.简介 二.绘制点集的凸包 #include<opencv2/opencv.hpp> using namespace cv; void main() { //---绘制点集的凸包 Mat ...

  2. linux --- 10.常见命令

    1.在登录Linux时,一个具有唯一进程ID号的shell将被调用,这个ID是什么()A.NID B.PID C.UID C.CID 2.下面那个用户存放用户密码信息()A./boot B./etc ...

  3. Bootstrap3基础 page-header 标题下加分割线

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  4. python from entry to abandon2

    学习Linux已经有大致两周了,依然感觉到自己仍然在运维的大门外徘徊.于是我想要找到一个在Linux之外的业余方向,可以以作为枯燥基础学习的调节.没过多久我就发现了Python可以说是钦定的选择,它作 ...

  5. Spring中ClassPathXmlApplication与FileSystemXmlApplicationContext的区别

    Spring中ClassPathXmlApplication与FileSystemXmlApplicationContext的区别 一.概述 在项目中遇到加载不到Spring配置文件,简单分析后,写此 ...

  6. 微信小程序计算器模拟后续

    今天按着自己的思路又重打了一遍 wxml没什么说的,就是分块起名,显示数字和结果的作为屏幕,数字键盘一行四块 <view class="onTop"> <view ...

  7. .NET Core 如何上传文件及处理大文件上传

    当你使用IFormFile接口来上传文件的时候,一定要注意,IFormFile会将一个Http请求中的所有文件都读取到服务器内存后,才会触发ASP.NET Core MVC的Controller中的A ...

  8. linux 新建用户、用户组 以及为新用户分配权限的基本操作

    分享下Linux系统中创建用户.设置密码.修改用户.删除用户的命令: 创建用户:useradd testuser  创建用户testuser设置密码:passwd testuser  给已创建的用户t ...

  9. JQUERY的属性进行操作

    Jquery方式操作属性(attribute) $().attr(属性名称);   //获得属性信息值 $().attr(属性名称,值);    //设置属性的信息 $().removeAttr(属性 ...

  10. WebSocket ,Socket ,Http差异

    最近一直被Socket 被Http搞懵了,然后归类整理了下 首先需要知道网络七层,从上至下 应用,表示,回话,传输,网络,数据链路,物理,一共7层 WebSocket: 这个是在Html5提出的一种规 ...