使用从Date转换的字符串时,“错误域= NSCocoaErrorDomain代码= 3840“字符0周围的值无效”

问题描述

使用从Date()转换为String的字符串,然后尝试使用参数作为请求时出现错误

第一次尝试:

var currentMonthToContactTime: String? {
        let formatter = DateFormatter()
        formatter.timeZone = TimeZone(identifier: "GMT")
        formatter.dateFormat = "MM"
        return formatter.string(from: Date())
    }

第二次尝试:

let currentMonth = String(Calendar.current.component(.month,from: Date()))

因此,当我使用简单的字符串(例如“ 11”)时,请求就可以正常工作。

我正在使用

URLSession.shared.task

let json = try JSONSerialization.jsonObject(with: responseData,options: [.allowFragments])

关于此的任何提示吗?

解决方法

我发现了错误。

对于某些请求,API返回“无”,我不予处理。

处理完“无”响应后,请求可以正常工作。

感谢支持。