iOS-简单的自定义保存n条浏览记录

释放双眼,带上耳机,听听看~!
#define RecordCount 6      //存储6条,自定义设置
#define SEARCH_HISTORY [[NSUserDefaults standardUserDefaults] arrayForKey:@"SearchHistory"]

@implementation SearchTool

+ (void)addSearchRecord:(NSString *)searchStr {
  NSMutableArray *searchArray = [[NSMutableArray alloc]initWithArray:SEARCH_HISTORY];
  if (searchArray == nil) {
     searchArray = [[NSMutableArray alloc]initWithObjects:@"北京",@"上海",@"杭州", nil];
  } else if ([searchArray containsObject:searchStr]) {
     [searchArray removeObject:searchStr];
  } else if ([searchArray count] >= RecordCount) {
     [searchArray removeObjectsInRange:NSMakeRange(RecordCount - 1,     
     [searchArray count] - RecordCount + 1)];
  }
  [searchArray insertObject:searchStr atIndex:0];
  [[NSUserDefaults standardUserDefaults] setObject:searchArray forKey:@"SearchHistory"];
}

+ (NSArray *)getAllSearchHistory {
  NSArray *arr = @[@"北京",@"上海",@"杭州"];
  return SEARCH_HISTORY == nil ? arr : SEARCH_HISTORY;
}

+ (void)clearAllSearchHistory {
  [[NSUserDefaults standardUserDefaults] setObject:   
  [[NSMutableArray alloc]init] forKey:@"SearchHistory"];
}

人已赞赏
iOS文章

iOS 常用的正则表达式

2019-10-16 14:44:11

iOS文章

iOS UIButton根据内容自动布局

2019-10-16 18:45:48

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索