gpt4 book ai didi

json - 访问埋藏在函数深处的变量以在 Swift 2 中的函数外部使用

转载 作者:行者123 更新时间:2023-11-30 13:03:59 24 4
gpt4 key购买 nike

所以我遇到了一个小障碍,我正在尝试访问在 Alamofire 请求函数中创建的变量。一些背景知识:

使用 SwiftyJSON/Alamofire 访问 JSON 文件并解析它,有一个用于访问日期的变量,但日期采用 RFC 3339 格式,现在我创建了一个函数来将日期从 RFC 339 解析为可读格式,但我不知道现在如何访问在 JSON 解析函数中创建的日期变量以与日期解析函数一起使用。

//Get the JSON from server
func getJSON() {

Alamofire.request(.GET, "link goes here").responseJSON { (Response) in

if let value = Response.result.value {

let json = JSON(value)

for anItem in json.array! {

let title: String? = anItem["Title"].stringValue
let date: String? = anItem["Date"].stringValue //trying to access this variable outside the function
let body: String? = anItem["Body"].stringValue
self.tableTitle.append(title!)
self.tableDate.append(date!)
self.tableBody.append(body!)
print(anItem["Title"].stringValue)
print(anItem["Date"].stringValue)

}

dispatch_async(dispatch_get_main_queue()) {

self.tableView.reloadData()

}

}

}

}

// this date stuff isn't being used yet, because I have no idea how...
public func dateForRFC3339DateTimeString(rfc3339DateTimeString: String) -> NSDate? {

let enUSPOSIXLocale = NSLocale(localeIdentifier: "en_US_POSIX")

let rfc3339DateFormatter = NSDateFormatter()
rfc3339DateFormatter.locale = enUSPOSIXLocale
rfc3339DateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
rfc3339DateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)

return rfc3339DateFormatter.dateFromString(rfc3339DateTimeString)
}

public func userVisibleDateTimeStringForRFC3339DateTimeString(rfc3339DateTimeString: String) -> String? {

let maybeDate = dateForRFC3339DateTimeString(rfc3339DateTimeString)
if let date = maybeDate {

let userVisibleDateFromatter = NSDateFormatter()
userVisibleDateFromatter.dateStyle = NSDateFormatterStyle.MediumStyle
userVisibleDateFromatter.timeStyle = NSDateFormatterStyle.ShortStyle

return userVisibleDateFromatter.stringFromDate(date)

} else {

return nil

}

}

let finalDateStr = userVisibleDateTimeStringForRFC3339DateTimeString(MasterViewController) //now this is where it gets weird, instead of letting me enter the string in the brackets, it defaults to MasterViewController, now I tried to move the date functions to another .swift file (an empty one) and it doesn't do that anymore

是的,就是这样,如果有人可以提供帮助,我们将不胜感激。

最佳答案

尝试下面的代码,让我知道它是否有效:

func dateForRFC3339Date(rfc3339Date: NSDate) -> NSDate? {
let enUSPOSIXLocale = NSLocale(localeIdentifier: "en_US_POSIX")
let rfc3339DateFormatter = NSDateFormatter()
rfc3339DateFormatter.locale = enUSPOSIXLocale
rfc3339DateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
rfc3339DateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
let dateString = rfc3339DateFormatter.stringFromDate(rfc3339Date)
return rfc3339DateFormatter.dateFromString(dateString)
}

并在您的 getJSON() 方法中调用它,如下所示:

let ConvertedDate = self.dateForRFC3339Date(rfc3339Date: anItem["Date"])

关于json - 访问埋藏在函数深处的变量以在 Swift 2 中的函数外部使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39605215/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com