mark 的总结开发笔记-备

2、播放音乐:
-(void) playMusic
{
@try{
//取文件路径
NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:@"startLogo" ofType:@"mp3"];       
NSURL *musicURL = [[NSURL alloc] initFileURLWithPath:musicFilePath];  
musicPlayer= [[AVAudioPlayeralloc] initWithContentsOfURL:musicURL error:nil];
[musicURL release];
//[musicPlayer prepareToPlay];
//[musicPlayer setVolume:1];            //设置音量大小
musicPlayer.numberOfLoops= 0; //设置播放次数,-1为一直循环,0为一次
[musicPlayerplay]; 
}
@catch(NSException* e) {
}

******************************************************************************/
/******************************************************************************
3、每隔0.8秒执行timeCount方法:
NSTimer*countTimer;
countTimer= [NSTimerscheduledTimerWithTimeInterval: 0.8target: selfselector: @selector(timeCount:)  userInfo: nilrepeats: YES];   
[countTimerfire];     //执行timer
******************************************************************************/
/******************************************************************************
4、延迟1秒执行test方法:
[selfperformSelector:@selector(test) withObject:nilafterDelay:0.1];
******************************************************************************/
/******************************************************************************
5、启动线程:
[NSThreaddetachNewThreadSelector:@selector(transImage) toTarget:selfwithObject:nil]; 
timer=[NSTimerscheduledTimerWithTimeInterval:0.03target:selfselector:@selector(TimerClock:) userInfo:nilrepeats:YES]; //启动一个NSTimer执行广播
[timerfire];  //执行timer

-(void)TimerClock:(id)sender
{
//控制延迟触发
if(Timecontrol>1) {   
[timerConditionbroadcast];      //广播,触发处于等待状态的timerCondition

}

-(void)transImage

isRunning=YES;
while (countTime < COUNTTIME) {
[timerConditionwait];
lim += 255 / (2 * KFrame);
[selfprocessImage];
countTime += 1000 / KFrame;
}
[timerinvalidate];
isRunning=NO;
}
******************************************************************************/
/******************************************************************************
6、获取文件路径:
//通过NSHomeDirectory获得文件路径
NSString *homeDirectory = NSHomeDirectory();
NSString *fileDirectory = [homeDirectory stringByAppendingPathComponent:@"temp/app_data.plist"];

//使用NSSearchPathForDirectoriesInDomains检索指定路径
NSArray*path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//常量NSDocumentDirectory表示正在查找Documents目录的路径(使用NSCachesDirectory表明要查找的时Caches文件夹),常量NSUserDomainMask表明我们希望将搜索限制于我们应用程序的沙盒,最后一个参数决定了是否“展开”波浪线符号。
//在Mac系统中,‘~’表示主路经(Home),如果不展开,路径看起来就是:‘~/Documents’,展开后即得到完整路径。这个参数一直设置位真即可。
NSString *documentsDirectory = [paths objectAtIndex:0];z
NSString *fileDirectory = [documentsDirectory stringByAppendingPathComponent:@"file.txt"];

//使用Foundation中的NSTemporaryDirectory函数直接返回代表temp文件夹的全路径的字符串对象
NSString *tempDirectory = NSTemporaryDirectory();
NSString*file = [tempDirectory stringByAppendingPathComponent:@"file.txt"];


  Example:
NSArray*path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *docDir = [path objectAtIndex:0];
NSLog(@"filepath:%@",docDir);
NSString*str = @"hello.jpg";
NSString*filepath = [docDir stringByAppendingPathComponent:str];
//NSString *filepath = [docDir stringByAppendingPathComponent:[NSString stringWithUTF8String:"///mest.txt"]];
NSLog(@"filepath:%@",filepath);
BOOLsuccess = [[NSFileManagerdefaultManager]createFileAtPath: filepath contents:nilattributes:nil];
NSLog(@"result",success);
printf("Create File:%s %s.",[filepath UTF8String], success ? "Success": "Error");

NSString* reValue= [NSString stringWithString:@""success""];
NSLog(reValue);
******************************************************************************/
/************************************************************************************************************************************************************
7文件、文件夹操作
//如果"/Documents/Theme"路径不存在,则创建。
if(![[NSFileManagerdefaultManager]fileExistsAtPath:themePath])
{
[[NSFileManagerdefaultManager] createDirectoryAtPath:themePath attributes:nil];
}
//删除已存在的同名文件夹
if([[NSFileManagerdefaultManager] fileExistsAtPath:savePath]) {
[[NSFileManagerdefaultManager] removeItemAtPath:savePath error:NULL];
}
************************************************************************************************************************************************************/
/************************************************************************************************************************************************************
7 子线程抛给主线程:
[selfperformSelectorOnMainThread:@selector(shiftView) withObject:nilwaitUntilDone:YES];

************************************************************************************************************************************************************/
/************************************************************************************************************************************************************
8获取当前时间
NSDateFormatter*formatter = [[NSDateFormatteralloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *locationString=[formatter stringFromDate: [NSDate date]];

//获取当前时间作为productId
NSDateFormatter*formatter = [[NSDateFormatteralloc] init];
[formatter setDateFormat:@"hhmmss"];
NSString *locationString=[formatter stringFromDate: [NSDate date]];
downloadInfo.productId = locationString;
[formatter release];
/******************************************************************************
 函数名称  : getDate
函数描述  : 获取当前日期时间
 输入参数  : N/A
 输出参数  : N/A
 返回值    : NSString 当前时间
 备注     :
 ******************************************************************************/
-(NSString *)getDate
{
NSDateFormatter*formatter = [[NSDateFormatteralloc] init];
[formatter setDateFormat:@"yyyy-MM-dd EEEE HH:mm:ss a"];
NSString *locationString=[formatter stringFromDate: [NSDate date]];
[formatter release];
return locationString;
}
大写的H日期格式将默认为24小时制,小写的h日期格式将默认为12小时
不需要特别设置,只需要在dataFormat里设置类似"yyyy-MMM-dd"这样的格式就可以了
日期格式如下:
y  年  Year  1996; 96  
M  年中的月份  Month  July; Jul; 07  
w  年中的周数  Number  27  
W  月份中的周数  Number  2  
D  年中的天数  Number  189  
d  月份中的天数  Number  10  
F  月份中的星期  Number  2  
E  星期中的天数  Text  Tuesday; Tue  
a  Am/pm 标记  Text  PM  
H  一天中的小时数(0-23)  Number  0  
k  一天中的小时数(1-24)  Number  24  
K  am/pm 中的小时数(0-11)  Number  0  
h  am/pm 中的小时数(1-12)  Number  12  
m  小时中的分钟数  Number  30  
s  分钟中的秒数  Number  55  
S  毫秒数  Number  978  
z  时区  General time zone  Pacific Standard Time; PST; GMT-08:00  
Z  时区  RFC 822 time zone  -0800
************************************************************************************************************************************************************/
/************************************************************************************************************************************************************
读取和写入plist文件

plist文件是标准的xml文件,在cocoa中可以很简单地使用。这里介绍一下使用方法: 以下代码在Mac和iPhone中均适用。 
写入plist文件: NSMutableDictionary * dict = [ [ NSMutableDictionary alloc ] initWith 
  
plist文件是标准的xml文件,在cocoa中可以很简单地使用。这里介绍一下使用方法:    
   
以下代码在Mac和iPhone中均适用。
   
写入plist文件:  
NSMutableDictionary* dict = [ [ NSMutableDictionaryalloc ] initWithContentsOfFile:@"/Sample.plist"];
[ dict setObject:@"Yes"forKey:@"RestartSpringBoard"];
[ dict writeToFile:@"/Sample.plist"atomically:YES];
   
读取plist文件:
   
NSMutableDictionary* dict =  [ [ NSMutableDictionaryalloc ] initWithContentsOfFile:@"/Sample.plist"];
NSString* object = [ dict objectForKey:@"RestartSpringBoard" ];

原文地址:https://www.cnblogs.com/isItOk/p/5228114.html