#import <Foundation/Foundation.h>
@interface NSString (SHYUtil)
/*!
@method charValue
@abstract 把字符串转为char类型
@result 返回char
*/
- (char)charValue;
/*!
@method unsignedCharValue
@abstract 把字符串转为unsigned char类型
@result 返回unsigned char
*/
- (unsigned char)unsignedCharValue;
/*!
@method shortValue
@abstract 把字符串转为short类型
@result 返回short
*/
- (short)shortValue;
/*!
@method unsignedShortValue
@abstract 把字符串转为unsigned short类型
@result 返回unsigned short
*/
- (unsigned short)unsignedShortValue;
/*!
@method unsignedIntValue
@abstract 把字符串转为unsigned int类型
@result 返回unsigned int
*/
- (unsigned int)unsignedIntValue;
/*!
@method longValue
@abstract 把字符串转为long类型
@result 返回long
*/
- (long)longValue;
/*!
@method unsignedLongValue
@abstract 把字符串转为unsigned long类型
@result 返回unsigned long
*/
- (unsigned long)unsignedLongValue;
/*!
@method longLongValue
@abstract 把字符串转为long long int类型
@result 返回long long int
*/
- (long long int)longLongValue;
/*!
@method unsignedLongLongValue
@abstract 把字符串转为unsigned long long类型
@result 返回unsigned long long
*/
- (unsigned long long)unsignedLongLongValue;
/*!
@method unsignedIntegerValue
@abstract 把字符串转为NSUInteger类型
@result 返回NSUInteger
*/
- (NSUInteger)unsignedIntegerValue;
/*!
@method numberValue
@abstract 把字符串转为NSNumber类型
@result 返回NSNumber
*/
- (NSNumber *)numberValue;
/*!
@method arrayValue
@abstract 把字符串转为NSArray类型
@result 返回NSArray
*/
- (NSArray *)arrayValue;
/*!
@method dictionaryValue
@abstract 把字符串转为NSDictionary类型
@result 返回NSDictionary
*/
- (NSDictionary *)dictionaryValue;
/*!
@method dataValue
@abstract 把字符串转为NSData类型,只支持UTF-8有损转换;排UTF-8编码的请用 dataUsingEncoding: 方法转
@result 返回NSData
*/
- (NSData *)dataValue;
/*!
@method decimalValue
@abstract 把字符串转为NSDecimal类型
@result 返回NSDecimal
*/
- (NSDecimal)decimalValue;
/*!
@method decimalNumberValue
@abstract 把字符串转为NSDecimalNumber类型
@result 返回NSDecimalNumber
*/
- (NSDecimalNumber *)decimalNumberValue;
@end
#import "NSString+SHYUtil.h"
#import <CommonCrypto/CommonDigest.h>
#define SHY_CONVERT_USING(strtowhat) {
char buf[24];
if ([self getCString:buf maxLength:24 encoding:NSASCIIStringEncoding])
return strtowhat(buf, NULL, 10);
}
return strtowhat([self UTF8String], NULL, 10);
@implementation NSString (SHYUtil)
- (char)charValue
{
const char *str = [self UTF8String];
return str[0];
}
- (unsigned char)unsignedCharValue
{
const char *str = [self UTF8String];
return (unsigned char)str[0];
}
- (short)shortValue
{
int i = [self intValue];
return (short)i;
}
- (unsigned short)unsignedShortValue
{
return (unsigned short)[self unsignedLongValue];
}
- (unsigned int)unsignedIntValue
{
return (unsigned int)[self unsignedLongValue];
}
- (long)longValue
{
SHY_CONVERT_USING(strtol);
}
- (unsigned long)unsignedLongValue
{
SHY_CONVERT_USING(strtoul);
}
- (long long int)longLongValue
{
SHY_CONVERT_USING(strtoll);
}
- (unsigned long long)unsignedLongLongValue
{
SHY_CONVERT_USING(strtoull);
}
- (NSUInteger)unsignedIntegerValue
{
return (NSUInteger)[self unsignedLongValue];
}
- (NSNumber *)numberValue
{
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
return [formatter numberFromString:self];
}
- (NSArray *)arrayValue
{
return [NSArray arrayWithObject:self];
}
- (NSDictionary *)dictionaryValue
{
return [self propertyList];
}
- (NSData *)dataValue
{
return [self dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
}
- (NSDecimal)decimalValue
{
return [[NSDecimalNumber decimalNumberWithString:self] decimalValue];
}
- (NSDecimalNumber *)decimalNumberValue
{
return [NSDecimalNumber decimalNumberWithString:self];
}
@end
NSArray的扩展
@interface NSArray (SHYUtil)
/*!
@method anyObject
@abstract 获取数组里的一个对象,索引号小的优先返回
@result 返回数组里的一个对象
*/
- (id)anyObject;
/*!
@method isEmpty
@abstract 是否没有对象,没有对象也是为YES;
@result 返回BOOL
*/
- (BOOL)isEmpty;
/*!
@method objectAtIndexCheck:
@abstract 检查是否越界和NSNull如果是返回nil
@result 返回对象
*/
- (id)objectAtIndexCheck:(NSUInteger)index;
/*!
@method objectAtIndexCheck:class:defaultValue:
@abstract 获取指定index的对象并检查是否越界和NSNull如果是返回nil
@param index 索引号
@param aClass 检查类型
@result 返回对象
*/
- (id)objectAtIndexCheck:(NSUInteger)index class:(__unsafe_unretained Class)aClass;
/*!
@method objectAtIndexCheck:class:defaultValue:
@abstract 获取指定index的对象并检查是否越界和NSNull如果是返回nil,获取失败返回为指定的defaultValue
@param index 索引号
@param aClass 检查类型
@param defaultValue 获取失败要返回的值
@result 返回对象,获取失败为指定的defaultValue
*/
- (id)objectAtIndexCheck:(NSUInteger)index class:(__unsafe_unretained Class)aClass defaultValue:(id)defaultValue;
/*!
@method arrayAtIndex:
@abstract 获取指定index的NSArray类型值
@param index 索引号
@result 返回NSArray,获取失败为nil
*/
- (NSArray *)arrayAtIndex:(NSUInteger)index;
/*!
@method arrayAtIndex:defaultValue:
@abstract 获取指定index的NSArray类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回NSArray,获取失败为指定的defaultValue
*/
- (NSArray *)arrayAtIndex:(NSUInteger)index defaultValue:(NSArray *)defaultValue;
/*!
@method mutableArrayAtIndex:
@abstract 获取指定index的NSMutableArray类型值
@param index 索引号
@result 返回NSMutableArray,获取失败为nil
*/
- (NSMutableArray *)mutableArrayAtIndex:(NSUInteger)index;
/*!
@method mutableArrayAtIndex:defaultValue:
@abstract 获取指定index的NSMutableArray类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回NSMutableArray,获取失败为指定的defaultValue
*/
- (NSMutableArray *)mutableArrayAtIndex:(NSUInteger)index defaultValue:(NSArray *)defaultValue;
/*!
@method dictionaryAtIndex:
@abstract 获取指定index的NSDictionary类型值
@param index 索引号
@result 返回NSDictionary,获取失败为nil
*/
- (NSDictionary *)dictionaryAtIndex:(NSUInteger)index;
/*!
@method dictionaryAtIndex:defaultValue:
@abstract 获取指定index的NSDictionary类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回NSDictionary,获取失败为指定的defaultValue
*/
- (NSDictionary *)dictionaryAtIndex:(NSUInteger)index defaultValue:(NSDictionary *)defaultValue;
/*!
@method mutableDictionaryAtIndex:
@abstract 获取指定index的NSMutableDictionary类型值
@param index 索引号
@result 返回NSMutableDictionary,获取失败为nil
*/
- (NSMutableDictionary *)mutableDictionaryAtIndex:(NSUInteger)index;
/*!
@method mutableDictionaryAtIndex:defaultValue:
@abstract 获取指定index的NSMutableDictionary类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回NSMutableDictionary,获取失败为指定的defaultValue
*/
- (NSMutableDictionary *)mutableDictionaryAtIndex:(NSUInteger)index defaultValue:(NSDictionary *)defaultValue;
/*!
@method dataAtIndex:
@abstract 获取指定index的NSData类型值
@param index 索引号
@result 返回NSData,获取失败为nil
*/
- (NSData *)dataAtIndex:(NSUInteger)index;
/*!
@method dataAtIndex:defaultValue:
@abstract 获取指定index的NSData类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回NSData,获取失败为指定的defaultValue
*/
- (NSData *)dataAtIndex:(NSUInteger)index defaultValue:(NSData *)defaultValue;
/*!
@method stringAtIndex:
@abstract 获取指定index的NSString类型值
@param index 索引号
@result 返回NSString,获取失败为nil
*/
- (NSString *)stringAtIndex:(NSUInteger)index;
/*!
@method stringAtIndexToString:
@abstract 获取指定index的NSString类型值
@param index 索引号
@result 返回字NSString,获取失败为@""
*/
- (NSString *)stringAtIndexToString:(NSUInteger)index;
/*!
@method stringAtIndex:defaultValue:
@abstract 获取指定index的NSString类型值,获取失败返回为指定的defaultValue
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回NSString,获取失败为指定的defaultValue
*/
- (NSString *)stringAtIndex:(NSUInteger)index defaultValue:(NSString *)defaultValue;
/*!
@method numberAtIndex:
@abstract 获取指定index的NSNumber类型值
@param index 索引号
@result 返回NSNumber,获取失败为nil
*/
- (NSNumber *)numberAtIndex:(NSUInteger)index;
/*!
@method numberAtIndex:defaultValue:
@abstract 获取指定index的NSNumber类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回NSNumber,获取失败为指定的defaultValue
*/
- (NSNumber *)numberAtIndex:(NSUInteger)index defaultValue:(NSNumber *)defaultValue;
/*!
@method charAtIndex:
@abstract 获取指定index的NSNumber类型值
@param index 索引号
@result 返回char
*/
- (char)charAtIndex:(NSUInteger)index;
/*!
@method unsignedCharAtIndex:
@abstract 获取指定index的unsigned char类型值
@param index 索引号
@result 返回unsigned char
*/
- (unsigned char)unsignedCharAtIndex:(NSUInteger)index;
/*!
@method shortAtIndex:
@abstract 获取指定index的short类型值
@param index 索引号
@result 返回short,获取失败为0
*/
- (short)shortAtIndex:(NSUInteger)index;
/*!
@method shortAtIndex:defaultValue:
@abstract 获取指定index的short类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回short,获取失败为指定的defaultValue
*/
- (short)shortAtIndex:(NSUInteger)index defaultValue:(short)defaultValue;
/*!
@method unsignedShortAtIndex:
@abstract 获取指定index的unsigned short类型值
@param index 索引号
@result 返回unsigned short,获取失败为0
*/
- (unsigned short)unsignedShortAtIndex:(NSUInteger)index;
/*!
@method unsignedShortAtIndex:defaultValue:
@abstract 获取指定index的unsigned short类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回unsigned short,获取失败为指定的defaultValue
*/
- (unsigned short)unsignedShortAtIndex:(NSUInteger)index defaultValue:(unsigned short)defaultValue;
/*!
@method intAtIndex:
@abstract 获取指定index的int类型值
@param index 索引号
@result 返回int,获取失败为0
*/
- (int)intAtIndex:(NSUInteger)index;
/*!
@method intAtIndex:defaultValue:
@abstract 获取指定index的int类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回int,获取失败为指定的defaultValue
*/
- (int)intAtIndex:(NSUInteger)index defaultValue:(int)defaultValue;
/*!
@method unsignedIntAtIndex:
@abstract 获取指定index的unsigned int类型值
@param index 索引号
@result 返回unsigned int,获取失败为0
*/
- (unsigned int)unsignedIntAtIndex:(NSUInteger)index;
/*!
@method unsignedIntAtIndex:defaultValue:
@abstract 获取指定index的unsigned int类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回unsigned int,获取失败为指定的defaultValue
*/
- (unsigned int)unsignedIntAtIndex:(NSUInteger)index defaultValue:(unsigned int)defaultValue;
/*!
@method longAtIndex:
@abstract 获取指定index的long类型值
@param index 索引号
@result 返回long,获取失败为0
*/
- (long)longAtIndex:(NSUInteger)index;
/*!
@method longAtIndex:defaultValue:
@abstract 获取指定index的long类型值
@param defaultValue 获取失败要返回的值
@result 返回long,获取失败为指定的defaultValue
*/
- (long)longAtIndex:(NSUInteger)index defaultValue:(long)defaultValue;
/*!
@method unsignedLongAtIndex:
@abstract 获取指定index的unsigned long类型值
@param index 索引号
@result 返回unsigned long,获取失败为0
*/
- (unsigned long)unsignedLongAtIndex:(NSUInteger)index;
/*!
@method unsignedLongAtIndex:defaultValue:
@abstract 获取指定index的unsigned long类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回unsigned long,获取失败为指定的defaultValue
*/
- (unsigned long)unsignedLongAtIndex:(NSUInteger)index defaultValue:(unsigned long)defaultValue;
/*!
@method longLongAtIndex:
@abstract 获取指定index的long long类型值
@param index 索引号
@result 返回long long,获取失败为0
*/
- (long long)longLongAtIndex:(NSUInteger)index;
/*!
@method longLongAtIndex:defaultValue:
@abstract 获取指定index的long long类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回long long,获取失败为指定的defaultValue
*/
- (long long)longLongAtIndex:(NSUInteger)index defaultValue:(long long)defaultValue;
/*!
@method unsignedLongLongAtIndex:
@abstract 获取指定index的unsigned long long类型值
@param index 索引号
@result 返回unsigned long long,获取失败为0
*/
- (unsigned long long)unsignedLongLongAtIndex:(NSUInteger)index;
/*!
@method unsignedLongLongAtIndex:defaultValue:
@abstract 获取指定index的unsigned long long类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回unsigned long long,获取失败为指定的defaultValue
*/
- (unsigned long long)unsignedLongLongAtIndex:(NSUInteger)index defaultValue:(unsigned long long)defaultValue;
/*!
@method floatAtIndex:
@abstract 获取指定index的float类型值
@param index 索引号
@result 返回float,获取失败为0.0
*/
- (float)floatAtIndex:(NSUInteger)index;
/*!
@method floatAtIndex:defaultValue:
@abstract 获取指定index的float类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回float,获取失败为指定的defaultValue
*/
- (float)floatAtIndex:(NSUInteger)index defaultValue:(float)defaultValue;
/*!
@method doubleAtIndex:
@abstract 获取指定index的double类型值
@param index 索引号
@result 返回double,获取失败为0.0
*/
- (double)doubleAtIndex:(NSUInteger)index;
/*!
@method doubleAtIndex:defaultValue:
@abstract 获取指定index的double类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回double,获取失败为指定的defaultValue
*/
- (double)doubleAtIndex:(NSUInteger)index defaultValue:(double)defaultValue;
/*!
@method boolAtIndex:
@abstract 获取指定index的BOOL类型值
@param index 索引号
@result 返回BOOL,获取失败为NO
*/
- (BOOL)boolAtIndex:(NSUInteger)index;
/*!
@method boolAtIndex:
@abstract 获取指定index的BOOL类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回BOOL,获取失败为指定的defaultValue
*/
- (BOOL)boolAtIndex:(NSUInteger)index defaultValue:(BOOL)defaultValue;
/*!
@method integerAtIndex:
@abstract 获取指定index的NSInteger类型值
@param index 索引号
@result 返回NSInteger,获取失败为0
*/
- (NSInteger)integerAtIndex:(NSUInteger)index;
/*!
@method integerAtIndex:defaultValue:
@abstract 获取指定index的NSInteger类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回NSInteger,获取失败为指定的defaultValue
*/
- (NSInteger)integerAtIndex:(NSUInteger)index defaultValue:(NSInteger)defaultValue;
/*!
@method unsignedIntegerAtIndex:
@abstract 获取指定index的NSUInteger类型值
@param index 索引号
@result 返回NSUInteger,获取失败为0
*/
- (NSUInteger)unsignedIntegerAtIndex:(NSUInteger)index;
/*!
@method unsignedIntegerAtIndex:defaultValue:
@abstract 获取指定index的NSUInteger类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回NSUInteger,获取失败为指定的defaultValue
*/
- (NSUInteger)unsignedIntegerAtIndex:(NSUInteger)index defaultValue:(NSUInteger)defaultValue;
/*!
@method pointAtIndex:
@abstract 获取指定index的CGPoint类型值
@param index 索引号
@result 返回CGPoint,获取失败为CGPointZero
*/
- (CGPoint)pointAtIndex:(NSUInteger)index;
/*!
@method pointAtIndex:defaultValue:
@abstract 获取指定index的CGPoint类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回CGPoint,获取失败为指定的defaultValue
*/
- (CGPoint)pointAtIndex:(NSUInteger)index defaultValue:(CGPoint)defaultValue;
/*!
@method sizeAtIndex:
@abstract 获取指定index的CGSize类型值
@param index 索引号
@result 返回CGPoint,获取失败为CGSizeZero
*/
- (CGSize)sizeAtIndex:(NSUInteger)index;
/*!
@method sizeAtIndex:defaultValue:
@abstract 获取指定index的CGSize类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回CGSize,获取失败为指定的defaultValue
*/
- (CGSize)sizeAtIndex:(NSUInteger)index defaultValue:(CGSize)defaultValue;
/*!
@method rectAtIndex:
@abstract 获取指定index的CGRect类型值
@param index 索引号
@result 返回CGPoint,获取失败为CGRectZero
*/
- (CGRect)rectAtIndex:(NSUInteger)index;
/*!
@method rectAtIndex:
@abstract 获取指定index的CGRect类型值
@param index 索引号
@param defaultValue 获取失败要返回的值
@result 返回CGRect,获取失败为指定的defaultValue
*/
- (CGRect)rectAtIndex:(NSUInteger)index defaultValue:(CGRect)defaultValue;
@end
@implementation NSArray (SHYUtil)
- (id)anyObject
{
return [self count] > 0 ? [self objectAtIndexCheck:0] : nil;
}
- (BOOL)isEmpty
{
return ([self count] == 0);
}
- (id)objectAtIndexCheck:(NSUInteger)index
{
if (index >= [self count]) {
return nil;
}
id value = [self objectAtIndex:index];
if (value == [NSNull null]) {
return nil;
}
return value;
}
- (id)objectAtIndexCheck:(NSUInteger)index class:(__unsafe_unretained Class)aClass
{
return [self objectAtIndexCheck:index class:aClass defaultValue:nil];
}
- (id)objectAtIndexCheck:(NSUInteger)index class:(__unsafe_unretained Class)aClass defaultValue:(id)defaultValue
{
id value = [self objectAtIndexCheck:index];
if (![value isKindOfClass:aClass]) {
return defaultValue;
}
return value;
}
- (NSArray *)arrayAtIndex:(NSUInteger)index
{
return [self arrayAtIndex:index defaultValue:nil];
}
- (NSArray *)arrayAtIndex:(NSUInteger)index defaultValue:(NSArray *)defaultValue
{
return [self objectAtIndexCheck:index class:[NSArray class] defaultValue:defaultValue];
}
- (NSMutableArray *)mutableArrayAtIndex:(NSUInteger)index
{
return [self mutableArrayAtIndex:index defaultValue:nil];
}
- (NSMutableArray *)mutableArrayAtIndex:(NSUInteger)index defaultValue:(NSArray *)defaultValue
{
return [self objectAtIndexCheck:index class:[NSMutableArray class] defaultValue:defaultValue];
}
- (NSDictionary *)dictionaryAtIndex:(NSUInteger)index
{
return [self dictionaryAtIndex:index defaultValue:nil];
}
- (NSDictionary *)dictionaryAtIndex:(NSUInteger)index defaultValue:(NSDictionary *)defaultValue
{
return [self objectAtIndexCheck:index class:[NSDictionary class] defaultValue:defaultValue];
}
- (NSMutableDictionary *)mutableDictionaryAtIndex:(NSUInteger)index
{
return [self mutableDictionaryAtIndex:index defaultValue:nil];
}
- (NSMutableDictionary *)mutableDictionaryAtIndex:(NSUInteger)index defaultValue:(NSDictionary *)defaultValue
{
return [self objectAtIndexCheck:index class:[NSMutableDictionary class] defaultValue:defaultValue];
}
- (NSData *)dataAtIndex:(NSUInteger)index
{
return [self dataAtIndex:index defaultValue:nil];
}
- (NSData *)dataAtIndex:(NSUInteger)index defaultValue:(NSData *)defaultValue
{
return [self objectAtIndexCheck:index class:[NSData class] defaultValue:defaultValue];
}
- (NSString *)stringAtIndex:(NSUInteger)index
{
return [self stringAtIndex:index defaultValue:nil];
}
- (NSString *)stringAtIndexToString:(NSUInteger)index
{
return [self stringAtIndex:index defaultValue:@""];
}
- (NSString *)stringAtIndex:(NSUInteger)index defaultValue:(NSString *)defaultValue
{
id value = [self objectAtIndexCheck:index];
if ([value isKindOfClass:[NSNumber class]]) {
return [value stringValue];
}
if (![value isKindOfClass:[NSString class]]) {
return defaultValue;
}
return value;
}
- (NSNumber *)numberAtIndex:(NSUInteger)index
{
return [self numberAtIndex:index defaultValue:nil];
}
- (NSNumber *)numberAtIndex:(NSUInteger)index defaultValue:(NSNumber *)defaultValue
{
id value = [self objectAtIndexCheck:index];
if ([value isKindOfClass:[NSString class]]) {
return [value numberValue];
}
if (![value isKindOfClass:[NSNumber class]]) {
return defaultValue;
}
return value;
}
- (char)charAtIndex:(NSUInteger)index
{
id value = [self objectAtIndexCheck:index];
if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]]) {
return [value charValue];
}
else {
return 0x0;
}
}
- (unsigned char)unsignedCharAtIndex:(NSUInteger)index
{
id value = [self objectAtIndexCheck:index];
if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]]) {
return [value unsignedCharValue];
}
else {
return 0x0;
}
}
- (short)shortAtIndex:(NSUInteger)index
{
return [self shortAtIndex:index defaultValue:0];
}
- (short)shortAtIndex:(NSUInteger)index defaultValue:(short)defaultValue
{
id value = [self objectAtIndexCheck:index];
if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSString class]]) {
return [value shortValue];
}
else {
return defaultValue;
}
}
- (unsigned short)unsignedShortAtIndex:(NSUInteger)index
{
return [self unsignedShortAtIndex:index defaultValue:0];
}
- (unsigned short)unsignedShortAtIndex:(NSUInteger)index defaultValue:(unsigned