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 ...
随机推荐
- Druid连接池简介和配置
Druid是什么?有什么作用? Druid首先是一个数据库连接池,但它不仅仅是一个数据库连接池,它还包含一个ProxyDriver,一系列内置的JDBC组件库,一个SQL Parser. Druid ...
- random_state 参数
SVC(random_state=0)里有参数 random_state random_state 相当于随机数种子,下面会有代码来解释其作用.图中设置了 random.seed() 就相当于在 SV ...
- idea 提交代码时提示 please tell me who you are .......
- Python引用(import)文件夹下的py文件的方法
Python的import包含文件功能就跟PHP的include类似,但更确切的说应该更像是PHP中的require,因为Python里的import只要目标不存在就报错程序无法往下执行.要包含目录里 ...
- @SuppressLint("NewApi")和@TargetApi()的区别
在Android代码中,我们有时会使用比我们在AndroidManifest中设置的android:minSdkVersion版本更高的方法,此时编译器会提示警告, 解决方法是在方法上加上@Suppr ...
- C# 动态解析表达式
需求 我们很难捉摸用户的思维,即使使用非常正式的文档规范某些数据的定义.结果的标准等,也不能抵挡住用户不断变化的需求,但他们有个万变不离的东西——你做这个东西要是万能的,即输入参数类型.个数等发生改变 ...
- SpringBoot添加对Log4j2的支持
1.在添加对Log4j2的支持前,需要先把SpringBoot默认使用的Logback日志框架排除,修改pom.xml文件: <dependency> <groupId>org ...
- iOS 获取已安装 的APP
-(void)getAppPlist { Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace ...
- javascript链式语法
因为 jQuery 库的缘故,链式语法在前端界变得非常流行.实际上这是一种非常容易实现的模式.基本上,你只需要让每个函数返回 'this',这样其他函数就可以立即被调用.看看下面的例子. var bi ...
- 使用Spring框架入门三:基于XML配置的AOP的使用
一.引入Jar包 <!--测试1使用--> <dependency> <groupId>org.springframework</groupId> &l ...