//oc中的不可变字符串 关键字NSString 创建方式:
NSString *str = @"heLlo,世界!";
NSLog(@"str的长度是:%li",[str length]);
int a = 5;
NSString *string = [NSString stringWithFormat:@"%@",str];
//判断两个字符串的内容是否相同
if ([str isEqualToString:string]) {
NSLog(@"内容相同");
}
else{
NSLog(@"内容不相同");
}
//三个值 1代表str大于string 0是相等 -1小于
if ([str compare:string]) {
NSLog(@"====");
}
else{
NSLog(@"+++++");
}
//首字母大写,其余的全部小写
NSString *prt = [str capitalizedString];
NSLog(@"%@",prt);
//可变字符串 NSMutableString
NSMutableString *muStr = [NSMutableString stringWithCapacity:10];
//追加字符串
[muStr appendString:@"abc,"];
NSLog(@"append:%@",muStr);
//设置字符串
[muStr setString:@"你好"];
NSLog(@"set:%@",muStr);
//在某个位置插入字符串
[muStr insertString:@"很" atIndex:1];
NSLog(@"insert:%@",muStr);
//查找字符串中的@“很”
NSRange range = [muStr rangeOfString:@"很"];
//用@"不"代替查找到的@“很”
[muStr replaceCharactersInRange:range withString:@"不"];
NSLog(@"replace:%@",muStr);
//不可变数组的定义
NSArray *arr = @[@"1",@"2",@"3"];
//数组的遍历
for (int i = 0; i<[arr count]; i++) {
NSLog(@"%@",arr[i]);
}
NSArray *arrI = [NSArray arrayWithObjects:@"1",@"2",@"4",@"3",nil];
Person *jack = [Person new];
[jack setName:@"jack"];
Person *alice = [Person new];
[alice setName:@"alice"];
Person *hangang = [Person new];
[hangang setName:@"韩刚"];
Person *zhangqi = [Person new];
[zhangqi setName:@"张奇"];
NSArray *arrayPer = @[jack,alice,hangang,zhangqi];
for (int i = 0; i<[arrayPer count]; i++) {
NSLog(@"%@",[arrayPer[i] name]);
NSLog(@"%@", [[arrayPer objectAtIndex:i] name]);
}
//可变的数组 NSMutableArray
NSMutableArray *muArray = [NSMutableArray arrayWithCapacity:10];
//给可变数组追加元素
[muArray addObject:@"1"];
[muArray addObjectsFromArray:@[@"2",@"3",@"4"]];
// NSLog(@"mutable%@",muArray);
//删除可变数组中的元素
// [muArray removeObject:@"2"];
// [muArray removeObjectAtIndex:0];
//数组查询 通过下标找元素
NSString *arrobj = [muArray objectAtIndex:0];
//通过元素找下标
NSInteger index = [muArray indexOfObject:@"1"];
NSRange range = NSMakeRange(0, 2);
[muArray subarrayWithRange:range];
//修改数组中元素的值
[muArray setObject:@"person" atIndexedSubscript:0];
//插入一个元素
[muArray insertObject:@"hello" atIndex:3];
//插入数组的需要借用NSIndexSet这个指针的集合
NSRange rangeI = NSMakeRange(1, [arrayPer count]);
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:rangeI];
[muArray insertObjects:arrayPer atIndexes:indexSet];
//用@"world"代替muArray下标为1的元素
[muArray replaceObjectAtIndex:1 withObject:@"world"];
NSDictionary *dict = @{@"name" : @"alice",@"age":@"19"};
NSDictionary *dictI = [NSDictionary dictionaryWithObject:@"nv" forKey:@"sex"];
NSArray *keyArray = @[@"name",@"age",@"1",@"id",@"num",@"person"];
NSArray *valueArray = @[@"alice",@"19",@"2",@"3",@"4",@"5"];
NSDictionary *dictII = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
NSLog(@"key:name value:%@",[dictII objectForKey:@"name"]);
NSLog(@"%@",dictII[@"name"]);
//字典的遍历
for (id key in dictII) {
NSLog(@"=====+++%@",dictII[key]);
}
//可变字典 NSMutableDictionary
NSMutableDictionary *muDict = [NSMutableDictionary dictionaryWithCapacity:10];
//给字典设置值
[muDict setObject:@"age" forKey:@"17"];
//给字典里面添加元素
[muDict addEntriesFromDictionary:@{@"name":@"jack"}];
//删除字典中name
[muDict removeObjectForKey:@"name"];
//插入元素 插入的值以数组的形式存在
[muDict insertValue:@"value" atIndex:0 inPropertyWithKey:@"key"];
//在字典中可以通过key找value 但是不能通过value找key
NSLog(@"%@",muDict);
//加密练习
NSArray *arrKey = @[@"N",@"S",@"D",@"i"];
NSArray *arrValue = @[@"R",@"W",@"H",@"m"];
NSDictionary *dictTest = [NSDictionary dictionaryWithObjects:arrValue forKeys:arrKey];
NSMutableString *strDict = [NSMutableString string];
for (int i = 0; i<[arrKey count]; i++) {
NSLog(@"%@",dictTest[arrKey[i]]);
[strDict appendFormat:@"%@",dictTest[arrKey[i]]];
}
NSLog(@"加密后的字符串:%@",strDict);
//枚举类型
NSEnumerator *enumor = [arrKey objectEnumerator];
NSString *strEnum ;
while (strEnum = [enumor nextObject]) {
NSLog(@"枚举:%@",strEnum);
}
//给字典的键请求枚举器
NSEnumerator *enumDict = [dictTest keyEnumerator];
for (id key in enumDict) {
NSLog(@"key:%@",key);
}