iOS一些BUGs/TIPs记录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS一些BUGs/TIPs记录相关的知识,希望对你有一定的参考价值。

目录


1,[NSString containsString:]在ios7会崩溃(废话) 
2,使用AFNetworking的时候,如果想要看发送的具体链接是什么请在这里打断点
3,遇到恶心的server,返回数据的时候先告诉你error,然后在body里面放错误信息....

 

1,[NSString containsString:]在iOS7会崩溃(废话)

解决:网上的改法一般是在nsstring里面加上一个containsString函数,但是这样无法根据版本号判断调用(在那里判断的话会导致死循环,可以试下玩玩~),所以可以考虑单独列出来。

[self containsString:string andSubString:@" "]

- (BOOL)containsString:(NSString *)oString andSubString:(NSString *)subString {
    if ([[UIDevice currentDevice] systemVersion].floatValue >= 8.0) {
        return [oString containsString:subString];
    }
    else if ([oString rangeOfString:subString].location != NSNotFound) {
        return YES;
    }
    return NO;
}

 

2,使用AFNetworking的时候,如果想要看发送的具体链接是什么请在这里打断点:

  AFURLRequestSerialization.m 331行

- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
                                 URLString:(NSString *)URLString
                                parameters:(id)parameters
                                     error:(NSError *__autoreleasing *)error
{
    NSParameterAssert(method);
    NSParameterAssert(URLString);

    NSURL *url = [NSURL URLWithString:URLString];

    NSParameterAssert(url);

    NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url];
    mutableRequest.HTTPMethod = method;

    for (NSString *keyPath in AFHTTPRequestSerializerObservedKeyPaths()) {
        if ([self.mutableObservedChangedKeyPaths containsObject:keyPath]) {
            [mutableRequest setValue:[self valueForKeyPath:keyPath] forKey:keyPath];
        }
    }

    mutableRequest = [[self requestBySerializingRequest:mutableRequest withParameters:parameters error:error] mutableCopy];

    return mutableRequest;
}

找不到的话查询mutableRequest就好。另外,如果是post类型的,在331行return前断掉,输入po parameters就可以检查你传的参数了。可惜的是这时候参数还没有被encode哟~

3,遇到恶心的server,返回数据的时候先告诉你error,然后在body里面放错误信息....

AFNetworking本身是不会帮你解析的。这时没办法的你只能改AFNetworking了...

试试利用 AFHTTPRequestOperation *operation 这个变量在error的时候解析

我们的项目里这样改的:BCJSONObjectRequest.m

- (void)sendRequest {
    _tag = sTag ++;
    [self buildParams];
    //解析
    void (^success)(AFHTTPRequestOperation *operation, id responseObject) = ^(AFHTTPRequestOperation *operation, id responseObject) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSError *erro;
            id result = [self parseResponse:responseObject error:&erro];
            dispatch_async(dispatch_get_main_queue(), ^{
                //在主线程回调
                if (erro != nil) {
                    [self inerJsonResultFailed:erro];
                }else {
                    [self inerJsonResultSuccess:result];
                }
            });
        });
    };
    
    void (^failure)(AFHTTPRequestOperation *operation, NSError *error) = ^(AFHTTPRequestOperation *operation, NSError *error) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            id erro = [self parseErro:error];
            dispatch_async(dispatch_get_main_queue(), ^{
                NSError *parseErro;
                id result = [self parseResponse:operation.responseObject error:&parseErro];
                if (result != nil && operation.responseObject != nil) {
                    if ([result isKindOfClass:NSError.class]) {
                        [self inerJsonResultFailed:result];
                    }
                    else{
                        [self inerJsonResultSuccess:result];
                    }
                }
                else {
                    //在主线程回调
                    [self inerJsonResultFailed:erro];
                }
            });
        });
    };
    //调用请求
    BCHTTPFacade *manager = [BCHTTPFacade shareFacade];
    switch (self.requestMethod) {
        case Get:
            self.requestOperation = [manager GET:_url parameters:self.params success:success failure:failure];
            break;
        case Post:
            if (self.mutipartBlock) {
                [manager POST:_url parameters:self.params constructingBodyWithBlock:self.mutipartBlock success:success failure:failure];
            }else {
                self.requestOperation = [manager POST:_url parameters:self.params success:success failure:failure];
            }
            break;
        default:
            LogError(@"UnSupport method:requestMethod = %d,url = %@",self.requestMethod,self.url);
            break;
    }
}

 

以上是关于iOS一些BUGs/TIPs记录的主要内容,如果未能解决你的问题,请参考以下文章

Sphinx、reStructuredText 显示/隐藏代码片段

CSP核心代码片段记录

常用python日期日志获取内容循环的代码片段

记录C#常用的代码片段

massCode 一款优秀的开源代码片段管理器

discuz X3.1 源代码阅读,记录代码片段