I would like to handle json with SwiftJSON, but I stacked. Does anyone show me example code?
I tried to use this library. https://github.com/SwiftyJSON/SwiftyJSON
Although I placed SwiftyJSON.swift in the same project, I have error "No such module "SwiftyJSON"" So correct my code or show me example code handling json from web with swiftyJSON lib.
Here is my code:
import UIKit
import SwiftyJSON // No such module "SwiftyJSON"
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = NSURL(string: "http://express.heartrails.com/api/json?method=getPrefectures")
var request = NSURLRequest(URL: url!)
var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
var json = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSDictionary
var hoge = JSON(data)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here is my Xcode capture
If you added
SwiftyJSON.swift
to your project, you don't need toimport
it. It's already available.Try:
Use this https://github.com/SwiftyJSON/SwiftyJSON version to get up to date SwiftyJSON
If you want to use
import SwiftyJSON
then you need to add using pod to do this, follow stepssudo gem install cocoapods
to installcocoapods
pod init
to initializePodfile
Podfile
and paste following commandplatform :ios, '8.0' use_frameworks! target 'MyApp' do pod 'SwiftyJSON', '~> 2.2.1' end
pod install
and it will add SwiftyJSON into you project.xcworkspace
instead of.xcodeproj
Now you are good to go
For more info, SwiftyJSON in cocoapods
The issue I've had is not following this bit of the CocoaPods instructions:
I was opening the project instead of the workspace which resulted in the No Such Module error.
This went away after opening the workspace.
Api.swift
Api class object:
login Api call:
Hi this is the link to a new Tutorial that explain very well how to working with JSON in Swift.
Parsing JSON the SwiftyJSON Way