iOS 用数组保存搜索记录

来源:互联网 发布:websocket php 编辑:程序博客网 时间:2024/05/22 05:11
//拼接路径
- (NSString *)dataFilePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains
            (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:@“searchPath”];

}


// 初始化数组

if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        NSMutableArray *arr = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
        [_searchHistory addObjectsFromArray:arr];
        [arr release];
    }


// 把数组写入文件,时时保存搜索记录

[_searchHistory addObject:number1];
            [_searchHistory writeToFile:[self dataFilePath] atomically:YES];


// 清空历史记录

[_searchHistory removeAllObjects];
        [_searchHistory writeToFile:[self dataFilePath] atomically:YES];

0 0
原创粉丝点击