0

Using SwiftyJSON I loaded a JSON array with the following structure.

 [
  {
  "x": 2,
  "y": 2,
  "r": 100,
  "description": {"name":"bob","institution":"NYU","technologies":[]}
  },
  {
  "x": 4,
  "y": 4,
  "r": 200,
  "description": {"name":"bob","institution":"NYU","technologies":[]}  },...
]

Problem

I want to create an object from each element in this array.

My approach

    let chartPoints: [ChartPointBubble] = json.array.map{
        ChartPointBubble(x: ChartAxisValueFloat(CGFloat($0.x), labelSettings:labelSettings),
            y:ChartAxisValueFloat(CGFloat($0.y)), diameterScalar: $0.r, description: $0.description)
    }

The error

Cannot invoke map with an argument of list type ((_) -> _)

2 Answers 2

1

Updated, not closure.

var chartPoints = [ChartPointBubble]()
for (index: String, subJson: JSON) in json {
   let pointBubble = ChartPointBubble(
        x: ChartAxisValueFloat(CGFloat(subJson.x.int),  
        labelSettings:labelSettings),
        y:ChartAxisValueFloat(CGFloat(subJson.y.int)), 
        diameterScalar: subJson.r.int, 
        description: subJson.description)

    chartPoints.append(pointBubble)
}

EDIT: I took a look at the docs. Think best approach is to loop it. Also, not sure of type for your: ChartAxisValueFloat.description.

Sign up to request clarification or add additional context in comments.

5 Comments

The README (github.com/SwiftyJSON/SwiftyJSON/blob/master/README.md) suggests the type is JSON
This gives me the error, cannot invoke map of argument list with type ((JSON) --> ChartPointBubble)
ChartAxisValueFloat.description is itself a JSON array, as indicated in the OP.
In that case the code above should probably work... :)
I think you mixed object and subJson
0

The error is pointing out the fact that you are passing a closure of type ()->() into the array's 'map' method. In this case, the map method needs to receive an argument of type (whateverTypeIsAlreadyInTheArray) -> (ChartPointBubble).

You might be able to fix this by:

  1. adding the line "(var arrayItem) -> ChartPointBubble in" right after the first open curly brace found after the method 'map'. (I'm pretty sure you don't have to specify the type of arrayItem as it can be inferred from the items already in the array)
  2. replacing '$0' with 'arrayItem'
  3. returning the new ChartPointBubble you are creating

Edit: - final code should look like this

let chartPoints: [ChartPointBubble] = json.array.map{
    (var arrayItem) -> ChartPointBubble in
        return ChartPointBubble(
            x: ChartAxisValueFloat(CGFloat(arrayItem.x),  
            labelSettings:labelSettings),
            y:ChartAxisValueFloat(CGFloat(arrayItem.y)), 
            diameterScalar: object.r, 
            description: object.description)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.