Please advice me for what to do remove the key also from url if that particular key has not any value available.

For ex.

http://api.ean.com/ean-services/rs/hotel/v3/list?minorRev=18&apiKey=rcnge5sr7v4yebfmr2p44gpq&cid=451493&city=London&CurrencyCode=USD&cyCode=US&numberOfResults=50&numberOfAdults=1&numberOfChildren=0&searchRadius=50&supplierCacheTolerance=MED_ENHANCED&arrivalDate=01/24/2014&departureDate=01/25/2014&room1=1&room2=0&room3=0&room4=0&propertyCategory=&amenities=&minStarRating=&maxStarRating=&minRate=&maxRate=

I have this url I want to remove the value with keys if there isn't any value.

I want to convert this url like following

http://api.ean.com/ean-services/rs/hotel/v3/list?minorRev=18&apiKey=rcnge5sr7v4yebfmr2p44gpq&cid=451493&city=London&CurrencyCode=USD&cyCode=US&numberOfResults=50&numberOfAdults=1&numberOfChildren=0&searchRadius=50&supplierCacheTolerance=MED_ENHANCED&arrivalDate=01/24/2014&departureDate=01/25/2014&room1=1

because the rest parameter are 0 or blank.

What is a best practice to achieve this. Either I want to apply condition one by one or is there any other smart approach.

有帮助吗?

解决方案

take one method and pass the values and check weather the value is there or not if there then Append it otherwise leave like that

 for (NSString *key in parametersDictKeyValues) {
       [self addArgument:key argumentValue:[parametersDict objectForKey:key]];
}
-(void)addArgument:(NSString *)key argumentValue:(id)value {
    if(key != nil && value != nil) {
        baseUrlWithParameters = [baseUrlWithParameters stringByAppendingFormat:@"%@=%@&",key,value];
    }
}

and finally remove the last & symbol from baseUrlWithParameters

其他提示

As soon as I post my query, I got an idea to achieve this.

I used

stringByAppendingString

to include only those parameter who are not null

if any value is not available then the string would not append it.

Thanks & Regards

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top