數(shù)據(jù)持久化:data persistent
存數(shù)據(jù):
-> 基本數(shù)據(jù)類型:NSString,NSArray,NSDictionary....
-> persistent持久化:把基本數(shù)據(jù)類型存儲的數(shù)據(jù)存儲到文件中
why?“永久”的保存基本的數(shù)據(jù)類型存儲數(shù)據(jù)到文件中:?memory內(nèi)存->disk磁盤
基本數(shù)據(jù)類型->數(shù)據(jù)容器(一般情況下會存在/Document/xxx);
實現(xiàn)方法:
-> NSUserDefaults
步驟:
1-》獲取單例對象NSUserDefaults*defaults = [NSUserDefaultsstandardUserDefaults];
2-》內(nèi)存-》磁盤(文件)[指定key,選方法]:使用set方法,給key dic[@“key”] = value;
[defaultssetBool:YESforKey:@"login"];
[defaultssetInteger:1forKey:@"count"];
NSArray*array =@[@"rose",@"jack"];
[defaultssetObject:arrayforKey:@"array"];
//強制把設(shè)置的值寫入文件中
[defaultssynchronize];
3->讀,通過key獲取存在文件中的數(shù)據(jù);
NSUserDefaults*defaults =[NSUserDefaultsstandardUserDefaults];
//根據(jù)不同的類型選擇不同的方法
BOOLisLogin = [defaultsboolForKey:@"login"];
NSIntegerinteger = [defaultsintegerForKey:@"count"];
NSArray*array = [defaultsarrayForKey:@"array"];
數(shù)據(jù)存儲到了 /Library/Perferences/BundleIdentifier.plist;
使用場景:不適合存儲“大量”的數(shù)據(jù)寫/讀
-> PList(Property List)屬性列表 ?(是一個特殊的xml)
Root根:type:NSArray/NSDictionary
寫:NSDictionary ->->writeToFile...
讀:根據(jù)root類型選擇接受數(shù)據(jù)的類型
//文件路徑
@property(nonatomic,strong)NSString*plistPath;
-(void)writeAndReadFromPList
{
NSString*doucumentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)firstObject];
self.plistPath= [doucumentPathstringByAppendingPathComponent:@"test.plist"];
NSDictionary*dic =@{@"name":@"rose",@"skills":@[@"objective-c",@"Ruby",@"Python"]};
[dicwriteToFile:self.plistPathatomically:YES];
//從指定的路徑讀取plist文件中得數(shù)據(jù)(root/寫入類型)
NSDictionary*readDic = [[NSDictionaryalloc]initWithContentsOfFile:self.plistPath];
//驗證
NSLog(@"讀取的數(shù)據(jù):%@",readDic);
}
-(void)createPListAndReadData
{
//獲取test.plist路徑
NSString*plistPath = [[NSBundlemainBundle]pathForResource:@"test"ofType:@"plist"];
//根據(jù)root類型接收收據(jù)
NSFileHandle*array = [NSFileHandlefileHandleForReadingAtPath:plistPath];
NSLog(@"%@",array);
NSArray*dataArray = [[NSArrayalloc]initWithContentsOfFile:plistPath];
for(NSDictionary*dicindataArray) {
NSLog(@"dic:%@",dic);
}
}
適用場景:
1.優(yōu)勢:一次性把plist所有的數(shù)據(jù)全都讀取出來(批量處理數(shù)據(jù))
2.缺點:只支持基本的數(shù)據(jù)類型(String/Data/Date/Array/Number/Dictionary);
-> 歸檔/解檔 :Archiving(理解)
前提:必須遵守NSCoding協(xié)議的任何類型,才可以用歸檔解檔
適用場景:不僅支持基本數(shù)據(jù)類型,也支持自定義類型;
//文件路徑
@property(nonatomic,strong)NSString*archivingFilePath;
-(NSString*)archivingFilePath
{
if(!_archivingFilePath)
{
NSString*documentPath = ? ? ? ? ? ? ? ? ? ? ?[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)firstObject];
_archivingFilePath= [documentPathstringByAppendingPathComponent:@"archivingFile"];
}
return_archivingFilePath;
}
步驟:1.歸檔(寫入的過程Archive)
-(void)writeDataByArchiving
{
//數(shù)據(jù)源
NSArray*array =@[@"Jonny",@18,@[@"swift",@"c"]];
//1創(chuàng)建一個可變的數(shù)據(jù)類型
NSMutableData*mutableData = [NSMutableDatadata];
NSLog(@"編碼前的數(shù)據(jù)長度:%ld",(unsignedlong)mutableData.length);
//2創(chuàng)建一個歸檔對象執(zhí)行寫入的動作
NSKeyedArchiver*archiver = [[NSKeyedArchiveralloc]initForWritingWithMutableData:mutableData];
//3對要存入的數(shù)據(jù)進行編碼,編碼的目的是保存為2進制的
[archiverencodeObject:arrayforKey:@"array"];
//4執(zhí)行一次完成編碼的操作
[archiverfinishEncoding];
NSLog(@"編碼后的數(shù)據(jù)長度:%ld",(unsignedlong)mutableData.length);
//5將編碼完的運算寫到文件里
[mutableDatawriteToFile:self.archivingFilePathatomically:YES];
}
2.解檔(讀取的過程Unarchive)
-(void)readDataByUnArchiving
{
//1從指定的文件中讀取數(shù)據(jù)
NSData*data = [NSDatadataWithContentsOfFile:self.archivingFilePath];
//2創(chuàng)建解檔對象
NSKeyedUnarchiver*unArchiver = [[NSKeyedUnarchiveralloc]initForReadingWithData:data];
//3對解檔對象進行解碼操作
NSArray*array = [unArchiverdecodeObjectForKey:@"array"];
//4執(zhí)行完成解碼
[unArchiverfinishDecoding];
//打印驗證
NSLog(@"%@",array);
自定義模型類步驟:
1 遵守NSCoding協(xié)議
2 實現(xiàn)兩個方法(編碼/解碼)
3 對模型類所有的屬性都要進行編碼/解碼
LQStudent.h
#import
@interfaceLQStudent :NSObject
@property(nonatomic,strong)NSString*name;
@property(nonatomic,assign)NSIntegerage;
//給定名字和年齡,返回一個已經(jīng)創(chuàng)建好的instance實例對象
-(id)initWithName:(NSString*)name addAge:(int)age;
@end
LQStudent.m
#import"LQStudent.h"
@implementationLQStudent
-(id)initWithName:(NSString*)name addAge:(int)age
{
self= [superinit];
if(self) {
//賦值
self.name= name;
self.age= age;
}
returnself;
}
#pragma mark- NSCoding
//時機:歸檔(編碼)調(diào)用該方法
- (void)encodeWithCoder:(NSCoder*)aCoder
{
//所有的屬性進行編碼操作
[aCoderencodeObject:self.nameforKey:@"name"];
[aCoderencodeInteger:self.ageforKey:@"age"];
NSLog(@"對屬性進行編碼");
}
//時機:解檔(解碼)調(diào)用該方法
- (id)initWithCoder:(NSCoder*)aDecoder
{
NSLog(@"對屬性進行解碼");
if(self= [superinit]) {
//所有的屬性進行解碼操作
self.name= [aDecoderdecodeObjectForKey:@"name"];
self.age= [aDecoderdecodeIntegerForKey:@"age"];
}
returnself;
}
@end
-> SQLite:數(shù)據(jù)庫(文件)
-> CoteDat
音頻處理
聲音文件的生成:
聲音采樣(2fs)->對聲音進行編碼操作(壓縮算法)->xxx.mp3等
讀取聲音文件的過程:
讀取xxx.mp3->解碼(解壓縮算法)->還原聲音數(shù)據(jù)->播放
聲音存儲的最小單位為:幀(Frame)內(nèi)容
幀內(nèi)容包括:幀頭;聲音數(shù)據(jù)
碼率BitRate:聲音壓縮質(zhì)量(越高越好) 128kbit/s;320kbit/s
IOS支持的音頻格式:xxx.mp3
包含兩個部分:文件格式(音頻容器)+數(shù)據(jù)格式(音頻編碼)
終端上的3個命令(了解):
MAC系統(tǒng)支持的音頻格式:afconvert -hf ? ? ?afinfo xxx.mp3
使用代碼播放音頻文件
功能1:播放音效(系統(tǒng)/自帶的)short audio
播放系統(tǒng)所提供的音頻/震動 AudioToolBox FrameWork(底層:C語言)
特點:<30s:音效; 不能暫停
功能2:AVFoundation FrameWork(指定音頻文件的播放) ?AVAudioPlayer(本地音頻播放)
#import
@interfaceViewController()
@property(nonatomic,strong)AVAudioPlayer*player;
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
//播放系統(tǒng)的聲音/震動/<30s音效
[selfplaySystemAudio];
//初始化palyer對象(音頻文件的路徑)
NSURL*fileURL = [[NSBundlemainBundle]URLForResource:@"AllOfMe"withExtension:@"mp3"];
self.player= [[AVAudioPlayeralloc]initWithContentsOfURL:fileURLerror:nil];
//播放
[self.playerplay];
}
-(void)playSystemAudio
{
/*前提:真機播放系統(tǒng)色聲音/震動
//1000~2000數(shù)字
AudioServicesPlaySystemSound(1600);
//震動
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
*/
//<30s的音頻文件
//獲取音頻文件的路徑
NSString*audioPath = [[NSBundlemainBundle]pathForResource:@"audio"ofType:@"wav"];
/*
URL(Uniform Resouce Locator:統(tǒng)一資源定位符)
*/
NSURL*audioURL = [NSURLfileURLWithPath:audioPath];
SystemSoundIDsystemID;
AudioServicesCreateSystemSoundID((__bridgeCFURLRef)(audioURL), &systemID);
//播放
AudioServicesPlaySystemSound(systemID);
}
@end