0

I have an object with HashMap called metadata (of type HashMap<String, String>), which I'm using in a Swift app.

All of the other properties of the object are accessible through Swift (via the bridging header), but metadata is not.

Is there something I need to be aware of when defining this type in Java? Here's the code:

package com.superpixel.advokit.temple8.domain;

import com.superpixel.advokit.temple8.api.network.ApiCallback;

import java.util.Date;
import java.util.HashMap;

public class User extends ApiResource {

    private String id;
    private boolean isLive;
    private Date created;
    private Date updated;
    private String email;
    private String forename;
    private String surname;

    private HashMap<String, String> metadata;

    // -- API

    public static void get(String id, ApiCallback<User> callback) {
        apiService.get(User.class, "/users/" + id, callback);
    }

    // -- Accessors

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public boolean isLive() {
        return isLive;
    }

    public void setLive(boolean live) {
        isLive = live;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getUpdated() {
        return updated;
    }

    public void setUpdated(Date updated) {
        this.updated = updated;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getForename() {
        return forename;
    }

    public void setForename(String forename) {
        this.forename = forename;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public HashMap<String, String> getMetadata() {
        return metadata;
    }

    public void setMetadata(HashMap<String, String> metadata) {
        this.metadata = metadata;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (isLive != user.isLive) return false;
        if (id != null ? !id.equals(user.id) : user.id != null) return false;
        if (created != null ? !created.equals(user.created) : user.created != null) return false;
        if (updated != null ? !updated.equals(user.updated) : user.updated != null) return false;
        if (email != null ? !email.equals(user.email) : user.email != null) return false;
        if (forename != null ? !forename.equals(user.forename) : user.forename != null) return false;
        if (surname != null ? !surname.equals(user.surname) : user.surname != null) return false;
        return metadata != null ? metadata.equals(user.metadata) : user.metadata == null;

    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (isLive ? 1 : 0);
        result = 31 * result + (created != null ? created.hashCode() : 0);
        result = 31 * result + (updated != null ? updated.hashCode() : 0);
        result = 31 * result + (email != null ? email.hashCode() : 0);
        result = 31 * result + (forename != null ? forename.hashCode() : 0);
        result = 31 * result + (surname != null ? surname.hashCode() : 0);
        result = 31 * result + (metadata != null ? metadata.hashCode() : 0);
        return result;
    }

}
Adam Colvin
  • 751
  • 1
  • 6
  • 16

1 Answers1

0

I'm not a Swift developer and so don't have a definitive answer, but Java developers recommend using the interface rather than the implementation class in a public API (Effective Java, Item 18). So you may have better luck defining metadata's type as java.util.Map, rather than HashMap.

tball
  • 1,984
  • 11
  • 21
  • This fixed the issue for me, though it would be nice to know exactly what the problem is. It might not be an issue with j2ObjC, because the interface file seems to be generated properly, even using HashMap. So maybe it's to do with how it's bridged into Swift. – Adam Colvin Mar 28 '17 at 14:57