——- android培训、java培训、期待与您交流! ———-
1.Foundation框架
框架是由许多类、方法、函数、文档按照一定的逻辑组织起来的集合。
2.NSString介绍和基本使用
一个NSString对象就代表一个字符串。
// 最直接的创建方式
NSString *str = @"abc";//常量
NSString *str2 = @"abc";//与str地址相同
//格式化的方式
NSString *str3 = [NSString stringWithFormat:@"%@",@"abc"];//堆内存
写入文件:
/*
文本编码:
中文: GBK GB2312
3 3
UTF-8: 国际通用编码
2
NSError *error;
[str writeToFile:@"文件路径" atomically:原子性(YES/NO) encoding:NSUTF8StringEncoding error:&error]
if(error!=nil){
NSLog(@"写入失败");
}else{
NSLog(@"写入成功");
}
*/
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str = @"basdasdcr";
//将字符串写入指定文件
NSError *error;
BOOL isOk = [str writeToFile:@"/Users/gongwei/Desktop/str.txt" atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (isOk) {
NSLog(@"写入成功");
}
}
return 0;
}
读取文件:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSError *error;//返回nil表示读取成功
NSString *str = [NSString stringWithContentsOfFile:@"/Users/gongwei/Desktop/str.txt" encoding:YES error:&error];
if (error == nil) {
NSLog(@"%@",str);
}
}
return 0;
}
3.使用NSURL读写字符串
URL格式:
协议,服务器域名,路径。
URL = 协议头://主机域名/路径
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str = @"sadsadgsfdhsdhsg";
//URL
//NSURL oc提供的一个URL处理的类
// NSURL *url1 = [NSURL fileURLWithPath:@"/Users/gongwei/Desktop/str2.txt"];
NSURL *url = [NSURL URLWithString:@"file:///Users/gongwei/Desktop/str2.txt"];
BOOL isOk = [str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (isOk) {
NSLog(@"写入成功!");
}
}
return 0;
}
4.NSString字符串的比较
campare方法,返回结果有三种:
NSOrderedAscending -1
NSOrderedSame 0
NSOrderedDescending 1
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str1 = @"abc";
NSString *str2 = @"Acd";
//比较每个字符的ASCII值大小
//compare默认区分大小写
NSComparisonResult result = [str1 compare:str2];
switch (result) {
case NSOrderedAscending:
NSLog(@"str1<str2");
break;
case NSOrderedSame:
NSLog(@"str1=str2");
break;
case NSOrderedDescending:
NSLog(@"str1>str2");
break;
default:
break;
}
}
return 0;
}
比较字符串是否相等:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str1 = @"abc";
NSString *str2 = [NSString stringWithFormat:@"abc"];
//"=="比较的是地址
//区分大小写
if ([str1 isEqualToString:str2]) {
NSLog(@"相等");
} else {
NSLog(@"不相等");
}
}
return 0;
}
4.NSString前后缀检查
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *url = @"https://ios.cn.png";
if ([url hasPrefix:@"http://"]||[url hasPrefix:@"https://"]) {
NSLog(@"是一个网址");
}
if ([url hasSuffix:@".png"]) {
NSLog(@"是一张图片");
}
}
return 0;
字符串查找:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//查找某个字符串在另一个字符串中首次出现的位置
NSString *str = @"asiosfasasdfsadfaiosfasdgciosfaergxc";
NSString *str1 = @"xjk";
//rangeOfString 返回字符串范围
NSRange rang = [str rangeOfString:str1];
//rang.location 首次出现为止 rang.length 子字符串长度
if (rang.location!=NSNotFound) {
NSLog(@"%lu %lu",rang.location,rang.length);
}else{
NSLog(@"没找到!");
}
}
return 0;
}
5.NSRang的使用
NSRang表示事务的范围,通常是字符串里的字符范围或者数组里的元素范围
typedef struct _NSRange {
NSUInteger location;//位置
NSUInteger length;//长度
} NSRange;
赋值方式:
NSRange range = {1,2};
range.location = 2;
range.length = 3;
range = (NSRange){3,4};
//oc中新增的
NSRange range1 = NSMakeRange(4, 5);
//输出range值
NSLog(@"%@",NSStringFromRange(range1));
6.字符串的截取和替换
//从某个位置开始,直到结束
- (NSString *)substringFromIndex:(NSUInteger)from;
//从开始的位置到指定的位置
- (NSString *)substringToIndex:(NSUInteger)to;
//截取范围内字符串
- (NSString *)substringWithRange:(NSRange)range;
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str = @"abcjsdsf";
NSString *subStr;
//从某个位置开始,直到结束
subStr = [str substringFromIndex:1];//包含起始位置
NSLog(@"%@",subStr);
//从开始的位置到指定的位置
subStr = [str substringToIndex:1];//不包含指定位置位置
NSLog(@"%@",subStr);
//截取范围内字符串
subStr = [str substringWithRange:(NSRange){2,3}];//包含起始位置
NSLog(@"%@",subStr);
}
return 0;
}
字符串替换:
//用replacement替换target
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement;
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str = @"abcjsdsf";
NSString *str1 =@"b";
NSString *str2 =@"@";
//用str2替换str1
str = [str stringByReplacingOccurrencesOfString:str1 withString:str2];
NSLog(@"%@",str);
}
return 0;
}
7.NSString的其他用法
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str = @"abcjsdsf";
//获取单个字符
NSLog(@"%c",[str characterAtIndex:1]);
NSString *str1 =@"10";
//字符串转为数值
NSLog(@"%f",str1.floatValue);
int a = 10;
//数值转换成字符串
NSLog(@"%@",[NSString stringWithFormat:@"%d",a]);
char *s = "zhang";
//c字符串转换为oc字符串
NSLog(@"%@",[NSString stringWithUTF8String:s]);
//c字符串转换为oc字符串
const char *s1 = str.UTF8String;
NSLog(@"%s",s1);
NSString *str2 = @" hello ";
//去掉首尾空格
NSLog(@"---%@---",[str2 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]);
}
return 0;
}
8.NSMutableString基本概念
NSMutableString继承NSString
可以任意添加字符串,删除字符串,指定位置插入字符串。
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableString *mStr = [NSMutableString stringWithFormat:@"abc"];
[mStr appendString:@"&efg"];
NSLog(@"%@",mStr);
}
return 0;
}
9.NSMutableString常用方法
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableString *mStr = [NSMutableString stringWithFormat:@"abcdefg"];
//appendFormat 格式化添加字符串
[mStr appendFormat:@"%d",100];
NSLog(@"%@",mStr);
//删除一部分内容
[mStr deleteCharactersInRange:NSMakeRange(3, 4)];
NSLog(@"%@",mStr);
//指定位置插入字符串
[mStr insertString:@"@#" atIndex:3];
NSLog(@"%@",mStr);
//替换指定内容
[mStr replaceCharactersInRange:NSMakeRange(5, 3) withString:@"替换"];
NSLog(@"%@",mStr);
}
return 0;
}
10.NSArray的介绍和基本使用
不能存储非OC对象,如:基本数据类型。
不能存储nil。可以使用[NSNULL null]
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//NSArray特点
//一旦创建成功,内容不可改变
//只能存放oc对象
//创建一个空数组
NSArray *arr1 = [NSArray array];
//创建只有一个元素的数组
NSArray *arr2 = [NSArray arrayWithObject:@"1"];
//创建多个元素数组
NSArray *arr3 = [NSArray arrayWithObjects:@"1",@"2",@"3",@4, nil];//常见用法
//调用对象方法创建数组
NSArray *arr4 = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@4, nil];
//用数组创建数组
NSArray *arr5 = [NSArray arrayWithArray:arr3];
}
return 0;
}
11.NSArray常见用法
/*
//获取数组元素个数 c语言 sizeof()
@property (readonly) NSUInteger count;
//获取指定下标的元素
- (ObjectType)objectAtIndex:(NSUInteger)index;
//是否包含某个对象
- (BOOL)containsObject:(ObjectType)anObject;
//返回最后一个元素
@property ObjectType lastObject;
//返回第一个元素
@property ObjectType firstObject;
//返回指定对象的下标地址(没找到返回-1)NSNotFound
- (NSUInteger)indexOfObject:(ObjectType)anObject;
//查找范围类指定元素,如果查找到则返回下标
- (NSUInteger)indexOfObject:(ObjectType)anObject inRange:(NSRange)range;
*/
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//创建多个元素数组
NSArray *arr3 = [NSArray arrayWithObjects:@"1",@"2",@"3",@4, nil];//常见用法
NSLog(@"%@",arr3);
//获取数组元素个数
NSLog(@"%lu",arr3.count);
//获取指定下标元素
NSLog(@"%@",[arr3 objectAtIndex:2]);
//
NSLog(@"%lu",[arr3 indexOfObject:@"3"]);
//
if ([arr3 containsObject:@"2"]) {
NSLog(@"包含");
}
}
return 0;
}
简化方式:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 用简化的方式定义和访问数组元素
//用简化的方式定义数组
//格式:@[元素]
NSArray *arr = @[@"0",@"1",@"2",@"3",@4,@"five"];
NSLog(@"%@",arr);
//用简化的方式访问数组元素
NSLog(@"%@",arr[0]);
}
return 0;
}
12.NSArray的遍历方法
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *arr = @[@"0",@"1",@"2",@"3",@4,@"five"];
//对数组遍历
//下标访问
for (int i = 0; i<arr.count; i++) {
NSLog(@"%@",arr[i]);
}
//快速枚举法,增强for循环
for (NSString *obj in arr) {
NSLog(@"%@",obj);
}
//使用bolck方式访问
// 数组元素 元素下标 是否停止
[arr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%lu %@",idx,obj);
}];
}
return 0;
}
13.NSArray 读写文件
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *arr = @[@"0",@"1",@"2",@"3",@4,@"five"];
//把NSArray内容写入文件中
//.plist一种特殊的文件格式
[arr writeToFile:@"/Users/gongwei/Desktop/arr.plist" atomically:YES];
//读取文件
NSArray *arr1 = [NSArray arrayWithContentsOfFile:@"/Users/gongwei/Desktop/arr.plist"];
NSLog(@"%@",arr1);
}
return 0;
}
14.NSArray 与字符串
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//@1 基本数据类型快速转化成对象
NSArray *arr = @[@0,@1,@2,@3,@4];
//把数组的元素链接为 0-1-2-3-4
NSString *str = [arr componentsJoinedByString:@"-"];
NSLog(@"%@",str);
//给一个字符串,分割成数组
NSString *str1 = @"400-800-12580";
NSArray *arr1 = [str1 componentsSeparatedByString:@"-"];
NSLog(@"%@",arr1);
NSLog(@"%@",arr1.firstObject);
}
return 0;
}
15.NSMutableArray及基本使用
NSMutableArray继承NSArray,可变,可以添加\更改\删除元素。
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//创建一个空数组
// NSMutableArray *mArr = [NSMutableArray array];
//创建数组,并初始化一个元素
// NSMutableArray *mArr = [NSMutableArray arrayWithObject:@"one"];
//创建数组,并初始化多个元素
NSMutableArray *mArr = [NSMutableArray arrayWithObjects:@"one",@"two",@"three", nil];
//创建数组并指定初始长度
// NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:5];
NSLog(@"%p",mArr);
//添加一个元素
[mArr addObject:@"fengjie"];
NSLog(@"%@",mArr);
NSLog(@"%p",mArr);
//插入一个元素到指定位置
[mArr insertObject:@"fiefei" atIndex:0];
NSLog(@"%@",mArr);
//删除某个元素
[mArr removeObject:@"fengjie"];
NSLog(@"%@",mArr);
//删除指定下标的元素
[mArr removeObjectAtIndex:2];
NSLog(@"%@",mArr);
//全部删除
[mArr removeAllObjects];
NSLog(@"%@",mArr);
NSMutableArray *mArr1 = [NSMutableArray arrayWithObjects:@"one",@"two",@"three", nil];
//修改元素
// [mArr1 replaceObjectAtIndex:0 withObject:@"four"];
//直接修改
mArr1[0] = @"four";
NSLog(@"%@",mArr1);
//查找元素
NSLog(@"%d",[mArr1 containsObject:@"four"]);
//交换元素
[mArr1 exchangeObjectAtIndex:0 withObjectAtIndex:2];
NSLog(@"%@",mArr1);
//逆序
NSMutableArray *arr3 = [NSMutableArray array];
arr3.array = @[@1,@2,@3,@4,@5];
NSLog(@"%@",arr3);
for (int i = 0; i<arr3.count-1-i; i++) {
[arr3 exchangeObjectAtIndex:i withObjectAtIndex:arr3.count-1-i];
}
NSLog(@"%@",arr3);
}
return 0;
}
16.NSDictionary的介绍及使用
字典,不可变,通过key找到对应value.
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@"Hello, World!");
//创建空字典
NSDictionary *d1 = [NSDictionary dictionary];
//创建只有一组键值对的字典
NSDictionary *d2 = [NSDictionary dictionaryWithObject:@"zhangsan" forKey:@"zs"];
NSLog(@"%@",d2);
//创建多组键值对字典
NSDictionary *d3 = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@1,@"value2",@2, nil];
NSLog(@"%@",d3);
//注意:字典的key值和value值都是对象
//快速创建一个字典
NSDictionary *d4 = @{@"ls":@"lisi",@"bz":@"banzhang",@"wy":@"wanyier"};
NSLog(@"%@",d4);
//获取字典长度
NSLog(@"%ld",d4.count);
//根据key值取出value值
NSString *str = [d4 objectForKey:@"ls"];
NSLog(@"%@",str);
//字典的遍历
for (NSString *key in d4) {
NSLog(@"-->%@",[d4 objectForKey:key]);
}
//另一种遍历
[d4 enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"%@ =>> %@",key,obj);
}];
}
return 0;
}
17.NSDictionary的简写形式及文件操作
简写方式根据key获取value:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSDictionary *d4 = @{@"ls":@"lisi",@"bz":@"banzhang",@"wy":@"wanyier"};
//简写方式通过key获取value
NSLog(@"%@",d4[@"ls"]);
//把字典保存到文件中
[d4 writeToFile:@"/Users/gongwei/Desktop/d.plist" atomically:YES];
//从文件中读取字典
NSLog(@"%@",[NSDictionary dictionaryWithContentsOfFile:@"/Users/gongwei/Desktop/d.plist"]);
}
return 0;
}
18.NSMutableDictionary介绍和使用
NSMutableDictionary继承NSDictionary,可变。
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//创建空字典
NSMutableDictionary *d1 = [NSMutableDictionary dictionary];
// NSMutableDictionary *d2 = [NSMutableDictionary dictionaryWithCapacity:3];
//添加key和value 修改一样的
[d1 setValue:@"lisi" forKey:@"ls"];
[d1 setValue:@"lisi2" forKey:@"ls"];//key值重复覆盖之前的value
[d1 setValue:@"zhaosi" forKey:@"zs"];
d1[@"ql"] = @"qianliu";//简写
NSLog(@"%@",d1);
//删除
[d1 removeObjectForKey:@"ql"];
NSLog(@"%@",d1);
//查找
NSLog(@"%d",[[d1 allKeys] containsObject:@"ls"]);
}
return 0;
}
NSArray和NSDictionary对比:
1)NSArray有序,NSDictionary无序
2)NSArray通过下标访问元素,NSDictionary通过key访问元素