ios - 获取当前月份和年份并传递给 API iOS Swift?

标签 ios json api swift3

我有一个函数,我必须在其中将当前月份和年份参数传递给 Swift 3 中的 Fetch API。如果我在参数中传递 hardCode,我会得到儿子的回应,但我无法使用当前月份和年份来完成.代码:-

func raffleNumberGenerate(){
    let prs = [
        "month":currentMonth,
        "year" : currentYear,
        "raffle_result": "1" as String
    ]
    Service.StartWithoutLoading(prs as [String : AnyObject]?, onCompletion: { result in
        let jsonResponseSingle = result as? NSDictionary
        print(" JSON Response :- \(String(describing: jsonResponseSingle))"
}

提前致谢。

最佳答案

currentMonthcurrentYear 没有值,因此您必须创建一些值。

func raffleNumberGenerate() {
    let date = Date() // gets current date
    let calendar = Calendar.current
    let currentYear = calendar.component(.year, from: date) // gets current year (i.e. 2017)
    let currentMonth = calendar.component(.month, from: date) // gets current month (i.e. 10)

    let prs = [
        "month":currentMonth,
        "year" : currentYear,
        "raffle_result": "1" as String
    ]

    Service.StartWithoutLoading(prs as [String : AnyObject]?, onCompletion: { result in
        let jsonResponseSingle = result as? NSDictionary
        print(" JSON Response :- \(String(describing: jsonResponseSingle))"
    }
}

现在您应该能够对 JSON 执行任何需要执行的操作。我应该注意到 currentMonthcurrentYear 现在是 Int 类型,如果你需要它们作为字符串,你可以通过说 String 来转换(currentMonth)String(currentYear)

关于ios - 获取当前月份和年份并传递给 API iOS Swift?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46592091/

相关文章:

c# - 将 List<CustomObject> 转换为 JSON

node.js - Node http 请求无序执行,导致使用随机数值的 API 出现问题

ios - Swift 有时会调用错误的方法

iphone - iOS 5支持web.app中的背景音频吗? (需要开发人员进行测试)

ios - 集合查看单元格图片按比例查看

json - 如何在我的 DialogFlow 请求中找到 JSON 错误?

node.js - 有没有办法将默认值设置为缺失/可选 JSON 属性?

python - 币安 API : how to get the USD as the quote asset

api -/v2/即将关闭 - 但/rest/给出 404

iOS - 将多张图片保存到文档文件夹