Reputation: 728
I'm having trouble trying to get this library Alamofire-SwiftyJSON (https://github.com/SwiftyJSON/Alamofire-SwiftyJSON) to work.
I created a new project and downloaded the Alamofire-SwiftyJSON library and dragged everything into my project navigation area.
However when I go into the ViewController and add:
import AlamofireSwiftyJSON
I get the error: no such module import AlamofireSwiftyJSON
Can someone tell me how to add this project manually. Ideally step by step so that I can get this code to work:
Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
.responseSwiftyJSON { (request, response, json, error) in
println(json)
println(error)
}
Thank you
Upvotes: 3
Views: 9314
Reputation: 1281
I finally resolved by reading at Cocoapods.org Get Started paragraph:
1) Close xCode application (important!)
2) from the Terminal, inside your project folder type:
open YourApp.xcworkspace
Now everything will work :)
Upvotes: 0
Reputation: 899
You can also use Cocoapods now. Pod install Alamofire and SwiftyJSON separately and just bring in AlamofireSwiftyJSON.swift into your xcode project folder.
Then in the view controller or whatever you want to use it, import Alamofire and SwiftyJSON at the top of your file.
Then below code snippet should compile fine.
Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
.responseSwiftyJSON({ (request, response, json, error) in
println(json)
println(error)
})
Upvotes: 0
Reputation: 2810
First follow your link to the Alamofire-SwiftyJSON project. There download the zip for the full project. Also follow the link for the Alamofire project and download the fullzip, same thing again for the swiftyJSON project.
Now here is what you need to do :
You can check the 'copy files if needed' if you want. If you don't, be sure to keep the folder for the alamofire project. 3. Do the exact same thing for the swiftyJSON project. By adding the 'SwiftyJSON.xcodeproj' file. 4. Go to your project settings > Build Phase > Link Binary With Libraries 5. Add the Alamofire and SwiftyJSON frameworks (choose the right frameworks depending on if you are developing for iphone or mac os x) like that :
Go to whatever part of the program you need your request to be made (e.g. ViewController or AppDelegate) and add the following code below the initial import statement
import Alamofire
Add your request with the following code:
Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"]) .responseSwiftyJSON { (request, response, json, error) in println(json) println(error) }
Have fun coding !
Upvotes: 2
Reputation: 2566
What is your Deployment Target? is it < 8.0 ? if so those targets that do not support embedded frameworks and as such all you need to do is:
so the code you have will be:
request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
.responseSwiftyJSON { (request, response, json, error) in
println(json)
println(error)
}
So now, that is that framework. Since I have some time now I will show you how to get it all working and neaten it up a little.
Once you have done the above, do this:
Again... Make sure you copy files if needed. That is it for the framework also.
Right to neaten it up a little - You don't have to do this but its always better to use variables and arrays where possible to make cleaner and speed up coding time. For your example above do this:
var url = "http://httpbin.org/get"
var arr_parameters = [
"foo": "bar"
]
request(.GET, url, parameters: arr_parameters)
Its also a good idea to incorporate some error testing so that you can debug things easier and also provide fallbacks etc...
so, with this in mind, your entire code should look like:
var url = "http://httpbin.org/get"
var arr_parameters = [
"foo": "bar"
]
request(.GET, url, parameters: arr_parameters)
.responseJSON { (req, res, json, error) in
if(error != nil) {
NSLog("Error: \(error)")
println(req)
println(res)
}
else {
NSLog("Success: \(url)")
var json = JSON(json!)
}
}
There was a really good article covering this in SwiftMonthly
I hope this helps. Good luck and keep us posted.
Upvotes: 4