Objective-C: How to add query parameter to NSURL?

前端 未结 8 1844
猫巷女王i
猫巷女王i 2020-12-23 18:45

Let\'s say I have an NSURL? Whether or not it already has an empty query string, how do I add one or more parameters to the query of the NSUR

8条回答
  •  遥遥无期
    2020-12-23 19:34

    If you're using RestKit it provides additions to NSString. One of which is:

    - (NSString *)stringByAppendingQueryParameters:(NSDictionary *)queryParameters
    

    So you could do:

    NSDictionary *shopParams = [NSDictionary dictionaryWithKeysAndObjects:
                                    @"limit",@"20", 
                                    @"location",@"latitude,longitude",
                                    nil];
    NSString *pathWithQuery = [@"/api/v1/shops.json" stringByAppendingQueryParameters:shopParams]
    

提交回复
热议问题