项目中常用的方法
2016-05-07 本文已影响32人
有思想的火柴
/*拼接post请求的网址/
- (NSString *)urlDictToStringWithUrlStr:(NSString *)urlStr WithDict:(NSDictionary *)parameters{
if (!parameters) {
return urlStr;
}
NSMutableArray *parts = [NSMutableArray array];
//enumerateKeysAndObjectsUsingBlock会遍历dictionary并把里面所有的key和value一组一组的展示给你,每组都会执行这个block 这其实就是传递一个block到另一个方法,在这个例子里它会带着特定参数被反复调用,直到找到一个ENOUGH的key,然后就会通过重新赋值那个BOOL *stop来停止运行,停止遍历同时停止调用block
[parameters enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
//接收key
NSString *finalKey = [key stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//接收值
NSString *finalValue = [obj stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSString *part =[NSString stringWithFormat:@"%@=%@",finalKey,finalValue];
[parts addObject:part];
}];
NSString *queryString = [parts componentsJoinedByString:@"&"];
queryString = queryString ? [NSString stringWithFormat:@"?%@",queryString] : @"";
NSString *pathStr = [NSString stringWithFormat:@"%@?%@",urlStr,queryString];
return pathStr;
}
/*检查是否包含特殊字符/
- (BOOL)isChineseCharacterAndLettersAndNumbersAndUnderScore:(NSString *)string
{
NSString *other = @"";
NSInteger len = string.length;
for(int i=0;i<len;i++)
{
unichar a=[string characterAtIndex:i];
if(!((isalpha(a))
||(isalnum(a))
||((a=='_'))
||((a >= 0x4e00 && a <= 0x9fa6))
||([other rangeOfString:string].location != NSNotFound)
||((a=='@'))
||((a=='#'))
))
return NO;
}
return YES;
}
/*蔡勒公式(根据年月日获得周几)/
- (NSString *)getWeek:(NSInteger)y month:(NSInteger)m day:(NSInteger)d{
NSInteger c = 0;
if (m ==1 || m ==2){
c = ( y - 1 ) / 100;
y = ( y - 1 ) % 100;
m = m + 12;
}else {
c = y / 100;
y = y % 100;
}
NSInteger iWeek = y + y / 4 + c / 4 - 2 * c + 26 * ( m + 1 ) / 10 + d - 1; //蔡勒公式
iWeek = iWeek >= 0 ? ( iWeek % 7 ) : ( iWeek % 7 + 7 ); //iWeek为负时取模
NSString *weekstr = @"";
switch(iWeek)
{
case 0: weekstr = @"星期日"; break;
case 1: weekstr = @"星期一"; break;
case 2: weekstr = @"星期二"; break;
case 3: weekstr = @"星期三"; break;
case 4: weekstr = @"星期四"; break;
case 5: weekstr = @"星期五"; break;
case 6: weekstr = @"星期六"; break;
}
return weekstr;
}
/*十六进制颜色转换/
+ (UIColor *)colorWithHexString:(NSString *)color
{
NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
if ([cString length] < 6) {
return [UIColor clearColor];
}
if ([cString hasPrefix:@"0X"])
cString = [cString substringFromIndex:2];
if ([cString hasPrefix:@"#"])
cString = [cString substringFromIndex:1];
if ([cString length] != 6)
{
return [UIColor clearColor];
}
NSRange range;
range.location = 0;
range.length = 2;
//r
NSString *rString = [cString substringWithRange:range];
//g
range.location = 2;
NSString *gString = [cString substringWithRange:range];
//b
range.location = 4;
NSString *bString = [cString substringWithRange:range];
// Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}
/*UIColor分类 随机色/
+(UIColor*) randomColor{
CGFloat hue = arc4random() % 256 / 256.0; //色调随机:0.0 ~ 1.0
CGFloat saturation = (arc4random() % 128 / 256.0) + 0.5; //饱和随机:0.5 ~ 1.0
CGFloat brightness = (arc4random() % 128 / 256.0) + 0.5; //亮度随机:0.5 ~ 1.0
return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}