iOS字符串删除控制符

今天遇到这么个问题
服务端返回一段jsonString, 需要转成数组使用
但是经过转换后发现是nil
转换方法:
JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
报错:
Unescaped control character around character XX
解决办法:

+(NSString *)deleteControlCharacter:(NSString *)originalString{
    NSMutableString *mstring = [NSMutableString stringWithString:originalString];
    NSCharacterSet *controlChars = [NSCharacterSet controlCharacterSet];
    NSRange range = [originalString rangeOfCharacterFromSet:controlChars];
    while (range.location != NSNotFound){//包含
        [mstring deleteCharactersInRange:range];//删除
        range = [mstring rangeOfCharacterFromSet:controlChars];//递归
    }
    return mstring;
}

你可能感兴趣的:(iOS字符串删除控制符)