- 递归处理数据、查找所有子节点
-(NSMutableArray *)childArray{
if (!_childArray) {
_childArray =[NSMutableArray array];
}
return _childArray;
}
/**
加载菜单数据
*/
-(void) loadDataWithData:(NSArray *)AllArray{
//完整的树形结构数据
NSMutableArray * finalRes = [NSMutableArray array];
for (NSDictionary * dictionary in AllArray) {
NSMutableArray * reslut = [NSMutableArray array];
//没有子菜单
if (![[dictionary allKeys] containsObject:@"child"]){
[reslut addObject:dictionary];
}else{
//有child菜单
[reslut addObject:dictionary];//外层字典加入数组
//写一个方法
NSArray * allChildArr = [self cyclicAllChildArr:dictionary[@"child"]];
[reslut addObjectsFromArray:allChildArr];
}
[finalRes addObject:reslut];
}
////每个字典添加一个头字段
NSMutableArray * addHeaderStrFinalReslutArray = [NSMutableArray array];
for (int i = 0; i< finalRes.count; i++) {
NSMutableArray * sectionArr = [NSMutableArray arrayWithArray:finalRes[i]];
NSString * headerStr = sectionArr.firstObject[@"appname"];
NSMutableArray * sectionIsHaveArr =[NSMutableArray array];
for (NSDictionary * dic in finalRes[i]) {
NSMutableDictionary * MutableDic =[[NSMutableDictionary alloc] initWithDictionary:dic];
[MutableDic setObject:headerStr forKey:@"header"];
[sectionIsHaveArr addObject:MutableDic];
}
[addHeaderStrFinalReslutArray addObject:sectionIsHaveArr];
}
self.moreAllPackedArray = addHeaderStrFinalReslutArray;
[self menuHomeAndSectionData];
}
//查找所有子child
-(NSArray *) cyclicAllChildArr:(NSArray *) chlidArr{
// NSMutableArray * childArray = [NSMutableArray array];
for (NSDictionary * dic in chlidArr) {
if (![[dic allKeys] containsObject:@"child"]){
//不包含
[self.childArray addObject:dic];
}else{
//包含child
[self.childArray addObject:dic];//包含child的外层数据
[self cyclicAllChildArr:dic[@"child"]];
}
}
return self.childArray;
}
2.项目中需要存储用户的偏好设置,菜单数据是数组嵌套字典,字典的某个key对应一个数组(self.inUseTitles writeToFile:<#(nonnull NSString *)#> atomically:<#(BOOL)#>直接写入不成功);分组数据就是数组里面装的字典(这种数据就可以直接用writeToFile 写入成功)。
//本地缓存数据
// NSString * docmentpath =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES).firstObject;
// NSString * menuHomePath = [docmentpath stringByAppendingPathComponent:@"menuSucess.plist"];
// NSString * secetionPath = [docmentpath stringByAppendingPathComponent:@"section.plist"];
MenuDataTool * menuTool =[MenuDataTool sharedMenuDataToolManager];
NSUserDefaults * defauts =[NSUserDefaults standardUserDefaults];
NSString * menuStr = [menuTool jsonStrWithArray:self.inUseTitles];
NSString * sectionStr = [menuTool jsonStrWithArray:self.unUseTitles];
[defauts setObject:menuStr forKey:@"menuKey"];
[defauts setObject:sectionStr forKey:@"sectionKey"];
[defauts synchronize];
// NSString * menuStr = [self jsonStrWithArray:self.inUseTitles];
// BOOL menuSucess = [menuStr writeToFile:menuHomePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
// NSString * sectionStr = [self jsonStrWithArray:self.unUseTitles];
// BOOL sectionSucess = [sectionStr writeToFile:secetionPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
// if (menuSucess) {
// NSLog(@"菜单写入成功");
// }
// if (sectionSucess) {
// NSLog(@"下面组数据写入成功");
// }
不管是NSUserDefaults 还是沙盒存储,最终都需要把存储的数据暴力转成json字符串才最终存储成功
//数组转换成JSON字符串
-(NSString *) jsonStrWithArray:(NSArray *)array{
NSData *data= [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonStr=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
return jsonStr;
}
// 将JSON串转化为字典或者数组
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
NSError *error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingAllowFragments
error:&error];
if (jsonObject != nil && error == nil){
return jsonObject;
}else{
// 解析错误
return nil;
}
}
导航栏透明度-当想要展示导航栏上的barButtonItem,又想让导航栏透明
//设置导航栏
- (void)setNavigationControllerWithColor: (UIColor *)color
{
[self.navigationController.navigationBar setBackgroundImage:[self createImageWithColor:color] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[self createImageWithColor:color]];
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setTranslucent:YES];
}
-(UIImage *) createImageWithColor: (UIColor *) color
{
CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
面试题