iOS 常用操作之本地存储

文件操作

1. 获取Documents文件夹路径

/**
 *  获取Documents文件夹路径
 *
 *  @return 文件夹路径
 */
+ (NSString *)getDocumentsFolderPath{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [paths objectAtIndex:0];
}

2. 获取Documents文件路径

/**
 *  获取Documents文件路径
 *
 *  @param fileName 文件名称
 *
 *  @return 文件路径
 */
+ (NSString *)getDocumentsFilePath:(NSString *)fileName
{
    return [[self getDocumentsFolderPath] stringByAppendingPathComponent:fileName];
}

3. 获取一个目录所占的空间大小


/**
 *  获取一个目录所占的空间大小
 *
 *  @param filePath 文件目录路径
 *
 *  @return 目录空间大小
 */
+ (long long)fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    else{
        return 0;
    }
} 

4.文件是否存在

/*文件是否存在*/  
+ (BOOL)isFileExisted:(NSString *)fileName{  
    NSFileManager *fileManager = [NSFileManager defaultManager];  
    if(![fileManager fileExistsAtPath:[self getFilePath:fileName]]){  
        return NO;  
    }  
      
    return YES;  
}  

5.创建指定名字的文件

/*创建指定名字的文件*/  
+ (BOOL)createFileAtPath:(NSString *)fileName{  
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    NSString *path = [[array objectAtIndex:0] stringByAppendingPathComponent:fileName];  
    NSLog(@"-----%@:", path);  
    NSFileManager *fileManager = [NSFileManager defaultManager];  
    if(![fileManager fileExistsAtPath:path]){  
        [fileManager createFileAtPath:path contents:nil attributes:nil];  
        return YES;  
    }  
      
    return NO;  
}  

6.创建指定名字的文件夹

/*创建指定名字的文件夹*/  
+ (BOOL)createDirectoryAtPath:(NSString *)fileName{  
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    NSString *path = [[array objectAtIndex:0] stringByAppendingPathComponent:fileName];  
    NSLog(@"-----%@:", path);  
    NSFileManager *fileManager = [NSFileManager defaultManager];  
    if(![fileManager fileExistsAtPath:path]){  
        NSError *error = nil;  
        [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];  
        return YES;  
    }  
      
    return NO;  
}  

7.删除文件

+ (BOOL)deleteFileAtPath:(NSString *)fileName{  
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    NSString *path = [[array objectAtIndex:0] stringByAppendingPathComponent:fileName];  
    NSFileManager *fileManager = [NSFileManager defaultManager];  
      
    if(![fileManager fileExistsAtPath:path]){  
        return NO;  
    }  
      
    [fileManager removeItemAtPath:path error:nil];  
    return YES;  
}  

NSUserDefaults

保存和取出

/**
 保存

 @param value <#value description#>
 @param key <#key description#>
 */
+(void)saveValue:(id) value forKey:(NSString *)key{
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:value forKey:key];
    [userDefaults synchronize];
}


/**
 取出

 @param key <#key description#>
 @return <#return value description#>
 */
+(id)valueForKey:(NSString *)key{
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults objectForKey:key];
}


/**
 根据key取出NSString的value

 @param key <#key description#>
 @return <#return value description#>
 */
+(id)StringForKey:(NSString *)key{
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults stringForKey:key];
}


/**
 根据key取出Bool型的值

 @param key <#key description#>
 @return <#return value description#>
 */
+(BOOL)boolValueForKey:(NSString *)key{
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults boolForKey:key];
}


/**
 保存Bool型的值

 @param value value
 @param key key
 */
+(void)saveBoolValue:(BOOL)value forKey:(NSString *)key{
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:value forKey:key];
    [userDefaults synchronize];
}


/**
 获取userDefaults存储的所有信息
 */
+(void)printAllUserDefault{
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = [userDefaults dictionaryRepresentation];
    NSLog(@"%@",dic);
}

你可能感兴趣的:(iOS 常用操作之本地存储)