ObjectMapper - 初始化对象IOS

时间:2015-11-01 22:46:54

标签: ios swift initialization swift2

让我头疼的简单事情 - 如何在没有任何JSON的情况下初始化符合可映射协议的对象。

我想做的只是在代码中初始化空的User对象:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                CustomDesktopPane pane = new CustomDesktopPane();
                JInternalFrame inFrm = new JInternalFrame("Ontop", true, true, true, true);
                inFrm.setSize(100, 100);
                inFrm.setLocation(150, 150);
                inFrm.setVisible(true);
                pane.add(inFrm);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(pane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class CustomDesktopPane extends JDesktopPane {

        public CustomDesktopPane() {
            setFont(UIManager.getFont("Label.font").deriveFont(24f));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            String text = "All your base are belong to us";
            Font font = g2d.getFont();
            FontMetrics fm = g2d.getFontMetrics();
            int x = (getWidth() - fm.stringWidth(text)) / 2;
            int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
            g2d.setColor(Color.WHITE);
            g2d.drawString(text, x, y);
            g2d.dispose();
        }

    }


}

然而,这给了我错误: “在调用中缺少参数#1的参数”

我能够在版本0.14中使用swift 1.2进行,但现在它无法正常工作。你们现在知道如何在swift 2和新的Object Mapper中做到这一点吗? (我知道如何使用json等初始化它,我只是想为其他目的初始化该对象而我无法弄清楚如何)

let user = User()

请帮忙!

2 个答案:

答案 0 :(得分:23)

以下内容应该有效:

class User: NSObject, Mappable {
var username: String?
var age: Int?
var weight: Double!
var array: [AnyObject]?
var dictionary: [String : AnyObject] = [:]
var bestFriend: User?                       // Nested User object
var friends: [User]?                        // Array of Users
var birthday: NSDate?

override init() {
    super.init()
}

convenience required init?(_ map: Map) {
    self.init()
}

// Mappable
func mapping(map: Map) {
    username    <- map["username"]
    age         <- map["age"]
    weight      <- map["weight"]
    array       <- map["arr"]
    dictionary  <- map["dict"]
    bestFriend  <- map["best_friend"]
    friends     <- map["friends"]
    birthday    <- (map["birthday"], DateTransform())
}
}

答案 1 :(得分:8)

上述答案的修正版:

init() {}
required convenience init?(_ map: Map) { self.init() }