//
// main.m
// OC所有知识点集合
//
// Created by apple on 15/11/24.
// Copyright © 2015年 apple. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//1.字符串
//1.字符串的截取(三个位置的截取)
NSString *str1=@"abcdefg";
//从头截取3个字符
NSLog(@"%@",[str1 substringToIndex:3]);
//从第5个位置截取直到最后
NSLog(@"%@",[str1 substringFromIndex:5]);
//从第3个位置截取3个字符
NSLog(@"%@",[str1 substringWithRange:NSMakeRange(3, 3)]);
// 2.比较两个字符串
NSString *str2=@"ads";
NSString *str3=@"fgh";
NSComparisonResult result=[str2 compare:str3];
if (result==NSOrderedSame) {
NSLog(@"str2=str3");
}else if (result==NSOrderedAscending){
NSLog(@"str2<str3");
}else if(result==NSOrderedDescending){
NSLog(@"str2>str3");
}
// 3.字符串转化成数组,数组转化成字符串
NSString *str4=@"a,b,c,d,e";
NSArray *arr=@[@"a",@"b",@"c",@"d",@"e"];
NSArray *arr1=[str4 componentsSeparatedByString:@","];
NSString *str5=[arr componentsJoinedByString:@","];
for (NSString *str in arr1) {
NSLog(@"字符串转化成数组%@ ",str);
}
NSLog(@"数组转化成字符串%@",str5);
// 4.创建可变字符串,并赋值
NSMutableString *str6=[[NSMutableString alloc]init];
str6=[NSMutableString stringWithString:str1];
// 5.插入字符
[str6 insertString:@"haha" atIndex:3];
NSLog(@"插入字符%@",str6);
// 6.删除字符
[str6 deleteCharactersInRange:NSMakeRange(3, 2)];
NSLog(@"删除字符%@",str6);
// 7.查找并删除
NSRange a=[str6 rangeOfString:@"ha"];
if (a.location!=NSNotFound) {
[str6 deleteCharactersInRange:a];
}
NSLog(@"查找并删除%@",str6);
// 8.重设字符串
[str6 setString:@"123"];
NSLog(@"重设字符串%@",str6);
// 9.替换字符串
[str6 replaceCharactersInRange:NSMakeRange(1, 1) withString:@"123456"];
NSLog(@"替换字符串%@",str6);
// 10.连接字符串
NSString* string; // 结果字符串
NSString* string1, string2; //已存在的字符串
- 1. string = [NSString initWithFormat:@"%@,%@", string1, string2 ];
- 2. string = [string1 stringByAppendingString:string2];
- 3 . string = [string stringByAppendingFormat:@"%@,%@",string1, string2];
//2.数组
//
// 1.创建不可变数组,并输出所有元素
NSArray *arr2=@[@"lv",@"23"];
for (NSString *str in arr2) {
NSLog(@"创建不可变数组,并输出所有元素%@",str);
}
// 2.创建可变数组,将不可变数组赋值到可变数组中
NSMutableArray *arr3=[[NSMutableArray alloc]init];
arr3=[NSMutableArray arrayWithArray:arr2];
for (NSString *str in arr3) {
NSLog(@"创建可变数组,将不可变数组赋值到可变数组中%@",str);
}
// 3.增加元素,删除元素
[arr3 addObject:@"haha"];
for (NSString *str in arr3) {
NSLog(@"增加元素%@",str);
}
[arr3 removeObject:@"lv"];
for (NSString *str in arr3) {
NSLog(@"删除元素%@",str);
}
// 4.将数组中的元素进行排序
arr=[arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSComparisonResult result=[obj1 compare:obj2];
return result==NSOrderedAscending;
}];
for (NSString *str in arr) {
NSLog(@"将数组中的元素进行排序%@",str);
}
// 5.找到数组中的元素,并删除
for (int i=0; i<[arr3 count]; i++) {
if ([arr[i] isEqualToString:@"23"]==NO) {
[arr3 removeObject:@"23"];
}
}
for (NSString *str in arr3) {
NSLog(@"找到数组中的元素,并删除%@",str);
}
//3.字典
// 1.创建不可变字典,遍历输出key值和value
NSDictionary *dic=@{@"name":@"lv",@"age":@"21"};
// 2.用枚举类型输出key 和value
NSEnumerator *key=[dic keyEnumerator];
for (NSString *str in key) {
NSLog(@"key=%@",str);
}
NSEnumerator *value=[dic objectEnumerator];
for (NSString *str in value) {
NSLog(@"value=%@",str);
}
// 3.创建可变字典,并赋值,增加键值对,删除键值对
NSMutableDictionary *dic1=[NSMutableDictionary dictionaryWithDictionary:dic];
[dic1 setObject:@"nan" forKey:@"sex"];
NSLog(@"增加键值对%@",dic1);
[dic1 removeObjectForKey:@"name"];
NSLog(@"删除键值对%@",dic1);
// 4.将字典放入数组元素中,增加一条数据,并删除某一条数据
NSMutableArray *aee=[[NSMutableArray alloc]init];
[aee addObject:dic1];
for (NSString *str in aee) {
NSLog(@"将字典放入数组元素中%@",str);
}
NSDictionary *dic3=@{@"name":@"bin",@"age":@"22"};
[aee addObject:dic3];
for (NSString *str in aee) {
NSLog(@"增加一条数据%@",str);
}
// 5.按照某一个键来排序
NSSortDescriptor *age=[NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
NSLog(@"%@",[aee sortedArrayUsingDescriptors:[NSArray arrayWithObjects:age, nil]]);
//4.集合
// 1.创建不可变集合,并输出
NSSet *set=[NSSet setWithObjects:@"1",@"2",@"3", nil];
NSLog(@"创建不可变集合,并输出%@",set);
// 2.判断对象是否在集合中
if ([set containsObject:@"1"]==YES) {
NSLog(@"找到了");
}
// 3.创建可变集合并输出,添加元素,
NSMutableSet *set1=[NSMutableSet setWithSet:set];
NSLog(@"创建可变集合并输出,并输出%@",set1);
[set1 addObject:@"4"];
NSLog(@"添加元素,并输出%@",set1);
// 4.求两个集合的交集和并集
NSMutableSet *set3=[NSMutableSet setWithSet:set];
//[set3 intersectsSet:set1];
[set3 intersectsSet:set1];
NSLog(@"两个集合的交集%@",set3);
[set3 unionSet:set1];
NSLog(@"两个集合的并集%@",set3);
// 5.删除某个元素
[set3 removeObject:@"3"];
NSLog(@"删除某个元素%@",set3);
// 6.集合中求某个元素的个数
NSCountedSet *set4=[NSCountedSet setWithSet:set3];
[set4 addObject:@"2"];
[set4 addObject:@"2"];
NSLog(@"集合中求某个元素的个数%lu",[set4 countForObject:@"2"]);
}
return 0;
}