19

I am currently using the ObjectMapper for Swift (see: https://github.com/Hearst-DD/ObjectMapper/) to convert a String from a HTTP Request to an object of a custom class. The JSON I get from the request is a JSON Array, and I would like to convert this to an Array from type CustomObject.

I have tried it like this:

var object = Mapper<Array<CustomObject>>().map(string: json)

But then I get an error: Can not find member 'map'.

How should this be done?

Edit: this is my CustomObject Class, from now called ProductVariant:

public class ProductVariant: Mappable {

    /* Attributes */

    public var id = 0
//    var size : Size = nil
    public var SKU = ""
    public var stock = 0
    public var numberOfDefects = 0

    /* Constructors */

    public init?() {
        // Empty Constructor
    }

    required public init?(_ map: Map) {
        mapping(map)
    }

    /* Methods */

    public func mapping(map: Map) {
        id <- map["id"]
        SKU <- map["SKU"]
        stock <- map["stock"]
        numberOfDefects <- map["numberOfDefects"]
    }
}
3
  • I would guess you haven't implemented the " Mappable protocol as given in the ObjectMapper documentation. Can you post your CustomObject mapping if this is not the case? Commented Mar 24, 2015 at 16:38
  • Thanks for your comment! I have implemented the Mappable protocol, and I have updated my question. Commented Mar 24, 2015 at 16:46
  • If you are using ObjectMapper to map HTTP responses, you should consider using AlamofireObjectMapper. It is a simple extension to Alamofire which automatically converts your responses into swift objects using ObjectMapper. Full disclosure: I am the author of both ObjectMapper and AlamofireObjectMapper Commented May 16, 2015 at 16:37

3 Answers 3

35

I have found a solution, which seems to be working:

var list: Array<ProductVariant> = Mapper<ProductVariant>().mapArray(string: json)

When I loop through the array, it gives me the correct attributes for the CustomObject.

My mistake was that I tried to put the Array in the type of the Mapper, as shown in my question.

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

3 Comments

Thanks, I looking for this. May I know the source you found ? I cannot find in their github page.
@Megamind I just figured it out with trial on error, cant remember that I have found it anywhere online
Just a little note that Mapper has multiple mapArray functions. You want to hit the mapArray(JSONString: String) one not the mapArray(JSON: AnyObject?) one.
3

Another option is

let products = Mapper<ProductVariant>().mapArray(JSONString: json)

Comments

0

I think you need to install the object mapper pod and import ObjectMapper in your file

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.