mark 的总结开发笔记

2、播放音乐:
-( void ) playMusic
{
@try {
//取文件路径
NSString *musicFilePath = [[ NSBundle mainBundle ] pathForResource : @"startLogo" ofType : @"mp3" ];
NSURL *musicURL = [[ NSURL alloc ] initFileURLWithPath :musicFilePath];
musicPlayer = [[ AVAudioPlayer alloc ] initWithContentsOfURL :musicURL error : nil ];
[musicURL release ];
//[musicPlayer prepareToPlay];
//[musicPlayer setVolume:1]; //设置音量大小
musicPlayer . numberOfLoops = 0 ; //设置播放次数,-1为一直循环,0为一次
[ musicPlayer play ];
}
@catch ( NSException * e) {
}
}
******************************************************************************/
/******************************************************************************
3、每隔0.8秒执行 timeCount 方法
NSTimer * countTimer ;
countTimer = [ NSTimer scheduledTimerWithTimeInterval : 0.8 target : self selector : @selector ( timeCount :) userInfo : nil repeats : YES ];
[ countTimer fire ]; // 执行 timer
******************************************************************************/
/******************************************************************************
4、延迟1秒执行test方法:
[ self performSelector : @selector ( test ) withObject : nil afterDelay : 0.1 ];
******************************************************************************/
/******************************************************************************
5、启动线程:
[ NSThread detachNewThreadSelector : @selector ( transImage ) toTarget : self withObject : nil ];
timer =[ NSTimer scheduledTimerWithTimeInterval : 0.03 target : self selector : @selector ( TimerClock :) userInfo : nil repeats : YES ]; // 启动一个 NSTimer 执行广播
[ timer fire ]; //执行timer

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

-( void )transImage
{
isRunning = YES ;
while ( countTime < COUNTTIME ) {
[ timerCondition wait ];
lim += 255 / ( 2 * KFrame );
[ self processImage ];
countTime += 1000 / KFrame ;
}
[ timer invalidate ];
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);
BOOL success = [[ NSFileManager defaultManager ] createFileAtPath : filepath contents : nil attributes : 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 (![[ NSFileManager defaultManager ] fileExistsAtPath :themePath])
{
[[ NSFileManager defaultManager ] createDirectoryAtPath :themePath attributes : nil ];
}
//删除已存在的同名文件夹
if ([[ NSFileManager defaultManager ] fileExistsAtPath :savePath]) {
[[ NSFileManager defaultManager ] removeItemAtPath :savePath error : NULL ];
}
************************************************************************************************************************************************************/
/************************************************************************************************************************************************************
7子线程抛给主线程:
[ self performSelectorOnMainThread : @selector ( shiftView ) withObject : nil waitUntilDone : YES ];

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

//获取当前时间作为productId
NSDateFormatter *formatter = [[ NSDateFormatter alloc ] 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 = [[ NSDateFormatter alloc ] 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年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 = [ [ NSMutableDictionary alloc ] initWith

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

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

写入plist文件:
NSMutableDictionary * dict = [ [ NSMutableDictionary alloc ] initWithContentsOfFile : @"/Sample.plist" ];
[ dict setObject : @"Yes" forKey : @"RestartSpringBoard" ];
[ dict writeToFile : @"/Sample.plist" atomically : YES ];

读取plist文件:

NSMutableDictionary * dict = [ [ NSMutableDictionary alloc ] initWithContentsOfFile : @"/Sample.plist" ];
NSString * object = [ dict objectForKey : @"RestartSpringBoard" ];
************************************************************************************************************************************************************/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值