您的位置:首页 > 移动开发 > IOS开发

渣蜀黍 - iOS 个人笔记(一)_数据处理

2015-04-13 11:07 477 查看
//获得沙盒路径

+(NSString *)getDocumentPath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
return path;
}

1.分割字符串

NSArray *array = [@"这里填写被分割的字符" componentsSeparatedByString:@"这里填写需要以什么为中心的符号或字符"];
NSString *rowStr=[array objectAtIndex:0];

2.替换字符串中的某个字符

NSString *trimmedString=[@"被执行操作的字符串" stringByReplacingOccurrencesOfString:@"要替换的字符串" withString:@"替换成的字符串"];

3.字符串后面追加字符

NSString *nsStr=[NSString stringWithFormat:@"原字符串%@",@"添加的字符"];
NSString *nsStr=[@"原字符串" stringByAppendingString:@"添加的字符"];
NSString *nsStr=[@"原文件夹名" stringByAppendingPathComponent:@"文件夹名"];//针对文件夹名,可省略 / 的符号


4.判断文件夹是否存在,如果不存在则创建,否则不创建

NSFileManager *fileManage = [NSFileManager defaultManager];
//先判断这个文件夹是否存在,如果不存在则创建,否则不创建
if (![fileManage fileExistsAtPath:DocumentPath]) {
[fileManage createDirectoryAtPath:DocumentPath withIntermediateDirectories:YES attributes:nil error:nil];
}

//判断文件是否存在
NSFileManager *fileManage = [NSFileManager defaultManager];
if([fileManage fileExistsAtPath:@"文件路径"]){
NSLog(@"文件存在");
}

[fileManage removeItemAtPath:@"文件路径"error:nil];//删除文件


5.解决URL 中文字的处理

UrlString=[UrlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];


6.判断字符串是不是为空

+ (BOOL)isBlankString:(NSString *)string{

if (string == nil) {

return YES;

}

if (string == NULL) {

return YES;

}

if ([string isKindOfClass:[NSNull class]]) {

return YES;

}

if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0) {

return YES;

}

return NO;

}


7.NSMutableString 字符串拼接

NSMutableString *postParameter= [NSMutableString string];
[postParameter appendString:@"&postUrl="];
[postParameter appendString:@"url"];


8.中文字排序

+(NSMutableArray *)paixu_jidu:(NSMutableArray *)mArray{
NSDictionary *numberMapping=@{@"一":@1,@"二":@2,@"三":@3,@"四":@4};

NSMutableArray *temp = [NSMutableArray arrayWithCapacity:4];
NSMutableArray *chinesenum = [NSMutableArray arrayWithCapacity:4];
for(int i=0;i<[mArray count];i++){
NSString *chinese_1=[self getChinesenum:[mArray objectAtIndex:i]];
[temp addObject:chinese_1];
}

NSArray *arrSorted =[temp sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2){
int num1=[numberMapping[obj1] intValue];
int num2=[numberMapping[obj2] intValue];

if(num1>num2){
return NSOrderedAscending;
}else if(num1 <num2){
return NSOrderedDescending;
}else{
return NSOrderedSame;
}
}];
for (NSString *num in arrSorted) {
NSString *ji=[num stringByAppendingString:@"季"];
[chinesenum addObject:ji];
}

return chinesenum;
}
+(NSString *)getChinesenum:(NSString *)dataString{
NSString *Chinesenum=nil;
NSArray * Chinesenumname=[NSArray arrayWithObjects:@"一",@"二",@"三",@"四", nil];
for(int i=0;i<[Chinesenumname count];i++){
NSRange range = [dataString rangeOfString:[Chinesenumname objectAtIndex:i]];
if(range.location!=NSNotFound){
Chinesenum=[Chinesenumname objectAtIndex:i];
}

}
return Chinesenum;
}
//排序 例:2014年1~12月</span>
+(NSMutableArray *)paixu:(NSMutableArray *)monthnum{

NSString * yeas=@"";
for (int i = 0; i<[monthnum count]; i++)

{

for (int j=i+1; j<[monthnum count]; j++)

{

NSString *month_1=[self getmonthsname:[monthnum objectAtIndex:i]];

yeas=[self getyeas:[monthnum objectAtIndex:i]];

NSMutableString *str_1 = [NSMutableString stringWithString:month_1];
[str_1 deleteCharactersInRange: [str_1 rangeOfString: @"月"]];

NSString *month_2=[self getmonthsname:[monthnum objectAtIndex:j]];
NSMutableString *str_2 = [NSMutableString stringWithString:month_2];
[str_2 deleteCharactersInRange: [str_2 rangeOfString: @"月"]];
int a = [str_1 intValue];

int b = [str_2 intValue];

if (a < b)

{

[monthnum replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%@%d月",yeas,b]];

[monthnum replaceObjectAtIndex:j withObject:[NSString stringWithFormat:@"%@%d月",yeas,a]];

}

}
}
return monthnum;
}
//年份排序 个人理解 ,有更好的方法欢迎留言谈论,谢谢
+(NSMutableArray *)paixu_yeas:(NSMutableArray *)mArray{
for (int i =0; i<[mArray count]; i++)

{
for (int j=i+1; j<[mArraycount]; j++)

{

NSString *yeas=[selfgetyeas:[mArray objectAtIndex:i]];

NSMutableString *str_1 = [NSMutableStringstringWithString:yeas];
[str_1 deleteCharactersInRange: [str_1rangeOfString: @"年"]];

NSString *yeas_2=[selfgetyeas:[mArray objectAtIndex:j]];

NSMutableString *str_2 = [NSMutableStringstringWithString:yeas_2];
[str_2 deleteCharactersInRange: [str_2rangeOfString: @"年"]];
int a = [str_1intValue];

int b = [str_2intValue];

if (a < b)

{

[mArray replaceObjectAtIndex:iwithObject:[NSStringstringWithFormat:@"%d年",b]];

[mArray replaceObjectAtIndex:jwithObject:[NSStringstringWithFormat:@"%d年",a]];

}

}
}
return mArray;
}


9.获得年   月  

//获得年份
+(NSString *)getyeas:(NSString *)dataString{
NSString *yeas=@"";

NSDateFormatter *nsdf2=[[NSDateFormatter alloc] init];
[nsdf2 setDateStyle:NSDateFormatterShortStyle];
[nsdf2 setDateFormat:@"YYYY"];
NSString *date=[nsdf2 stringFromDate:[NSDate date]];
int leng=[date intValue];
NSMutableArray *muArray = [NSMutableArray arrayWithCapacity:leng-2002];

for(int i=2002;i<leng+1;i++){
NSString *int_i = [NSString stringWithFormat:@"%d", i];
NSStr
4000
ing *n=@"年";
NSString *nian = [int_i stringByAppendingString:n];

[muArray addObject:nian];

for(int i=0;i<[muArray count];i++){
NSRange range = [dataString rangeOfString:[muArray objectAtIndex:i]];
if(range.location!=NSNotFound){
yeas=[muArray objectAtIndex:i];
}

}

}
return yeas;
}

//获得月份
+(NSString *)getmonthsname:(NSString *)dataString{
NSString *month=nil;
NSArray * monthsname=[NSArray arrayWithObjects:@"1月",@"2月",@"3月",@"4月",@"5月",@"6月",@"7月",@"8月",@"9月",@"10月",@"11月",@"12月", nil];

for(int i=0;i<[monthsname count];i++){
NSRange range = [dataString rangeOfString:[monthsname objectAtIndex:i]];
if(range.location!=NSNotFound){
month=[monthsname objectAtIndex:i];
}

}
return month;
}

//获得最新的一年
+(NSMutableArray *)getnewyeas{

NSDateFormatter *nsdf2=[[NSDateFormatter alloc] init];
[nsdf2 setDateStyle:NSDateFormatterShortStyle];
[nsdf2 setDateFormat:@"YYYY"];
NSString *date=[nsdf2 stringFromDate:[NSDate date]];
int leng=[date intValue];
NSMutableArray *yeas=[NSMutableArray arrayWithCapacity:leng-2009];
for(int i=leng;i>2009;i--){
[yeas addObject:[NSString stringWithFormat:@"%d年",i]];
}
return yeas;
}


10.取数组里的最大值和最小值
//获得最大
+(CGFloat)FindMaximumNumber: (NSMutableArray *) array{

CGFloat _max=[[array objectAtIndex:0] floatValue];

for(int i=1;i<[array count];i++)
{
if([[array objectAtIndex:i] floatValue]>_max){
_max = [[array objectAtIndex:i] floatValue];
}
}
return _max;
}

//获得最小
+(CGFloat)FindMinimumNumber:(NSMutableArray *) array{
CGFloat _min=[[array objectAtIndex:0] floatValue];

for(int i=1;i<[array count];i++)
{
if([[array objectAtIndex:i] floatValue]<_min){
_min = [[array objectAtIndex:i] floatValue];
}
}

return _min;

}


11.获得文件的md5值    

+ (NSString *)fileMD5:(NSString *)filePath
{
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
if(!handle)
{
return nil;
}

CC_MD5_CTX md5;
CC_MD5_Init(&md5);
BOOL done = NO;
while (!done)
{
NSData *fileData = [handle readDataOfLength:256];
CC_MD5_Update(&md5, [fileData bytes], [fileData length]);
if([fileData length] == 0)
done = YES;
}
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5_Final(digest, &md5);

NSString *result = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
digest[0], digest[1],
digest[2], digest[3],
digest[4], digest[5],
digest[6], digest[7],
digest[8], digest[9],
digest[10], digest[11],
digest[12], digest[13],
digest[14], digest[15]];

return result;
}


======================================================================================================================================
个人观点,有更好方法欢迎留言

By:渣蜀黍                                           

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息