.h文件
#import <Foundation/Foundation.h>
@interface YPSearchTool : NSObject
@property (nonatomic, strong) NSMutableArray *SearchDataArray;
/**
* 添加搜索歷史
* @param searchStr 新加的輸入條目
*/
+ (void)addSearchRecord:(NSString *)searchStr;
/**
* 獲取所有的搜索歷史
* @return 搜索歷史 字符數(shù)組
*/
+ (NSArray *)getAllSearchHistory;
/**
* 清空搜索歷史
*/
+ (void)clearAllSearchHistory;
@end
.m文件
#import "YPSearchTool.h"
#define RecordCount 3 //存儲數(shù)量
#define SEARCH_HISTORY [[NSUserDefaults standardUserDefaults] arrayForKey:@"SearchHistory"]
@implementation YPSearchTool
+ (void)addSearchRecord:(NSString *)searchStr
{
NSMutableArray *searchArray = [[NSMutableArray alloc]initWithArray:SEARCH_HISTORY];
if (searchArray == nil) {
searchArray = [[NSMutableArray alloc]init];
} 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
{
return SEARCH_HISTORY;
}
+ (void)clearAllSearchHistory
{
[[NSUserDefaults standardUserDefaults] setObject:[[NSMutableArray alloc]init] forKey:@"SearchHistory"];
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者