#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. 关于使用rem单位、css函数calc()进行自适应布局

    一.关于css中的单位 大家都知道在css中的单位,一般都包括有px,%,em等单位,另外css3新增加一个单位rem. 其中px,%等单位平时在传统布局当中使用的比较频繁,大家也比较熟悉,不过px单 ...

  2. 微信小程序通过api接口将json数据展现到小程序示例

    这篇文章主要介绍了微信小程序通过api接口将json数据展现到小程序示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧实现知乎客户端的一个重要知识前提就是,要知道怎么通过 ...

  3. 转:在centos安装与启动mysql

    一. 下载与安装过程相录详细 相当详细推荐.digitalocean.com 这个网站的东西,很详细,很专业. https://www.digitalocean.com/community/tutor ...

  4. (转)HLSL,函数列表

    中文列表 函数名 说明 abs 计算输入值的绝对值. acos 返回输入值反余弦值. all 测试非0值. any 测试输入值中的任何非零值. asin 返回输入值的反正弦值. atan 返回输入值的 ...

  5. maven多module项目中千万不要引入其它模块的单元測试代码

    本文出处:http://blog.csdn.net/chaijunkun/article/details/35796335,转载请注明. 因为本人不定期会整理相关博文,会对对应内容作出完好. 因此强烈 ...

  6. 如何用简单例子讲解 Q - learning 的具体过程?

    作者:牛阿链接:https://www.zhihu.com/question/26408259/answer/123230350来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  7. (纪录片)统计的乐趣 The Joy of Stats (2010)

    简介: 导演: 丹·希尔曼主演: Hans Rosling类型: 纪录片官方网站: www.bbc.co.uk/programmes/b00wgq0l制片国家/地区: 英国语言: 英语上映日期: 20 ...

  8. 大量带BPM的跑步歌曲/跑步音乐下载

    20150110停止更新告知:不知不觉本帖更新有近半年了.从最开始跑步已经四年多,一直是听着音乐跑的,音乐支持.陪伴.丰富着我的跑步之旅.直到上个月因一次觉得音乐吵,我开始有意地摘掉耳机去跑步,并开始 ...

  9. Camtasia Studio CamStudio如何不录制鼠标

    在录制的小窗口中,点击Effects-Options,然后Cursor里面取消勾选Make cursor effects 可能会报错说请选择有效的声音文件   在Sound里面选择一个有效的目录,不能 ...

  10. STL 笔记(四) 迭代器 iterator

    stl 中迭代器能够理解为面向对象版本号的广义指针,提供了对容器中的对象的訪问方法,能够遍历容器全部元素.也能够訪问随意元素.stl 迭代器有下面五种: Input iterators   仅仅读,输 ...