当前位置:编程学习 > wap >>

Iphone文件操作和字符串操作的工具类

直接上代码:
.h文件:
[cpp] #import <Foundation/Foundation.h> 
#define DEFAULT_DATE_TIME_FORMAT (@"yyyy-MM-dd'T'HH:mm:ss'Z'") 
#define DEFAULT_DATE_FORMAT (@"yyyy-MM-dd") 
#define DEFAULT_TIME_FORMAT (@"HH:mm:ss'Z'") 
 
@inte易做图ce FileUtils : NSObject { 
     

 
+(NSString*) settingPath; 
+(NSString*) cachePath; 
+(BOOL) clearCache:(NSError**)error; 
+(NSString*) fileAtBundleResource:(NSString*)file; 
+(BOOL) fileExist:(NSString*)file; 
+(BOOL) fileExistAtCache:(NSString*)file; 
+(NSString*) cachedFile:(NSString*)file; 
+(BOOL) writeToFile:(NSString*)fileWithPath withData:(NSData*)data; 
+(BOOL)deleteFile:(NSString*)fileWithPath; 
+(NSString*) documentPathWith:(NSString*)cmp; 
@end 
 
 
@inte易做图ce ValueUtils : NSObject { 
     

+(BOOL) isEmpty:(NSString*)text; 
+(BOOL) boolValue:(NSDictionary*)dict forKey:(id)key; 
+(float) floatValue:(NSDictionary*)dict forKey:(id)key; 
+(NSInteger) intValue:(NSDictionary*)dict forKey:(id)key; 
+(NSString*) stringValue:(NSDictionary*)dict forKey:(id)key; 
+(NSArray*) arrayValue:(NSDictionary*)dict forKey:(id)key; 
+(id) noneNullValue:(NSDictionary*)dict forKey:(id)key; 
+(NSDictionary*) dictionaryValue:(NSDictionary*)dict forKey:(id)key; 
+(BOOL) stringEquals:(NSString*)str1 to:(NSString*)str2; 
+(BOOL) caseEquals:(NSString*)str1 to:(NSString*)str2; 
+(NSString*) refineUrl:(NSString*)url; 
+(BOOL) isURLString:(NSString*)text; 
+(BOOL) startWith:(NSString*)prefix forString:(NSString*)text; 
+(BOOL) endWith:(NSString*)suffix forString:(NSString*)text; 
+(NSDate *)stringToDate:(NSString *)string withFormat:(NSString*)fmt; 
+(NSString*) dateToString:(NSDate*)date withFormat:(NSString*)fmt; 
+(NSString*) idToString:(id)obj; 
+(BOOL)  booleanToBool:(id)bobj; 
@end 

.m文件:
[cpp] view plaincopy
#import "FileUtils.h" 
#import "CJSONDeserializer.h" 
#import "CJSONSerializer.h" 
 
@implementation FileUtils 
 
+(NSString*) settingPath 

    ////Make a new director 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
     
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@".settings"]; 
     
    BOOL folder = NO; 
    BOOL exist = [fileManager fileExistsAtPath:documentsDirectory isDirectory:&folder]; 
    if ( exist == YES && folder == YES ) { 
        return [documentsDirectory stringByAppendingPathComponent:@"measets.ini"]; 
    }else{ 
        NSError* error = nil; 
        [fileManager createDirectoryAtPath:documentsDirectory  
               withIntermediateDirectories:YES 
                                attributes:nil 
                                     error:&error]; 
        if( error != nil ){ 
            NSLog(@"Can not create directory %@.  Error is %@.", documentsDirectory, 
                  [error description]); 
            return nil; 
        } 
        return documentsDirectory; 
    } 
    /***
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectoryPath = [paths objectAtIndex:0];
     NSString *secondaryDirectoryPath = [documentsDirectoryPath stringByAppendingPathComponent:@"secondary"];
     NSString *databaseFile = [secondaryDirectoryPath stringByAppendingPathComponent:@"database.db"];
     
     NSFileManager *fileManager = [NSFileManager defaultManager];
     [fileManager removeItemAtPath:databaseFile error:NULL];
     ***/ 

 
+(BOOL) fileExist:(NSString*)file 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    return [fileManager fileExistsAtPath:file]; 

 
+(NSString*) fileAtBundleResource:(NSString*)file 

    return [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:file]; 

 
+(BOOL) fileExistAtCache:(NSString*)file 

    return [self fileExist:[self cachedFile:file]]; 

 
+(NSString*) cachedFile:(NSString*)file 

    NSString* fileAtCache = [NSString stringWithFormat:@"%@%@",[self cachePath], file]; 
    return fileAtCache; 

 
+(BOOL) writeToFile:(NSString*)fileWithPath withData:(NSData*)data 

    BOOL folder = NO; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
     
    NSString* fileName = [fileWithPath lastPathComponent]; 
    NSInteger toIndex = [fileWithPath length] - [fileName length]; 
    NSString* path = [fileWithPath substringToIndex:toIndex]; 
    BOOL exist = [fileManager fileExistsAtPath:path isDirectory:&folder]; 
    if (  exist != YES ) { 
        NSError* error = nil; 
        [fileManager createDirectoryAtPath:path  
               withIntermediateDirectories:YES 
                                attributes:n

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,