简体   繁体   English

如何在Swift中对完整的DateTime进行URL编码

[英]How to URL Encode full DateTime in Swift

Hi I am trying to make a request to the Google Calendar API V3 and setting a minDate value. 嗨,我正在尝试向Google Calendar API V3发出请求并设置minDate值。 According to the Google API documentation the date needs to be encoded. 根据Google API文档,需要对日期进行编码。 see: https://developers.google.com/google-apps/calendar/v3/reference/events/list 请参阅: https : //developers.google.com/google-apps/calendar/v3/reference/events/list

Looking at the explanations given in this Question: Swift - encode URL I can party achieve this by using the following: 查看此问题中给出的解释: Swift-编码URL我可以使用以下方法来实现此目的:

// todayString = "2014-11-10T23:15:25+1300"
var escapedTodayString:NSString! = todayString.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())

This however only encodes part of the string and the output is: 2014-11-10T23%3A15%3A25+1300 This fails in the request. 但是,这仅编码字符串的一部分,输出为: 2014-11-10T23%3A15%3A25 + 1300这在请求中失败。 The output I require is: 2014-11-10T22%3A38%3A48%2B1300 我需要的输出是: 2014-11-10T22%3A38%3A48%2B1300

When I make the request using the desired output it works as expected. 当我使用所需的输出发出请求时,它可以按预期工作。

How can I achieve this level of encoding please? 请问如何实现这种编码水平?

You can achieve that by following code. 您可以通过以下代码来实现。 Which removes "+" from the allowed characters 从允许的字符中删除“ +”

var todayString = "2014-11-10T23:15:25+1300"

var characters = NSCharacterSet.URLHostAllowedCharacterSet().mutableCopy() as NSMutableCharacterSet

characters.removeCharactersInString("+")

var escapedTodayString:NSString! = todayString.stringByAddingPercentEncodingWithAllowedCharacters(characters)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM