Alamofire : Updating array's values with JSON's data - arrays

I have a problem when trying to update an array of struct with the JSON's data. Here's my code:
This is how the struct is defined:
Struct
struct Shot {
var title: String
var desc: String
var img: String
init(title: String, desc: String, img: String) {
self.title = title
self.desc = desc
self.img = img
}
}
Here I try to update the array, but it doesn't work:
Code
func authDribbble() {
let endURL = "https://api.dribbble.com/v1/shots/"
let token = "***"
Alamofire.request(.GET, endURL, parameters: ["access_token" : token])
.responseJSON { response in
if let JSON = response.result.value {
for i in 0..<JSON.count {
let titleA: String = String(JSON[i]["title"])
let descA: String = String(JSON[i]["description"])
let imgA: String = String(JSON[i]["images"])
self.data += [Shot(title: titleA, desc: descA, img: imgA)]
}
}
}
}
The problem is that after the function is performed, the array stays void. What's wrong?
Thank you.

Working under the assumption that you've called this method as follows:
authDribbble()
print("my data: \(self.data)") // Prints empty array.
The issue is that Alamofire is performing the request asynchronously. That is, the work isn't done yet when your code proceeds to the next line (the print statement). You need to implement a callback mechanism. One way to do this is to pass a function as a parameter of your authDribbble() function. That would change the function to something like:
func authDribbble(completion: () -> Void) {
// create the url
// create parameters
Alamofire.request(.GET, url!, parameters: params)
.responseJSON { response in
guard let JSON = response.result.value as? [[String : AnyObject]] else {
// Wrong type in JSON response.
print(response.result.error)
}
self.data = JSON.map { dict in
guard let title = dict["title"] as? String,
desc = dict["description"] as? String,
img = dict["images"] as? String else {
// Handle error creating Shot from JSON
}
return Shot(title: title, desc: desc, img: img)
}
completion()
}
}
}
You would then replace the call to authDribbble() with:
authDribbble() {
print("data: \(self.data)") // Prints populated array.
}

Related

Swift - json nested array not returning value

I am trying to grab the array from "imports" from the JSON file but the array is returning encoded.
[ServerName.JSONNodeValue, ServerName.JSONNodeValue, ServerName.JSONNodeValue, ServerName.JSONNodeValue, ServerName.JSONNodeValue, ServerName.JSONNodeValue]
I know the data can be seen as if I print the value of the data in the JSONNode file, it comes up as
https://servername.com/storage/sessions/00167/imports
(
"movie1.mov",
"movie3.mov",
"movie2.mov",
"._movie1.mov",
"._movie2.mov",
"._movie3.mov"
)
This isn't my code and the reason why I am struggling as I am still pretty new at swift.
My JSON file looks similar to this.
{
"id": 135
"name": Test
"angles" [
{
"id": 35,
"name": "test",
"mp4": "http:/0.0.0:3000/storage/seesion/00138/url.mp4"
}
]
"imports" [
movie1.mp4,
movie2.mp4,
movie3.mp4
]
}
Swift Code - Session File
struct Session {
var id: Int
var name: String
var angles: [Angle] = []
var imports: [Any]
extension Session {
init(fromDict dict: [String: AnyObject]){
let node = JSONNodeValue(dict)
let sessionId = node["id"].int ?? 0
self.id = sessionId
self.name = node["name"].string ?? "???"
print(name)
self.imports = node["imports"].arrayOrEmpty
self.angles = node["angles"].arrayOrEmpty.map { angleDict in
Angle(fromDict: angleDict.object!)
}
JSONnode file that handles the JSON
protocol JSONNode {
subscript(key: String) -> JSONNode { get }
var int: Int? { get }
var double: Double? { get }
var string: String? { get }
var bool: Bool? { get }
var date: Date? { get }
var array: [JSONNode]? { get }
var arrayOrEmpty: [JSONNode] { get }
var object: [String: Any]? { get }
var objectOrEmpty: [String: Any] { get }
}
class JSONNodeValue : JSONNode {
static func parse (_ data: Data) -> JSONNode {
if let root = try? JSONSerialization.jsonObject(with: data) {
return JSONNodeValue(root)
} else {
return JSONNodeNone.instance
}
}
var value: Any
init(_ value: Any) {
self.value = value
print(value) // SHOWS THE DATA I NEED
}
subscript(key: String) -> JSONNode {
if let object = value as? [String: Any], let subvalue = object[key] {
return JSONNodeValue(subvalue)
} else {
return JSONNodeNone.instance
}
}
var int: Int? {
return value as? Int
}
var double: Double? {
return value as? Double
}
var string: String? {
return value as? String
}
var bool: Bool? {
return value as? Bool
}
var date: Date? {
if let formatted = string {
return Date.fromIso8601(formatted)
} else {
return nil
}
}
var array: [JSONNode]? {
if let array = value as? [Any] {
return array.map { JSONNodeValue($0) }
} else {
return nil
}
}
var arrayOrEmpty: [JSONNode] {
return array ?? []
}
var object: [String: Any]? {
return value as? [String: Any]
}
var objectOrEmpty: [String : Any] {
return object ?? [:]
}
}
Could someone point me in the right direction or to other answered questions that could help me solve this? Thanks
The struct looks good, but remove the = [] after [Angle], but you should use Codable protocol to parse the JSON like so:
struct Session: Codable {
var id: Int
var name: String
var angles: [Angle]
var imports_dir_contents: [Any]
then create another struct to get the Angle
struct Angle: Codable
let id: Int
let name: String
let mp4: String
Then you want to parse using the Codable protocol method by passing in the data retrieved from the networking call.
do {
let newJSONDecoder = JSONDecoder()
let session = try newJSONDecoder.decode(Session.self, from:data)
let anglesArray = session.angles // or use whatever array property you want
} catch {
print("error while parsing:\(error)")
}
I solved what I was trying to achieve by adding a new a variable in the JSNODE file which then gave me ["movie1.mov", "movie3.mov", "movie2.mov", "._movie1.mov", "._movie2.mov", "._movie3.mov"]
var arrayList: [String]? {
return value as? [String]
}
Thanks for the help though.

Weird results with parsing array from json in Swift3

Hi guys I don't know why the array Places returns weird values like 0x6080004b3aa0 instead of displaying my title, coordinate and subtitle out of my JSON url. Thanks for your Help!
import MapKit
#objc class Place: NSObject {
var title: String?
var coordinate: CLLocationCoordinate2D
var subtitle: String?
init(title:String,subtitle:String, coordinate:CLLocationCoordinate2D){
self.title = title
self.coordinate = coordinate
self.subtitle = subtitle
}
static func getPlaces() -> [Place] {
guard let url = NSURL(string: "https://script.googleusercontent.com/macros/echo?user_content_key=Z-LfTMdhgAg_6SRd-iMucSyWu-LFBQO8MLxJZ6DPcL05Rtr3joCCypWD2l46qaegSpVpVINc1DLl5inoDOgGx3p3ANpY1AkGOJmA1Yb3SEsKFZqtv3DaNYcMrmhZHmUMWojr9NvTBuBLhyHCd5hHa1ZsYSbt7G4nMhEEDL32U4DxjO7V7yvmJPXJTBuCiTGh3rUPjpYM_V0PJJG7TIaKp4bydEiKBUZP6fpOyGJIhkmEGneM7ZIlWloTVbXmkjs15vHn8T7HCelqi-5f3gf3-sKiW3k6MDkf31SIMZH6H4k&lib=MbpKbbfePtAVndrs259dhPT7ROjQYJ8yx") else { return [] }
let request = NSMutableURLRequest(url: url as URL!)
var places = [Place]()
let task = URLSession.shared.dataTask(with: request as URLRequest) {data,response,error in
guard error == nil && data != nil else {
print ("Error:",error)
return
}
let httpStatus = response as? HTTPURLResponse
if httpStatus?.statusCode == 200
{ if data?.count != 0
{
let responseString = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary
let contacts = responseString["Sheet1"] as? [AnyObject]
for contact in contacts!{
var places = [Place]()
let title = contact["name"] as! String
let subtitle = contact["description"] as? String
let latitude = contact["latitude"] as? Double ?? 0, longitude = contact["longitude"] as? Double ?? 0
let place = Place(title:title,subtitle:subtitle!,coordinate: CLLocationCoordinate2DMake(latitude, longitude))
places.append(place)
print(latitude)
print(place)
}
}
else {
print("No data got from url")
}
} else {
print("error httpsatus code is:", httpStatus!.statusCode)
}
}
task.resume()
return places as [Place]
}
}
I think the problem is this:
let place = Place(title:title,subtitle:subtitle!,coordinate: CLLocationCoordinate2DMake(latitude, longitude))
When I print(place) it returns the weird results
When you make a class that subclasses from NSObject you're creating a object that is backed by an Objective-c class -- which in some circumstances can be really useful (most common use is when you want to take your object and archive it as a blob of binary data).
I'm guessing that in your case, you probably don't want/need to subclass NSObject.
Here's a simplified example to show what's happening:
Here's a class backed by NSObject:
#objc class ObjCPlace: NSObject {
let name: String
init(name: String) {
self.name = name
}
}
If you create an instance of this object and try to print contents - like you've found, you get the objects location in memory:
let testObjcPlace = ObjCPlace(name: "New York")
print(testObjcPlace)
// prints:
// <__lldb_expr_310.ObjCPlace: 0x600000055060>
On alternative to using print could be to use dump that provides a more detailed look at your object:
let testObjcPlace = ObjCPlace(name: "New York")
dump(testObjcPlace)
// Prints:
// ▿ <__lldb_expr_310.ObjCPlace: 0x600000055060> #0
// - super: NSObject
// - name: "New York"
But instead of making an NSObject subclass, you probably just want to make a Swift class (or in this example a struct take a look at this question and answers for explanations of the differences)
struct Place {
let name: String
init(name: String) {
self.name = name
}
}
Because this object isn't an Objective-c object you can use print and get the internal properties printed as well:
let testPlace = Place(name: "New York")
print(testPlace)
// Prints:
// Place(name: "New York")
p/s welcome to StackOverflow!

How to Make JSON from Array of Struct in Swift 3?

I have a problem to make a JSON from an array of struct in Swift3. I searched in Stack Overflow, nothing help me (here the screenshot). I have a struct like this:
public struct ProductObject {
var prodID: String
var prodName: String
var prodPrice: String
var imageURL: String
var qty: Int
var stock: String
var weight: String
init(prodID: String, prodName: String, prodPrice: String, imageURL: String, qty: Int, stock: String, weight: String){
self.prodID = prodID
self.prodName = prodName
self.prodPrice = prodPrice
self.imageURL = imageURL
self.qty = qty
self.stock = stock
self.weight = weight
}
}
and the array of that struct:
private var productsArray = [ProductObject]()
When the array is not empty, and then I tried to print it in another class, it shows this in debugger:
[app.cartclass.ProductObject(prodID: "2", prodName: "produk 2", prodPrice: "IDR 1000000", imageURL: "someURL", qty: 1, stock: "11", weight: "200")]
The array is not a valid JSON object. How to make it a valid JSON object? And I wonder whether this part "app.cartclass.ProductObject" is a problem or not to make it a valid JSON object?
edit:
Here's how I serialize into a JSON:
var products = [String:Any]()
for j in 0 ..< cart.numberOfItemsInCart() {
products=["\(j)":cart.getAllProduct(atIndex: j)]
}
if let valid = JSONSerialization.isValidJSONObject(products) {
do {
let jsonproducts = try JSONSerialization.data(withJSONObject: products, options: .prettyPrinted) as! [String:Any]
//print(jsonproducts)
} catch let error as NSError {
print(error)
}
} else {
print("it is not a valid JSON object");
}
If you want to make JSON from custom object then first you need to convert your custom object to Dictionary, so make one function like below in your ProductObject struct.
func convertToDictionary() -> [String : Any] {
let dic: [String: Any] = ["prodID":self.prodID, "prodName":self.prodName, "prodPrice":self.prodPrice, "imageURL":self.imageURL, "qty":qty, "stock":stock, "weight":weight]
return dic
}
Now use this function to generate Array of dictionary from Array of custom object ProductObject.
private var productsArray = [ProductObject]()
let dicArray = productsArray.map { $0.convertToDictionary() }
Here dicArray is made of type [[String:Any]], now you can use JSONSerialization to generate JSON string from this dicArray.
if let data = try? JSONSerialization.data(withJSONObject: dicArray, options: .prettyPrinted) {
let str = String(bytes: data, encoding: .utf8)
print(str)
}

json missing fields swift

So I have a function that pulls data from a json file and parses it. Its throwing me a nil error because some of the json entries don't have the "colors" field/array. How would i account for this and put in "ERROR" as the text for the ones that didn't.
func getData2(){
let dataPath = NSBundle.mainBundle().pathForResource("cardata", ofType: "json")
if let JSONData = NSData(contentsOfFile: dataPath!)
{
do
{
if let dictionariesArray = try NSJSONSerialization.JSONObjectWithData(JSONData, options: NSJSONReadingOptions()) as?
[[String: AnyObject]]
{
for dictionary in dictionariesArray
{
let name = dictionary["name"] as! String
let type = dictionary["type"] as! String
let text = String(dictionary["text"])
if let printingsArray = dictionary["colors"] as? [String]
{
let printingsString = String(printingsArray.joinWithSeparator("-"))
nameColor[name] = printingsString
}
nameType[name] = type
nameText[name] = text
}
}
}
catch
{
print("Could not parse file at")
}
}
struct Card {
let name: String
let type: String
let colorr: String
let textt: String
init(name: String, type: String, textt: String, colorr:String) {
self.name = name
self.type = type
self.textt = textt
self.colorr = colorr
}
}
var goodCard = [Card]()
for (cardName, cardType) in nameType {
let cardText = nameText[cardName]!
let cardColor = nameColor[cardName]! //fatal error: unexpectedly found nil while unwrapping an Optional value
goodCard.append(Card(name: cardName, type: cardType, textt: cardText, colorr: cardColor))
}
if typee != "" {
let redDogs = goodCard.filter {$0.type == typee}
print(redDogs)
}
You should avoid using the forced unwrapping operator ! whenever possible. If you use it and the Optional is nil then when you use the variable it will throw an exception. Instead use optional binding to unwrap the value:
// optional binding
if let cardColor = nameColor[cardName] {
// do something with the value
}

struggling with JSON parsing in swift

I am trying to load data in JSON format from my server into IOS application.
Here is my JSON:
[
{
"BankName": "bank1",
"CurrencyName": "cur1",
"SellRate": "0.65",
"BuyRate": "0.55",
"OfficialRate": "0.6"
},
{
"BankName": "bank1",
"CurrencyName": "cur2",
"SellRate": "1.65",
"BuyRate": "1.55",
"OfficialRate": "1.6"
}
]
There are 2 files in my project:
1:
import Foundation
class Shot {
var bankName: String!
var currencyName: String!
var sellRate: String!
var buyRate: String!
var offRate: String!
init (data: NSDictionary) {
self.bankName = getStringFromJSON(data, key:"BankName")
self.currencyName = getStringFromJSON(data, key:"CurrencyName")
self.sellRate = getStringFromJSON(data, key:"SellRate")
self.buyRate = getStringFromJSON(data, key:"BuyRate")
self.offRate = getStringFromJSON(data, key: "OfficialRate")
}
func getStringFromJSON(data: NSDictionary, key: String) -> String {
if let info = data[key] as? String{
return info
}
return ""
}
}
2:
import Foundation
class JsonTest {
func loadJson(completion: ((AnyObject) -> Void)!) {
var urlString = "http://a.com/g.php"
let session = NSURLSession.sharedSession()
let sourceUrl = NSURL(string: urlString)
var task = session.dataTaskWithURL(sourceUrl!){
(data, response, error) -> Void in
if error != nil {
println(error.localizedDescription)
} else {
var error: NSError?
var jsonData = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: &error) as NSArray
var shots = [Shot]()
println(jsonData)
for shot in jsonData{
let shot = Shot(data: shot as NSDictionary)
shots.append(shot)
}
println(shots) //[jsontest.Shot, jsontest.Shot]
}
}
task.resume()
}
}
I am trying to populate array automatically when my app starts. To do it I have a code in my mainViewController class.
override func viewDidLoad() {
super.viewDidLoad()
let api = JsonTest()
api.loadJson(nil)
}
The problem occurs when I try to print shots variable in the second file.
it returns [jsontest.Shot, jsontest.Shot] when I was expecting the array of dictionaries.
println(jsonData) works fine and shows JSON data from URL.
Can anybody advise what is wrong in my program?
"shots" is an array of instances of Shot, not a dictionary:
class Shot {
var bankName: String!
var currencyName: String!
var sellRate: String!
var buyRate: String!
var offRate: String!
init (data: NSDictionary) {
self.bankName = getStringFromJSON(data, key:"BankName")
self.currencyName = getStringFromJSON(data, key:"CurrencyName")
self.sellRate = getStringFromJSON(data, key:"SellRate")
self.buyRate = getStringFromJSON(data, key:"BuyRate")
self.offRate = getStringFromJSON(data, key: "OfficialRate")
}
func getStringFromJSON(data: NSDictionary, key: String) -> String {
if let info = data[key] as? String{
return info
}
return ""
}
}
var shots = [Shot]()
let urlString = "http://almazini.lt/getrates.php"
let sourceUrl = NSURL(string: urlString)
// Using NSData instead of NSURLSession for experimenting in Playground
let data = NSData(contentsOfURL: sourceUrl!)
var error: NSError?
// As I'm using Swift 1.2 I had to change "as" with "as!"
let jsonData = NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers, error: &error) as! NSArray
for shot in jsonData{
let shot = Shot(data: shot as! NSDictionary)
shots.append(shot)
}
println(shots[0].bankName)
Update for Swift 2
var shots = [Shot]()
let urlString = "http://almazini.lt/getrates.php"
// Using NSData instead of NSURLSession for experimenting in Playground
if let sourceUrl = NSURL(string: urlString) {
NSURLSession.sharedSession().dataTaskWithURL(sourceUrl, completionHandler: { (data, response, error) in
if error == nil {
if let data = data, jsonData = try? NSJSONSerialization.JSONObjectWithData(data, options: []), jsonArray = jsonData as? [NSDictionary] {
for item in jsonArray {
let shot = Shot(data: item)
shots.append(shot)
}
print(shots[0].bankName)
} else {
print("no JSON data")
}
} else {
print(error!.localizedDescription)
}
}).resume()
}
Seems like there are two problems:
You're trying to use println to debug instead of setting a breakpoint and checking your objects values.
You have not created a description or debugDescription property for your object, so println on your object is just using some default implementation.
shots is an array of your custom object, so when you call println, it's using the description for Array, which prints out the objects in the array, comma separated, and within square brackets.
The default description property for classes in Swift just prints the class name.
Ideally, you should just use a break point to check the values of your object to be certain it initialized correctly, but if it's actually important to get them to print right, it's only a matter of implementing the description property:
override var description: String {
get {
// build and return some string that represents your Shot object
}
}

Resources