iOS數(shù)據(jù)本地化

  • UserDefaults
  • Plist
    存儲NSArrayNSDictionary
  • 歸檔
    針對單一的對象。
  • FMDB
    FMDB是iOS平臺的SQLite數(shù)據(jù)庫框架
    FMDB以OC的方式封裝了SQLite的C語言API
    使用起來更加面向?qū)ο螅∪チ撕芏嗦闊⑷哂嗟腃語言代碼
    提供了多線程安全的數(shù)據(jù)庫操作方法倘待,有效地防止數(shù)據(jù)混亂
  • LevelDB 鏈接
    當你的app需要處理的數(shù)據(jù)上萬條時挠阁,F(xiàn)MDB顯得很吃力,這個吃力表現(xiàn)在效率上孝常,數(shù)據(jù)庫的操作苟跪,肯定都有加鎖廷痘,上萬條數(shù)據(jù)排隊去執(zhí)行,加上反序列化的一些操作件已,
  • CoreData 鏈接
    CoreData出現(xiàn)在iOS 3中笋额,是蘋果推出的一個數(shù)據(jù)存儲框架。CoreData提供了一種對象關系映射(ORM)的存儲關系篷扩,類似于Java的hibernate框架兄猩。CoreData可以將OC對象存儲到數(shù)據(jù)庫中,也可以將數(shù)據(jù)庫中的數(shù)據(jù)轉(zhuǎn)化為OC對象,在這個過程中不需要手動編寫任何SQL語句枢冤。

簡單地將對象保存到沙河路徑的思路

項目中我們可能需要保存一些數(shù)組鸠姨,同時數(shù)組中保存的是一些對象類型數(shù)據(jù),如果使我們創(chuàng)建的對象淹真,需要讓對象遵守 NSCoding 協(xié)議讶迁,采用歸檔來存儲數(shù)據(jù)。

初始化一個單例核蘸,創(chuàng)建沙盒路徑地址

-(instancetype)init{
    if (self = [super init]) {
        self.documentDefaultPath = [self getLocalDataDefaultPathWithOption:JHLocalDataDocuments];
        self.cacheDefaultPath = [self getLocalDataDefaultPathWithOption:JHLocalDataCache];
        self.tempDefaultPath = [self getLocalDataDefaultPathWithOption:JHLocalDataTemp];
        self.libraryDefaultPath = [self getLocalDataDefaultPathWithOption:JHLocalDataLibrary];
        self.perferenceDefaultPath = [self getLocalDataDefaultPathWithOption:JHLocalDataPerferences];
        
    }
    return self;
}

/** data */

-(void)saveData:(NSData *)data withDataType:(JHLocalDataType)type WithKey:(NSString *)key withisSucess:(void (^)(BOOL))complementBack{
    if (data.length == 0 || !type || !key) {
        return;
    }
    NSString *path = [self getDefaultPathWithDataType:type withKey:key];
    BOOL isSucess;
    isSucess = [data writeToFile:path atomically:YES];
    complementBack(isSucess);
}
- (NSData *)getDataWithDataType:(JHLocalDataType)type andKey:(NSString *)key{
    if (!type || !key) {
        return nil;
    }
    NSString *path = [self getDefaultPathWithDataType:type withKey:key];
    NSData *data = [NSData dataWithContentsOfFile:path];
    return data;
}

/** string */

-(void)saveString:(NSString *)string withDataType:(JHLocalDataType)type WithKey:(NSString *)key withisSucess:(void (^)(BOOL))complementBack{
    if (string.length == 0 || !type || !key) {
        return;
    }
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    [self saveData:data withDataType:type WithKey:key withisSucess:^(BOOL isSucess) {
        complementBack(isSucess);
    }];
}

-(NSString *)getStringWithDataType:(JHLocalDataType)type andKey:(NSString *)key{
    if (!type || !key) {
        return nil;
    }
    NSData *data = [self getDataWithDataType:type andKey:key];
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return string;
}

/** array */

-(void)saveArray:(NSArray *)array withDataType:(JHLocalDataType)type WithKey:(NSString *)key withisSucess:(void (^)(BOOL))complementBack{
    if (!array || !type || !key) {
        return;
    }
    NSString *path = [self getDefaultPathWithDataType:type withKey:key];
    BOOL isSucess;
    isSucess = [array writeToFile:path atomically:YES];
    complementBack(isSucess);
}

- (NSArray *)getArrayWithDataType:(JHLocalDataType)type andKey:(NSString *)key{
    if (!type || !key) {
        return nil;
    }
    NSString *path = [self getDefaultPathWithDataType:type withKey:key];
    NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];
    return array;
}

/** dictionary */

- (void)saveDictionary:(NSDictionary *)dictionry withDataType:(JHLocalDataType)type WithKey:(NSString *)key withisSucess:(void (^)(BOOL))complementBack{
    if (!dictionry || !type || !key) {
        return;
    }
    NSString *path = [self getDefaultPathWithDataType:type withKey:key];
    BOOL isSucess;
    isSucess = [dictionry writeToFile:path atomically:YES];
    complementBack(isSucess);
}

-(NSDictionary *)getDictionaryWithDataType:(JHLocalDataType)type andKey:(NSString *)key{
    if (!type || !key) {
        return nil;
    }
    NSString *path  = [self getDefaultPathWithDataType:type withKey:key];
    NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:path];
    return dic;
}

/** image */

-(void)saveImage:(UIImage *)image withDataType:(JHLocalDataType)type WithKey:(NSString *)key withisSucess:(void (^)(BOOL))complementBack{
    if (!image || !type || !key) {
        return;
    }
    NSData *data = UIImagePNGRepresentation(image);
    [self saveData:data withDataType:JHLocalDataDocuments WithKey:key withisSucess:^(BOOL isSucess) {
        complementBack(isSucess);
    }];
}
-(UIImage *)getImageWithDataType:(JHLocalDataType)type andKey:(NSString *)key{
    if (!type || !key) {
        return nil;
    }
    NSData *data = [self getDataWithDataType:type andKey:key];
    UIImage *image = [UIImage imageWithData:data];
        return image;
}

/** object */

-(void)saveObject:(id)object withDataType:(JHLocalDataType)type WithKey:(NSString *)key withisSucess:(void (^)(BOOL))complementBack{
    if (!object || !type || !key) {
        return;
    }
    NSString *path = [self getDefaultPathWithDataType:type withKey:key];
    BOOL isSucess;
    isSucess = [NSKeyedArchiver archiveRootObject:object toFile:path];
    complementBack(isSucess);
}

- (id)getObjectWithDataType:(JHLocalDataType)type andKey:(NSString *)key{
    if (!type || !type) {
        return nil;
    }
    NSString *path = [self getDefaultPathWithDataType:type withKey:key];
    NSData *data = [NSData dataWithContentsOfFile:path];
    id object = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    return object;
}

路徑管理

#pragma mark - pathManager
- (NSString *)getDefaultPathWithDataType:(JHLocalDataType)type withKey:(NSString *)key{
    NSString *path;
    switch (type) {
        case JHLocalDataDocuments:
        {
            path = self.documentDefaultPath;
        }
            break;
        case JHLocalDataCache:
        {
            path = self.cacheDefaultPath;
        }
            break;
        case JHLocalDataTemp:
        {
            path = self.tempDefaultPath;
        }
            break;
        case JHLocalDataLibrary:
        {
            path = self.libraryDefaultPath;
        }
            break;
        case JHLocalDataPerferences:
        {
            path = self.perferenceDefaultPath;
        }
            break;
        default:
            break;
    }
    [self creatDataPathWith:type];
    return path ? [path stringByAppendingPathComponent:key] : @"";
}

/** makeAndSaveDefaultPath */
- (NSString *)getLocalDataDefaultPathWithOption:(JHLocalDataType)option{
    NSString *path;
    switch (option) {
        case JHLocalDataDocuments:
        {
            path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
        }
            break;
        case JHLocalDataCache:
        {
            path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
        }
            break;
        case JHLocalDataTemp:
        {
            path = NSTemporaryDirectory();
        }
            break;
        case JHLocalDataLibrary:
        {
            path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject;
        }
            break;
        case JHLocalDataPerferences:
        {
            path = NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, YES).firstObject;
        }
            break;
        default:
            break;
    }
    path ? [path stringByAppendingString:JHLocalDefaultPath] : @"";
    return path;
}

/** makeDocument */
- (void)creatDataPathWith:(JHLocalDataType)type{
    switch (type) {
        case JHLocalDataDocuments:
        {
            BOOL isExist;
            [self.fileManager fileExistsAtPath:self.documentDefaultPath isDirectory:&isExist];
            if (!isExist) {
                [self.fileManager createDirectoryAtPath:self.documentDefaultPath withIntermediateDirectories:YES attributes:nil error:nil];
            }
        }
            break;
            
        case JHLocalDataPerferences:
        {
            BOOL isExist;
            [self.fileManager fileExistsAtPath:self.perferenceDefaultPath isDirectory:&isExist];
            if (!isExist) {
                [self.fileManager createDirectoryAtPath:self.perferenceDefaultPath withIntermediateDirectories:YES attributes:nil error:nil];
            }
        }
            break;
        case JHLocalDataLibrary:
        {
            BOOL isExist;
            [self.fileManager fileExistsAtPath:self.libraryDefaultPath isDirectory:&isExist];
            if (!isExist) {
                [self.fileManager createDirectoryAtPath:self.libraryDefaultPath withIntermediateDirectories:YES attributes:nil error:nil];
            }
        }
            break;
        case JHLocalDataCache:
        {
            BOOL isExist;
            [self.fileManager fileExistsAtPath:self.cacheDefaultPath isDirectory:&isExist];
            if (!isExist) {
                [self.fileManager createDirectoryAtPath:self.cacheDefaultPath withIntermediateDirectories:YES attributes:nil error:nil];
            }
        }
            break;
        case JHLocalDataTemp:
        {
            BOOL isExist;
            [self.fileManager fileExistsAtPath:self.tempDefaultPath isDirectory:&isExist];
            if (!isExist) {
                [self.fileManager createDirectoryAtPath:self.tempDefaultPath withIntermediateDirectories:YES attributes:nil error:nil];
            }
        }
            break;
            
        default:
            break;
    }
}

Demo下載

參考

http://www.code4app.com/forum.php?mod=viewthread&tid=11657&extra=page%3D1%26filter%3Dsortid%26sortid%3D1
http://www.cocoachina.com/ios/20160729/17245.html

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末巍糯,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子客扎,更是在濱河造成了極大的恐慌祟峦,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,640評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件徙鱼,死亡現(xiàn)場離奇詭異宅楞,居然都是意外死亡,警方通過查閱死者的電腦和手機疆偿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評論 3 395
  • 文/潘曉璐 我一進店門咱筛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人杆故,你說我怎么就攤上這事「瘸睿” “怎么了处铛?”我有些...
    開封第一講書人閱讀 165,011評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長拐揭。 經(jīng)常有香客問我撤蟆,道長,這世上最難降的妖魔是什么堂污? 我笑而不...
    開封第一講書人閱讀 58,755評論 1 294
  • 正文 為了忘掉前任家肯,我火速辦了婚禮,結果婚禮上盟猖,老公的妹妹穿的比我還像新娘讨衣。我一直安慰自己,他們只是感情好式镐,可當我...
    茶點故事閱讀 67,774評論 6 392
  • 文/花漫 我一把揭開白布反镇。 她就那樣靜靜地躺著,像睡著了一般娘汞。 火紅的嫁衣襯著肌膚如雪歹茶。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,610評論 1 305
  • 那天,我揣著相機與錄音惊豺,去河邊找鬼燎孟。 笑死,一個胖子當著我的面吹牛尸昧,可吹牛的內(nèi)容都是我干的缤弦。 我是一名探鬼主播,決...
    沈念sama閱讀 40,352評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼彻磁,長吁一口氣:“原來是場噩夢啊……” “哼碍沐!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起衷蜓,我...
    開封第一講書人閱讀 39,257評論 0 276
  • 序言:老撾萬榮一對情侶失蹤累提,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后磁浇,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體斋陪,經(jīng)...
    沈念sama閱讀 45,717評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,894評論 3 336
  • 正文 我和宋清朗相戀三年置吓,在試婚紗的時候發(fā)現(xiàn)自己被綠了无虚。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,021評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡衍锚,死狀恐怖友题,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情戴质,我是刑警寧澤度宦,帶...
    沈念sama閱讀 35,735評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站告匠,受9級特大地震影響戈抄,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜后专,卻給世界環(huán)境...
    茶點故事閱讀 41,354評論 3 330
  • 文/蒙蒙 一划鸽、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧戚哎,春花似錦裸诽、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至啰脚,卻和暖如春殷蛇,著一層夾襖步出監(jiān)牢的瞬間实夹,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評論 1 270
  • 我被黑心中介騙來泰國打工粒梦, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留亮航,地道東北人。 一個月前我還...
    沈念sama閱讀 48,224評論 3 371
  • 正文 我出身青樓匀们,卻偏偏與公主長得像缴淋,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子泄朴,可洞房花燭夜當晚...
    茶點故事閱讀 44,974評論 2 355

推薦閱讀更多精彩內(nèi)容