.h
#import{Foundation/Foundation.h}
@interfaceNSString (Helper)
/**
*清空字符串中的空白字符
*
*@return清空空白字符串之后的字符串
*/
- (NSString*)trimString;
/**
*是否空字符串
*
*@return如果字符串为nil或者长度为0返回YES
*/
- (BOOL)isEmptyString;
/**
*返回沙盒中的文件路径
*
*@return返回当前字符串对应在沙盒中的完整文件路径
*/
- (NSString*)documentsPath;
/**
*写入系统偏好
*
*@paramkey写入键值
*/
- (void)saveToNSDefaultsWithKey:(NSString*)key;
.m
#import"NSString+Helper.h"
@implementationNSString (Helper)
#pragma mark清空字符串中的空白字符
- (NSString*)trimString
{
return[selfstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]];
}
#pragma mark是否空字符串
- (BOOL)isEmptyString
{
return(self==nil||self.length==0);
}
#pragma mark返回沙盒中的文件路径
- (NSString*)documentsPath
{
NSString*path =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)[0];
return[pathstringByAppendingString:self];
}
#pragma mark写入系统偏好
- (void)saveToNSDefaultsWithKey:(NSString*)key
{
[[NSUserDefaultsstandardUserDefaults]setObject:selfforKey:key];
[[NSUserDefaultsstandardUserDefaults]synchronize];
}
@end
网友评论