• ios 从URL中截取所包含的参数,并且以字典的形式返回和参数字典转URL


    //字典转链接(参数)

    - (NSString *)keyValueStringWithDict:(NSDictionary *)dict
    {
        if (dict == nil) {
            return nil;
        }
        NSMutableString *string = [NSMutableString stringWithString:@"?"];
        [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            [string appendFormat:@"%@=%@&",key,obj];
        }];
    
    
        if ([string rangeOfString:@"&"].length) {
            [string deleteCharactersInRange:NSMakeRange(string.length - 1, 1)];
        }
    
    
        return string;
    }
    
    
    //链接转字典  (参数)
    -(NSDictionary *)dictionaryWithUrlString:(NSString *)urlStr
    {
        if (urlStr && urlStr.length && [urlStr rangeOfString:@"?"].length == 1) {
            NSArray *array = [urlStr componentsSeparatedByString:@"?"];
            if (array && array.count == 2) {
                NSString *paramsStr = array[1];
                if (paramsStr.length) {
                    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
                    NSArray *paramArray = [paramsStr componentsSeparatedByString:@"&"];
                    for (NSString *param in paramArray) {
                        if (param && param.length) {
                            NSArray *parArr = [param componentsSeparatedByString:@"="];
                            if (parArr.count == 2) {
                                [paramsDict setObject:parArr[1] forKey:parArr[0]];
                            }
                        }
                    }
                    return paramsDict;
                }else{
                    return nil;
                }
            }else{
                return nil;
            }
        }else{
            return nil;
        }
    }
    //特殊链接解析不了 。  'https://wwww.baidu.com/#/sdffff?name=sdfff&pass=dddff'
    /*
    * 获取url的所有参数 @param url 需要提取参数的url @return NSDictionary */ -(NSDictionary *) parameterWithURL:(NSURL *) url { NSMutableDictionary *parm = [[NSMutableDictionary alloc]init]; //传入url创建url组件类 NSURLComponents *urlComponents = [[NSURLComponents alloc] initWithString:url.absoluteString]; //回调遍历所有参数,添加入字典 [urlComponents.queryItems enumerateObjectsUsingBlock:^(NSURLQueryItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [parm setObject:obj.value forKey:obj.name]; }]; return parm; }
  • 相关阅读:
    DSP28335 烧写到RAM和FLASH方法
    脉冲提取
    基于飞秒光频梳的正弦相位调制干涉绝对距离测量方法研究 -张世华
    DSP28335 IO口寄存器
    DSP28335 使用技巧
    freertos、UCos这种实时操作系统和Linux、Windows这种系统的 本质区别
    stm32的定时器输入捕获 与输出比较
    五大常用算法--回溯
    Dijkstra算法
    字典树-Trie
  • 原文地址:https://www.cnblogs.com/shenlaiyaoshi/p/10797036.html
Copyright © 2020-2023  润新知