标签:
什么是框架?
Foundation框架的作用
如何使用Foundation框架
Foundation框架中的类
objc NSString : 字符串 NSArray : 数组 NSDictionary : 字典 NSDate : 日期 NSData : 数据 NSNumber : 数字
Foundation框架中的类都是以NS为前缀(Next Step的缩写)
解决方案很简单, 只需要删除Xcode的缓存即可
要想看到上述文件夹, 必须在终端敲指令显示隐藏文件夹, 指令如下
什么是NSString?
NSString应用场景
objc NSString *str1 = @"lnj"; NSString *str3 = @"lnj"; NSLog(@"str1 = %p, str3 = %p", str1, str3); 输出地址一致
objc NSString *str2 = [NSString stringWithFormat:@"lnj"]; NSString *str4 = [NSString stringWithFormat:@"lnj"]; NSLog(@"str2 = %p, str4 = %p", str2, str4); 输出地址不一样
NSString *str2 = [[NSString alloc] initWithFormat:@"height is %f". 1.75];
// 用来保存错误信息
NSError *error = nil;
// 读取文件内容
NSString *str = [NSString stringWithContentsOfFile:@"/Users/LNJ/Desktop/lnj.txt" encoding:NSUTF8StringEncoding error:&error];
// 如果有错误信息
if (error) {
NSLog(@"读取失败, 错误原因是:%@", [error localizedDescription]);
} else { // 如果没有错误信息
NSLog(@"读取成功, 文件内容是:\n%@", str);
}
NSString *str = @"江哥";
BOOL flag = [str writeToFile:@"/Users/LNJ/Desktop/lnj.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (flag == 1)
{
NSLog(@"写入成功");
}
NSString *str1 = @"江哥";
BOOL flag = [str1 writeToFile:@"/Users/LNJ/Desktop/lnj.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSString *str2 = @"南哥";
[str2 writeToFile:@"/Users/LNJ/Desktop/lnj.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSString *str = [NSString stringWithContentsOfFile:@"/Users/LNJ/Desktop/lnj.txt" encoding:NSUTF8StringEncoding error:&error];
NSLog(@"str = %@", str);
输出结果:南哥
什么是URL
URL的格式
常见的URL协议头(URL类型)
URL的创建
objc NSURL *url = [NSURL URLWithString:@"file:///Users/LNJ/Desktop/str.txt"];
objc NSURL *url = [NSURL fileURLWithPath:@"/Users/LNJ/Desktop/str.txt"];
---// 用来保存错误信息
NSError *error = nil;
// 创建URL路径
// NSString *path = @"file://192.168.199.119/Users/LNJ/Desktop/lnj.txt";
// 本机可以省略主机域名
// NSString *path = @"file:///Users/LNJ/Desktop/lnj.txt";
NSURL *url = [NSURL URLWithString:path];
// 利用fileURLWithPath方法创建出来的URL默认协议头为file://
NSURL *url = [NSURL fileURLWithPath:@"/Users/LNJ/Desktop/lnj.txt"];
// 读取文件内容
NSString *str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
// 如果有错误信息
if (error) {
NSLog(@"读取失败, 错误原因是:%@", [error localizedDescription]);
} else { // 如果没有错误信息
NSLog(@"读取成功, 文件内容是:\n%@", str);
}
NSString *str = @"江哥";
[str writeToURL:[NSURL URLWithString:@"/Users/LNJ/Desktop/lnj.txt"] atomically:YES encoding:NSUTF8StringEncoding error:nil];
全部字符转为大写字母
全部字符转为小写字母
首字母变大写,其他字母都变小写
NSString *str1 = @"lnj";
NSString *str2 = [NSString stringWithFormat:@"lnj"];
if ([str1 isEqualToString:str2]) {
NSLog(@"字符串内容一样");
}
if (str1 == str2) {
NSLog(@"字符串地址一样");
}
NSString *str1 = @"abc";
NSString *str2 = @"abd";
switch ([str1 compare:str2]) {
case NSOrderedAscending:
NSLog(@"后面一个字符串大于前面一个");
break;
case NSOrderedDescending:
NSLog(@"后面一个字符串小于前面一个");
break;
case NSOrderedSame:
NSLog(@"两个字符串一样");
break;
}
输出结果: 后面一个字符串大于前面一个
NSString *str1 = @"abc";
NSString *str2 = @"ABC";
switch ([str1 caseInsensitiveCompare:str2]) {
case NSOrderedAscending:
NSLog(@"后面一个字符串大于前面一个");
break;
case NSOrderedDescending:
NSLog(@"后面一个字符串小于前面一个");
break;
case NSOrderedSame:
NSLog(@"两个字符串一样");
break;
}
输出结果:两个字符串一样
- (BOOL)hasPrefix:(NSString *)aString;
- (BOOL)hasSuffix:(NSString *)aString;
- (NSRange)rangeOfString:(NSString *)aString;
typedef struct _NSRange {
NSUInteger location;
NSUInteger length;
} NSRange;
// NSUInteger的定义
typedef unsigned int NSUInteger;
NSRange用来表示事物的一个范围,通常是字符串里的字符范围或者数组里的元素范围
NSRange有2个成员
比如@“I love LNJ”中的@“LNJ”可以用location为7,length为3的范围来表示
NSRange range;
range.location = 7;
range.length = 3;
NSRange range = {7, 3};
或者
NSRange range = {.location = 7,.length = 3};
NSRange range = NSMakeRange(7, 3);
NSString *str = @"<head>小码哥</head>";
str = [str substringFromIndex:7];
NSLog(@"str = %@", str);
输出结果: 小码哥</head>
NSString *str = @"<head>小码哥</head>";
str = [str substringToIndex:10];
NSLog(@"str = %@", str);
输出结果: <head>小码哥
NSString *str = @"<head>小码哥</head>";
NSRange range;
/*
range.location = 6;
range.length = 3;
*/
range.location = [str rangeOfString:@">"].location + 1;
range.length = [str rangeOfString:@"</"].location - range.location;
NSString *res = [str substringWithRange:range];
NSLog(@"res = %@", res);
输出结果: 小码哥
NSString *str = @"http:**520it.com*img*ljn.gif";
NSString *newStr = [str stringByReplacingOccurrencesOfString:@"*" withString:@"/"];
NSLog(@"newStr = %@", newStr);
输出结果: http://www.520it.com/img/ljn.gif
NSString *str = @" http://520it.com/img/ljn.gif ";
NSString *newStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"str =|%@|", str);
NSLog(@"newStr =|%@|", newStr);
输出结果:
str =| http://520it.com/img/ljn.gif |
newStr =|http://520it.com/img/ljn.gif|
NSString *str = @"***http://520it.com/img/ljn.gif***";
NSString *newStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"*"]];
NSLog(@"str =|%@|", str);
NSLog(@"newStr =|%@|", newStr);
输出结果:
str =|***http://520it.com/img/ljn.gif***|
newStr =|http://520it.com/img/ljn.gif|
// 其实就是判断是否以/开头
// NSString *str = @"/Users/NJ-Lee/Desktop/lnj.txt";
NSString *str = @"Users/NJ-Lee/Desktop/lnj.txt";
if ([str isAbsolutePath]) {
NSLog(@"是绝对路径");
}else
{
NSLog(@"不是绝对路径");
}
// 截取最后一个/后面的内容
NSString *str = @"/Users/NJ-Lee/Desktop/lnj.txt";
NSString *component = [str lastPathComponent];
NSLog(@"component = %@", component);
// 其实就是上次最后一个/和之后的内容
NSString *str = @"/Users/NJ-Lee/Desktop/lnj.txt";
NSString *newStr = [str stringByDeletingLastPathComponent];
NSLog(@"newStr = %@", newStr);
// 其实就是在最后面加上/和要拼接得内容
// 注意会判断后面有没有/有就不添加了, 没有就添加, 并且如果有多个会替换为1个
// NSString *str = @"/Users/NJ-Lee/Desktop";
NSString *str = @"/Users/NJ-Lee/Desktop/";
NSString *newStr = [str stringByAppendingPathComponent:@"lnj"];
NSLog(@"newStr = %@", newStr);
// 其实就是从最后面开始截取.之后的内容
// NSString *str = @"lnj.txt";
NSString *str = @"abc.lnj.txt";
NSString *extension = [str pathExtension];
NSLog(@"extension = %@", extension);
// 其实就是上次从最后面开始.之后的内容
// NSString *str = @"lnj.txt";
NSString *str = @"abc.lnj.txt";
NSString *newStr = [str stringByDeletingPathExtension];
NSLog(@"newStr = %@", newStr);
// 其实就是在最后面拼接上.和指定的内容
NSString *str = @"lnj";
NSString *newStr = [str stringByAppendingPathExtension:@"gif"];
NSLog(@"newStr = %@", newStr);
- (NSUInteger)length;
- (unichar)characterAtIndex:(NSUInteger)index;
NSString *str1 = @"110";
NSString *str2 = @"10";
int res = str1.intValue + str2.intValue;
NSLog(@"res = %i", res);
NSString *str1 = @"110";
NSString *str2 = @"10.1";
double res = str1.doubleValue + str2.doubleValue;
NSLog(@"res = %f", res);
NSString *str = @"abc";
const char *cStr = [str UTF8String];
NSLog(@"cStr = %s", cStr);
char *cStr = "abc";
NSString *str = [NSString stringWithUTF8String:cStr];
NSLog(@"str = %@", str);
标签:
原文地址:http://www.cnblogs.com/zhoudaquan/p/5017645.html