Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift 4中获取ISO8601格式?_Ios_Swift_Xcode_Amazon Web Services_Swift3 - Fatal编程技术网

Ios 如何在swift 4中获取ISO8601格式?

Ios 如何在swift 4中获取ISO8601格式?,ios,swift,xcode,amazon-web-services,swift3,Ios,Swift,Xcode,Amazon Web Services,Swift3,我正在使用amazon web服务进行项目搜索。我可以得到时间戳值,并将其转换为iso格式。但在控制台输出中,它显示时间戳无效。原因:必须采用ISO8601格式 以下是我的完整控制台输出值: 状态代码为:可选(400) XML: . InvalidParameterValue2018-06-15T18%3A43%3A52Z参数时间戳的值无效。原因:必须采用ISO8601格式。66a698a6-a967-4b34-b15a-94780f9287ce 这是我试过的女生: public fu

我正在使用amazon web服务进行项目搜索。我可以得到时间戳值,并将其转换为iso格式。但在控制台输出中,它显示时间戳无效。原因:必须采用ISO8601格式

以下是我的完整控制台输出值: 状态代码为:可选(400) XML: . InvalidParameterValue2018-06-15T18%3A43%3A52Z参数时间戳的值无效。原因:必须采用ISO8601格式。66a698a6-a967-4b34-b15a-94780f9287ce

这是我试过的女生:

    public func getSearchItem(searchKeyword: String) -> [String:AnyObject]{
      let timeStamp = getTimestamp()

    let dateFormatter = DateFormatter()
    dateFormatter.timeZone = TimeZone(identifier: "IST")
    let tempLocale = dateFormatter.locale // save locale temporarily
    dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
    let date = dateFormatter.date(from: timeStamp.string(from: Date()))!
    dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
    dateFormatter.locale = tempLocale // reset the locale
    let dateString = dateFormatter.string(from: date)
    print("EXACT_DATE : \(dateString)")

    let url = "http://webservices.amazon.in/onca/xml"
    let parameters: Parameters = ["Service": "AWSECommerceService",
                                  "Operation": "ItemSearch",
                                  "ResponseGroup": "Images,ItemAttributes",
                                  "SearchIndex":"All",
                                  "Keywords": searchKeyword,
                                  "Timestamp": urlEncode(timeStamp.string(from: date)),
                                  "AWSAccessKeyId": urlEncode(ViewController.kAmazonAccessID),
                                  "AssociateTag": urlEncode(ViewController.kAmazonAssociateTag)
                                 ]



    let signedParams = signedParametersForParameters(parameters: parameters as! [String : String])

    Alamofire.request(url, method: .get, parameters: signedParams, encoding: URLEncoding.default)
        .responseString { response in
            print(" - API url: \(String(describing: response.request!))")   // original url request
            var statusCode = response.response?.statusCode

            switch response.result {
            case .success:
                print("status code is: \(String(describing: statusCode))")
                if let string = response.result.value {
                    print("XML: \(string)")
                }
            case .failure(let error):
                statusCode = error._code // statusCode private
                print("status code is: \(String(describing: statusCode))")
                print(error)
            }
    }


    return parameters as [String : AnyObject]
}

func getTimestamp() -> DateFormatter{
    var timestampFormatter = DateFormatter()
    timestampFormatter = DateFormatter()
    timestampFormatter.dateFormat = AWSDateISO8601DateFormat1
    timestampFormatter.timeZone = TimeZone(identifier: "IST")
    timestampFormatter.locale = Locale(identifier: "en_US_POSIX")
    return timestampFormatter
}
}错误是:

Optional(400) XML: . InvalidParameterValue Value 2018-06-15T18%3A43%3A52Z for parameter Timestamp is invalid. Reason: Must be in ISO8601 format.

问题是关于
逃逸百分比(转换为
%3A
),因为
2018-06-15T18:43:52Z
在ISO8601格式中似乎有效,而
2018-06-15T18%3A43%3A52Z
则无效

您正在
参数中添加逃逸百分比:

因此:

=>

另外,请注意,在iOS中,iOS10+提供了一个可避免您自己编写格式的选项。

试试看

func convertDateToDateString(date: Date, format: String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "en_EN")
    dateFormatter.dateFormat = format
    dateFormatter.calendar = Calendar(identifier: .iso8601)

    let resultDate = dateFormatter.string(from: date)
    return resultDate
}

2018-06-15T18%3A43%3A52Z
我想知道问题是否与“:”逃逸百分比(转换为“%3A”)有关,因为否则
2018-06-15T18:43:52Z
对我来说似乎有效。因此,如果只做
“Timestamp”:Timestamp.string(from:date)
而不是
“Timestamp”:urlEncode(Timestamp.string(from:date))
?@Larme您能添加它吗?它有一个答案。。
"Timestamp": timeStamp.string(from: date)
func convertDateToDateString(date: Date, format: String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "en_EN")
    dateFormatter.dateFormat = format
    dateFormatter.calendar = Calendar(identifier: .iso8601)

    let resultDate = dateFormatter.string(from: date)
    return resultDate
}