ios中NSObject分类(2)
#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)的更多相关文章
- ios中NSObject分类
#import <Foundation/Foundation.h> #define BUNDLE_PATH_IMAGENAME(c) [[NSBundle mainBundle] path ...
- IOS中把字符串加密/IOS中怎么样MD5加密/IOS中NSString分类的实现
看完过后,你会学到: 1学习IOS开发中的分类实现, 2以及类方法的书写, 3以及字符串的MD5加密/解密. ---------------------------wolfhous---------- ...
- iOS中的分类(category)和类扩展(extension)
今天在研究swift的时候看到了分类和扩展.这是两个十分重要有用的功能,但是之前用的不多,没有深入了解过,在今天就从头理一遍. 一.分类(Category): 概念: 分类(Category)是OC中 ...
- iOS中的分类和扩展
一.什么是分类? 概念:分类(Category)是OC中的特有语法,它是表示一个指向分类的结构体指针.根据下面源码组成可以看到它没有属性列表,原则上是不能添加成员变量(其实可以借助运行时功能,进行关联 ...
- 转iOS中delegate、protocol的关系
iOS中delegate.protocol的关系 分类: iOS Development2014-02-12 10:47 277人阅读 评论(0) 收藏 举报 delegateiosprocotolc ...
- iOS仿京东分类菜单之UICollectionView内容
在上<iOS仿京东分类菜单实例实现>已经实现了大部分主体的功能,本文是针对右边集合列表进行修改扩展,使它达到分组的效果,本文涉及到的主要是UICollectionView的知识内容,左边列 ...
- iOS中的动画
iOS中的动画 Core Animation Core Animation是一组非常强大的动画处理API,使用它能做出非常绚丽的动画效果,而且往往是事半功倍,使用它需要添加QuartzCore .fr ...
- iOS中关于KVC与KVO知识点
iOS中关于KVC与KVO知识点 iOS中关于KVC与KVO知识点 一.简介 KVC/KVO是观察者模式的一种实现,在Cocoa中是以被万物之源NSObject类实现的NSKeyValueCodin ...
- ios中关于delegate(委托)的使用心得
ios中关于delegate(委托)的使用心得 分类: iOS开发2012-05-15 10:54 34793人阅读 评论(9) 收藏 举报 iosuiviewtimerinterfaceprinti ...
随机推荐
- Visual Studio 2008 安装失败(“Web 创作组件”无法安装)(转)
今天安装VS2008时出现了问题,怎么都无法安装成功.装了好几次都在“Visual Studio Web 创作组件(Visual Studio Authoring Component)”的安装的时候失 ...
- High performance find query using lean() in mongoose
原文: http://www.tothenew.com/blog/high-performance-find-query-using-lean-in-mongoose-2/ ------------- ...
- redis信息相关集群
转: http://www.runoob.com/redis/redis-install.html //redis的安装与运维相关 http://zhou123.blog.51cto.com/4355 ...
- (转)Unity3D工程版本管理方案
自:http://blog.dou.li/unity3d%E5%B7%A5%E7%A8%8B%E7%89%88%E6%9C%AC%E7%AE%A1%E7%90%86%E6%96%B9%E6%A1%88 ...
- MySQL老是提示视图没有主键
写了一个视图,每次打开都提示没有主键.我又不想更新视图,根本不关心这个,但每次都提示,很烦. 网上找到解决办法,就是关闭提示: Windows 和 Linux:选择工具 > 选项,并在外观 &g ...
- MFC自定义控件如何向父窗口发送自定义消息
自定义了一个控件 class CHtmlEditCtrlEx : public CHtmlEditCtrl 想在这个控件接收到Ctrl+V键盘消息的时候,向该控件所在的窗口发送一个自定义消息.具体 ...
- ifdef ANDROID总是不好用
在vs的proprocessor definition中定义了ANDRIOD, 但是执行下面的代码的时候,总是说ANDROID is not defined. #ifdef ANDROID ...
- 【转】java并发编程系列之ReadWriteLock读写锁的使用
前面我们讲解了Lock的使用,下面我们来讲解一下ReadWriteLock锁的使用,顾明思义,读写锁在读的时候,上读锁,在写的时候,上写锁,这样就很巧妙的解决synchronized的一个性能问题:读 ...
- 【Linux】好玩的Linux命令(二)
关于Linux talk:http://man.linuxde.net/talk 下面文章转自:http://www.oschina.net/translate/11-lesser-known-use ...
- ANT task之Junit、JunitReport
一.ANT任务之Junit: 学习ANT其实主要是学习ANT的task,ANT众多task中有一个Testing Tasks,它下面有两个任务:Junit和JunitReport,主要用来进行单元测试 ...