使用 SwiftyJSON 解析 JSON 字段
Parsing JSON Field Using SwiftyJSON
请原谅我的天真,但我正在逐步解构一个 SwiftyJSON 示例项目以满足我的需要。现在,我有一个包含以下内容的 AppDelegate 文件;
import UIKit
import SwiftyJSON
@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let navigationController = self.window?.rootViewController as! UINavigationController
let viewController = navigationController.topViewController as! ViewController
let url = NSURL(string: "http://myurl/json/")
let request = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
if error == nil {
let json = JSON(data: data!)
for (key, subJson) in json {
if let userName = subJson["userName"].string {
println(userName)
}
}
viewController.json = json
}
else {
println("Error: \(error.localizedDescription)")
}
})
return true
}
}
当我 运行 应用程序时,这似乎在打印 "userName" 字段方面非常成功。我现在遇到的障碍是弄清楚如何将我的 "userName" 数据传递到我的 ViewController 文件,并让它在我的单元格中显示为文本标签。尽管我做出了努力,但我只能将单元格标记为 "null,",这让我相信我的 AppDelegate 中正在解析的内容无法从 ViewController 文件访问。听起来对吗?
提前致谢!
尝试通过以下方式访问您想要的 UIViewController
:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject :AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var storyboard = UIStoryboard(name: "Main", bundle: nil)
// You need to cast to the name of your ViewController to acces to its fields after.
var initialViewController = storyboard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
// set the JSON value to the ViewController here.
initialViewController.json = json
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
您必须在 Interface Builder 中为您想要的 UIViewController
设置 Storyboard ID。通过上述方式,您可以保护对 hold 的引用,将 UIViewController
设置为您想要的 rootViewController
.
希望对你有所帮助。
请原谅我的天真,但我正在逐步解构一个 SwiftyJSON 示例项目以满足我的需要。现在,我有一个包含以下内容的 AppDelegate 文件;
import UIKit
import SwiftyJSON
@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let navigationController = self.window?.rootViewController as! UINavigationController
let viewController = navigationController.topViewController as! ViewController
let url = NSURL(string: "http://myurl/json/")
let request = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
if error == nil {
let json = JSON(data: data!)
for (key, subJson) in json {
if let userName = subJson["userName"].string {
println(userName)
}
}
viewController.json = json
}
else {
println("Error: \(error.localizedDescription)")
}
})
return true
}
}
当我 运行 应用程序时,这似乎在打印 "userName" 字段方面非常成功。我现在遇到的障碍是弄清楚如何将我的 "userName" 数据传递到我的 ViewController 文件,并让它在我的单元格中显示为文本标签。尽管我做出了努力,但我只能将单元格标记为 "null,",这让我相信我的 AppDelegate 中正在解析的内容无法从 ViewController 文件访问。听起来对吗?
提前致谢!
尝试通过以下方式访问您想要的 UIViewController
:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject :AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var storyboard = UIStoryboard(name: "Main", bundle: nil)
// You need to cast to the name of your ViewController to acces to its fields after.
var initialViewController = storyboard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
// set the JSON value to the ViewController here.
initialViewController.json = json
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
您必须在 Interface Builder 中为您想要的 UIViewController
设置 Storyboard ID。通过上述方式,您可以保护对 hold 的引用,将 UIViewController
设置为您想要的 rootViewController
.
希望对你有所帮助。