我有一个函数,其中我必须将当前的月份和年份参数传递给Swift 3中的Fetch API。代码:-
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))"
}
提前致谢。
最佳答案
您没有currentMonth
和currentYear
的值,因此必须进行一些设置。
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进行任何操作。我应该注意,
currentMonth
和currentYear
现在是Int
类型,如果您需要将它们用作字符串,则只需说出String(currentMonth)
和String(currentYear)
即可进行转换。