#import <Foundation/Foundation.h>

UIColor * rgb(int r, int g, int b);
UIColor * rgbA(int r, int g, int b, float a);
UIColor * rgbweb(int rgbval); @interface NSObject (Utilty) // 读文件
-(NSString*)readApiCacheFileName:(NSString*)fileName; // 写文件
-(void)saveApiCacheFileName:(NSString*)fileName textContent:(NSString*)content Append:(BOOL)isAppend; // 字符串过滤不规则字符
-(NSString*)filterString:(NSString*)aString filterWith:(NSString*)fStr; /*
* 检查输入的金额是否符合金额的输入规则
*
*/
-(bool)doCheckInputAmount:(NSString *)textFieldtext replacementString:(NSString *)string; @end @interface NSDictionary (extra) //根据key值的到字典中的object
- (id)getObjectByKey:(NSString*)key; //根据key值得到字符串,如为空则返回@“”
- (id)getStringByKey:(NSString*)key; //根据key值把字典中的list字典转换成数组
- (NSMutableArray*)getArrayByString:(NSString*)string; - (NSArray*)sortedKeysByString:(NSString*)elementName; @end @interface NSString (chnbs) -(void)saveApiCacheFileName:(NSString*)fileName; @end
#import "NSObject+Utilty.h"

//定义颜色
UIColor * rgb(int r, int g, int b)
{
return [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:];
} UIColor * rgbA(int r, int g, int b, float a)
{
return [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a];
} UIColor * rgbweb(int rgbval)
{
return rgb((rgbval>>)&0xFF,(rgbval>>)&0xFF,(rgbval>>)&0xFF);
} @implementation NSObject (Utilty) //读取保存文件。
-(NSString*)readApiCacheFileName:(NSString*)fileName{
NSString * cacheFolder = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
cacheFolder = [cacheFolder stringByAppendingPathComponent:@"JiangSu"]; //获取文件路径
NSString *path = [cacheFolder stringByAppendingPathComponent:fileName]; return [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
} /*
* @fileName 需要保存的文件名字
*/
-(void)saveApiCacheFileName:(NSString*)fileName textContent:(NSString*)content Append:(BOOL)isAppend{ NSString * cacheFolder = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
cacheFolder = [cacheFolder stringByAppendingPathComponent:@"JiangSu"]; NSFileManager * fmgr = [NSFileManager defaultManager]; if (![fmgr fileExistsAtPath:cacheFolder])
{
NSError * error;
[fmgr createDirectoryAtPath:cacheFolder withIntermediateDirectories:YES attributes:nil error:&error];
}
cacheFolder = [cacheFolder stringByAppendingPathComponent:fileName]; if ([content writeToFile:cacheFolder atomically:YES encoding:NSUTF8StringEncoding error:NULL] == YES) {
NSLog(@"保存成功");
} } -(NSString*)filterString:(NSString*)aString filterWith:(NSString*)fStr{ aString = [aString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; if ([aString rangeOfString:fStr].location != NSNotFound) {
aString = [aString stringByReplacingOccurrencesOfString:fStr withString:@""];
[self filterString:aString filterWith:fStr];
}
return aString;
} /*
* 检查输入的金额是否符合金额的输入规则
*
*/
-(bool)doCheckInputAmount:(NSString *)textFieldtext replacementString:(NSString *)string
{
int length_f = [textFieldtext length]; if([string length]>)
return FALSE;
if(![string isEqualToString:@"."]&& [textFieldtext length]){
if ([textFieldtext length]>= && ![string isEqualToString:@""] ) {
return FALSE;
} }
if( [string isEqualToString:@""] )
return TRUE;
// 判断输入字符[是否合格]
unichar curChar_f = [string characterAtIndex:];
if( (curChar_f>=''&&curChar_f<='') || (curChar_f=='.') )
{
if( curChar_f=='.' )
{
if(length_f==) // 首字节不能为.
return FALSE;
if( [textFieldtext rangeOfString:@"."].length> ) // .不能重复
return FALSE;
}
if(length_f>)
{
if( [textFieldtext characterAtIndex:]=='' ) // 如果首输入为0,第二必须为.
{
if( curChar_f!='.'&& length_f== )
{
return FALSE;
}
}
} if( length_f>= )
{
// 小数点后2位不能在输入
if( [textFieldtext characterAtIndex:length_f-]=='.' )
return FALSE;
}
return TRUE;
} return FALSE;
} @end @implementation NSDictionary (extra) //根据key值的到字典中的object
- (id)getObjectByKey:(NSString*)key {
NSArray *array = [key componentsSeparatedByString:@"/"];
NSDictionary *dic = self;
for (int i=; i<[array count]-; i++) {
dic = [dic objectForKey:[array objectAtIndex:i]];
}
return [dic objectForKey:[array objectAtIndex:[array count]-]];
} /*
根据key值得到字符串,如为空则返回@“”
*/
- (id)getStringByKey:(NSString*)key {
NSArray *array = [key componentsSeparatedByString:@"/"];
NSDictionary *dic = self;
for (int i=; i<[array count]-; i++) {
dic = [dic objectForKey:[array objectAtIndex:i]];
}
NSString *temp = [dic objectForKey:[array objectAtIndex:[array count]-]];
if (!temp) {
temp = @"";
}
return temp;
} /*
根据key值把字典中的list字典转换成数组
使用方法:NSArray *array = [self getArrayByString:@"Data/responseBody/eventList"];
*/
- (NSMutableArray*)getArrayByString:(NSString*)string {
NSDictionary *dic = [self getObjectByKey:string]; NSMutableArray *array = [[NSMutableArray alloc] init];
if (dic && [dic isKindOfClass:[NSDictionary class]]) {
NSArray *keys = [dic allSortedKeys];
for (int i=; i<[keys count]; i++) {
NSString *key = [keys objectAtIndex:i];
[array addObject:[dic objectForKey:key]];
}
}
return [array autorelease];
} /*
根据参数返回排序的keys
*/
- (NSArray*)sortedKeysByString:(NSString*)elementName {
NSMutableArray *array = [NSMutableArray array];
for (int i=; i<[[self allKeys] count]; i++) {
if (i == ) {
[array addObject:[NSString stringWithFormat:@"%@", elementName]];
continue;
}
[array addObject:[NSString stringWithFormat:@"%@%d", elementName, i]];
}
return array;
} /*
对key进行排序,替代[dic allKeys],对keys进行排序
*/
- (NSArray *)allSortedKeys {
if ([self objectForKey:@"ITEM"]) {
NSMutableArray *array = [NSMutableArray array];
[array addObject:@"ITEM"];
for (int i=; i<[[self allValues] count]; i++) {
[array addObject:[NSString stringWithFormat:@"ITEM%d", i]];
}
return array;
}
else if ([self objectForKey:@"ITEM"]) {
NSMutableArray *array = [NSMutableArray array];
[array addObject:@"ITEM"];
for (int i=; i<[[self allValues] count]; i++) {
[array addObject:[NSString stringWithFormat:@"ITEM%d", i]];
}
return array;
}
return [self allKeys];
} @end @implementation NSString (chnbs) -(void)saveApiCacheFileName:(NSString*)fileName{ NSString * cacheFolder = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
cacheFolder = [cacheFolder stringByAppendingPathComponent:@"JiangSu"]; NSFileManager * fmgr = [NSFileManager defaultManager];
if (![fmgr fileExistsAtPath:cacheFolder])
{
NSError * error;
[fmgr createDirectoryAtPath:cacheFolder withIntermediateDirectories:YES attributes:nil error:&error];
}
cacheFolder = [cacheFolder stringByAppendingPathComponent:fileName];
[self writeToFile:cacheFolder atomically:YES encoding:NSUTF8StringEncoding error:NULL];
} @end

ios中NSObject分类(2)的更多相关文章

  1. ios中NSObject分类

    #import <Foundation/Foundation.h> #define BUNDLE_PATH_IMAGENAME(c) [[NSBundle mainBundle] path ...

  2. IOS中把字符串加密/IOS中怎么样MD5加密/IOS中NSString分类的实现

    看完过后,你会学到: 1学习IOS开发中的分类实现, 2以及类方法的书写, 3以及字符串的MD5加密/解密. ---------------------------wolfhous---------- ...

  3. iOS中的分类(category)和类扩展(extension)

    今天在研究swift的时候看到了分类和扩展.这是两个十分重要有用的功能,但是之前用的不多,没有深入了解过,在今天就从头理一遍. 一.分类(Category): 概念: 分类(Category)是OC中 ...

  4. iOS中的分类和扩展

    一.什么是分类? 概念:分类(Category)是OC中的特有语法,它是表示一个指向分类的结构体指针.根据下面源码组成可以看到它没有属性列表,原则上是不能添加成员变量(其实可以借助运行时功能,进行关联 ...

  5. 转iOS中delegate、protocol的关系

    iOS中delegate.protocol的关系 分类: iOS Development2014-02-12 10:47 277人阅读 评论(0) 收藏 举报 delegateiosprocotolc ...

  6. iOS仿京东分类菜单之UICollectionView内容

    在上<iOS仿京东分类菜单实例实现>已经实现了大部分主体的功能,本文是针对右边集合列表进行修改扩展,使它达到分组的效果,本文涉及到的主要是UICollectionView的知识内容,左边列 ...

  7. iOS中的动画

    iOS中的动画 Core Animation Core Animation是一组非常强大的动画处理API,使用它能做出非常绚丽的动画效果,而且往往是事半功倍,使用它需要添加QuartzCore .fr ...

  8. iOS中关于KVC与KVO知识点

    iOS中关于KVC与KVO知识点 iOS中关于KVC与KVO知识点  一.简介 KVC/KVO是观察者模式的一种实现,在Cocoa中是以被万物之源NSObject类实现的NSKeyValueCodin ...

  9. ios中关于delegate(委托)的使用心得

    ios中关于delegate(委托)的使用心得 分类: iOS开发2012-05-15 10:54 34793人阅读 评论(9) 收藏 举报 iosuiviewtimerinterfaceprinti ...

随机推荐

  1. 构建-0 Gradle DSL 属性和方法【API】

    Android Plugin DSL Reference This is the DSL reference for Android Gradle Plugin. Start reading by f ...

  2. 超链接a标签的href与onclick中使用javascript的区别

    onclick中javascript的区别一般没用到都没注意,但出错时才有些郁闷,看文本章解释如下: 以前一直很随意,后来看.net里的linkbutton似乎是用在<a href=" ...

  3. Python3爬虫:利用Fidder抓取手机APP的数据

    1.什么是Fiddler? Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的“进出”Fiddler的数据(指cookie,ht ...

  4. python 获取几小时之前,几分钟前,几天前,几个月前,及几年前的具体时间

    引入以下两个包: import datetime import arrow 具体代码 # import datetime # import arrow def getTime(self, flag,d ...

  5. struts2 18拦截器详解(九)

    ScopedModelDrivenInterceptor 该拦截器处于defaultStack第八的位置,其主要功能是从指定的作用域内检索相应的model设置到Action中,该类中有三个相关的属性: ...

  6. (转)Unity原厂讲师大解密

    本文转载自:http://nedwu13.blogspot.tw/2013_11_01_archive.html   Asset Bundle工作流程及人物換裝實例 - 劉剛 Unity內部的資源有兩 ...

  7. ZH奶酪:利用CSS将checkbox选项放大

    在Hybrid App开发过程中,html中默认的checkbox选项在手机屏幕上显得比较小,app不能像web page那样放大缩小,所以要通过CSS将checkbox选项放大: 例如HTML代码: ...

  8. 求两个有序数组的中位数或者第k小元素

    问题:两个已经排好序的数组,找出两个数组合并后的中位数(如果两个数组的元素数目是偶数,返回上中位数). 设两个数组分别是vec1和vec2,元素数目分别是n1.n2. 算法1:最简单的办法就是把两个数 ...

  9. 【linux】ubuntu中上下左右键变为^[[A^[[B^[[D^[[C问题处理

    问题现象: 使用上下左右键时,结果为 ^[[A^[[B^[[D^[[C,如图: 原因在于ubuntu系统自带的 vi 不完整导致. 解决方法:安装完整的vi $ sudo apt-get instal ...

  10. VMware用于Site Recovery Manager 5的vSphere Replication功能一览

    http://www.searchstorage.com.cn/showcontent_54838.htm 参考:深度解析SRM 5.0和vSphere Replication http://wenk ...