Objective-C 学习笔记
2013-02-26 15:54
169 查看
1。隐藏状态栏[[UIApplication
sharedApplication]
setStatusBarHidden:YES];
/******************************************************************************
1、取随机数:
NSData *datanow = [NSData
data];
int i = (int)datanow;
srand(i);
rand();
//int effectPicNum = rand()%7;
******************************************************************************/
/******************************************************************************
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" ];
************************************************************************************************************************************************************/
sharedApplication]
setStatusBarHidden:YES];
/******************************************************************************
1、取随机数:
NSData *datanow = [NSData
data];
int i = (int)datanow;
srand(i);
rand();
//int effectPicNum = rand()%7;
******************************************************************************/
/******************************************************************************
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" ];
************************************************************************************************************************************************************/
相关文章推荐
- Objective-C基础学习笔记(五)-面向对象的三大特性之多态
- objective-c 简要学习笔记
- Programming in Objective-C 学习笔记06 —— Preprocessor
- Objective-C 学习笔记 - part 7 - 相关引用
- Objective-C学习笔记:数据类型和表达式
- 学习笔记之01-Objective-C概述
- 【初学】Objective-C学习笔记-委托代理(delegate)和协议(protocol)
- Objective-C学习笔记(一)——OC语言的特点
- 2011斯坦福大学iOS应用开发教程学习笔记(第一课)MVC.and.Introduction.to.Objective-C
- 斯坦福大学iOS应用开发教程学习笔记(第三课)Objective-C
- Objective-C学习笔记(十)——循环语句for和do-while的使用
- iOS学习笔记-Objective-C-继承和实例化、对象初始化
- Objective-c 语法,类/属性/函数(iOS学习笔记,从零开始。)
- Objective-C学习笔记1--windows+GNUstep环境下不能使用@autoreleasepool?
- Objective-C学习笔记(二十三)——面向对象之封装
- 第二章、Objective-c 语法,继承,protocol和delegate(iOS学习笔记,从零开始)
- 【Objective-C学习笔记】变量和基本的数据类型
- Objective-C学习笔记2:类、对象和方法
- 《objective-c 2.0 程序设计》学习笔记
- Objective-c学习笔记(一)oc的基本语法