OC NSString 一些不常用的方法(持续更新)

判断字符串的开头和结尾是否包含指定的字符串

1
2
- (BOOL)hasPrefix:(NSString *)str;//判断是否以str 开头
- (BOOL)hasSuffix:(NSString *)str;//判断是否以str结尾

去掉字符串首尾空格

1
str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

数组转Json类型的字符串

  • 数组中存的元素为字典类型

.h文件

1
2
3
4
5
6
7
#import <Foundation/Foundation.h>
@interface Object2Json : NSObject
+ (NSDictionary*)getObjectData:(id)obj;
+ (NSData*)getJSON:(id)obj options:(NSJSONWritingOptions)options error:(NSError**)error;
+ (id)getObjectInternal:(id)obj;
@end

.m文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#import "Object2Json.h"
#import <objc/runtime.h>
@implementation Object2Json
+ (NSDictionary*)getObjectData:(id)obj
{
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
unsigned int propsCount;
objc_property_t *props = class_copyPropertyList([obj class], &propsCount);
for(int i = 0;i < propsCount; i++)
{
objc_property_t prop = props[i];
NSString *propName = [NSString stringWithUTF8String:property_getName(prop)];
id value = [obj valueForKey:propName];
if(value == nil)
{
value = [NSNull null];
}
else
{
value = [self getObjectInternal:value];
}
[dic setObject:value forKey:propName];
}
return dic;
}
+ (NSData*)getJSON:(id)obj options:(NSJSONWritingOptions)options error:(NSError**)error
{
return [NSJSONSerialization dataWithJSONObject:[self getObjectData:obj] options:kNilOptions error:error];
}
+ (id)getObjectInternal:(id)obj
{
if([obj isKindOfClass:[NSString class]]
|| [obj isKindOfClass:[NSNumber class]]
|| [obj isKindOfClass:[NSNull class]])
{
return obj;
}
if([obj isKindOfClass:[NSArray class]])
{
NSArray *objarr = obj;
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:objarr.count];
for(int i = 0;i < objarr.count; i++)
{
[arr setObject:[self getObjectInternal:[objarr objectAtIndex:i]] atIndexedSubscript:i];
}
return arr;
}
if([obj isKindOfClass:[NSDictionary class]])
{
NSDictionary *objdic = obj;
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:[objdic count]];
for(NSString *key in objdic.allKeys)
{
[dic setObject:[self getObjectInternal:[objdic objectForKey:key]] forKey:key];
}
return dic;
}
return [self getObjectData:obj];
}
@end

转化方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- (NSString *)pictureArrayToJSON:(NSArray *)picArr {
if (picArr && picArr.count > 0) {
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
for (postModel *model in picArr) {
NSData *jsonData = [Object2Json getJSON:model options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonText = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[arr addObject:jsonText];
}
return [self objArrayToJSON:arr];
}
return nil;
}
- (NSString *)objArrayToJSON:(NSArray *)array {
NSString *jsonStr = @"[";
for (NSInteger i = 0; i < array.count; ++i) {
if (i != 0) {
jsonStr = [jsonStr stringByAppendingString:@","];
}
jsonStr = [jsonStr stringByAppendingString:array[i]];
}
jsonStr = [jsonStr stringByAppendingString:@"]"];
return jsonStr;
}

调用方法

1
NSString * json = [self pictureArrayToJSON:dicArray];