访问深埋在函数内部的变量,以便在Swift 2中使用函数外部

时间:2016-09-20 23:24:18

标签: json swift nsdateformatter

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

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

//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

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

1 个答案:

答案 0 :(得分:0)

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

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"])