---
title: "mark 的总结开发笔记"
author: "feipigwang"
date: "2011-11-04T14:59:00Z"
tags: []
slug: "mark"
draft: false
lastmod: "2011-11-04T16:40:12Z"
id: 1235632
url: "https://feipigwang.iteye.com/blog/1235632"
---

2、播放音乐：  
\-(void) playMusic  
{  
@try{  
//取文件路径  
NSString\*musicFilePath = \[\[NSBundlemainBundle]pathForResource:@"startLogo"ofType:@"mp3"];  
NSURL\*musicURL = \[\[NSURLalloc]initFileURLWithPath:musicFilePath];  
musicPlayer= \[\[AVAudioPlayeralloc]initWithContentsOfURL:musicURLerror:nil];  
\[musicURLrelease];  
//\[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&gt;1) {  
\[timerConditionbroadcast]; //广播，触发处于等待状态的timerCondition  
}  
}

-(void)transImage  
{  
isRunning=YES;  
while(countTime&lt;COUNTTIME) {  
\[timerConditionwait];  
lim+=255/ (2\*KFrame);  
\[selfprocessImage];  
countTime+=1000/KFrame;  
}  
\[timerinvalidate];  
isRunning=NO;  
}  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
6、获取文件路径：  
//通过NSHomeDirectory获得文件路径  
NSString\*homeDirectory =NSHomeDirectory();  
NSString\*fileDirectory = \[homeDirectorystringByAppendingPathComponent:@"temp/app\_data.plist"];

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

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

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

NSString* reValue= \[NSStringstringWithString:@"\\"success\\""];  
NSLog(reValue);  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
7文件、文件夹操作  
//如果"/Documents/Theme"路径不存在，则创建。  
if(!\[\[NSFileManagerdefaultManager]fileExistsAtPath:themePath])  
{  
\[\[NSFileManagerdefaultManager]createDirectoryAtPath:themePathattributes:nil];  
}  
//删除已存在的同名文件夹  
if(\[\[NSFileManagerdefaultManager]fileExistsAtPath:savePath]) {  
\[\[NSFileManagerdefaultManager]removeItemAtPath:savePatherror:NULL];  
}  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*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\*\*\*\*\*\*\*\*\*\*\*\*\**  
8获取当前时间  
NSDateFormatter\*formatter = \[\[NSDateFormatteralloc]init];  
\[formattersetDateFormat:@"yyyy-MM-dd hh:mm:ss"];  
NSString\*locationString=\[formatterstringFromDate: \[NSDatedate]];

//获取当前时间作为productId  
NSDateFormatter\*formatter = \[\[NSDateFormatteralloc]init];  
\[formattersetDateFormat:@"hhmmss"];  
NSString\*locationString=\[formatterstringFromDate: \[NSDatedate]];  
downloadInfo.productId = locationString;  
\[formatterrelease];  
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
函数名称 : getDate  
函数描述 : 获取当前日期时间  
输入参数 : N/A  
输出参数 : N/A  
返回值 : NSString 当前时间  
备注 :  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
\-(NSString\*)getDate  
{  
NSDateFormatter\*formatter = \[\[NSDateFormatteralloc]init];  
\[formattersetDateFormat:@"yyyy-MM-dd EEEE HH:mm:ss a"];  
NSString\*locationString=\[formatterstringFromDate: \[NSDatedate]];  
\[formatterrelease];  
returnlocationString;  
}  
大写的H日期格式将默认为24小时制，小写的h日期格式将默认为12小时  
不需要特别设置，只需要在dataFormat里设置类似"yyyy-MMM-dd"这样的格式就可以了  
日期格式如下:  
y年Year1996; 96  
M年中的月份MonthJuly; Jul; 07  
w年中的周数Number27  
W月份中的周数Number2  
D年中的天数Number189  
d月份中的天数Number10  
F月份中的星期Number2  
E星期中的天数TextTuesday; Tue  
aAm/pm 标记TextPM  
H一天中的小时数（0-23）Number0  
k一天中的小时数（1-24）Number24  
Kam/pm 中的小时数（0-11）Number0  
ham/pm 中的小时数（1-12）Number12  
m小时中的分钟数Number30  
s分钟中的秒数Number55  
S毫秒数Number978  
z时区General time zonePacific Standard Time; PST; GMT-08:00  
Z时区RFC 822 time zone-0800  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
读取和写入plist文件

plist文件是标准的xml文件，在cocoa中可以很简单地使用。这里介绍一下使用方法： 以下代码在Mac和iPhone中均适用。  
写入plist文件： NSMutableDictionary * dict = \[ \[NSMutableDictionaryalloc ] initWith

plist文件是标准的xml文件，在cocoa中可以很简单地使用。这里介绍一下使用方法：

以下代码在Mac和iPhone中均适用。

写入plist文件：  
NSMutableDictionary* dict = \[ \[NSMutableDictionaryalloc ]initWithContentsOfFile:@"/Sample.plist"];  
\[ dictsetObject:@"Yes"forKey:@"RestartSpringBoard"];  
\[ dictwriteToFile:@"/Sample.plist"atomically:YES];

读取plist文件：

NSMutableDictionary* dict = \[ \[NSMutableDictionaryalloc ]initWithContentsOfFile:@"/Sample.plist"];  
NSString* object = \[ dictobjectForKey:@"RestartSpringBoard"];  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/