快速创建通用变量字典

时间:2019-05-29 00:55:25

标签: swift dictionary generics

我正在寻找一种在字典中存储泛型子类的方法,但是遇到了问题。

在下面的示例中,我试图创建一个对象字典,该对象将成为Entity<T>的子类。我已成功将子类FloatEntityStringEntity的单个实例存储在Entity类型的var中,但是当我尝试将它们存储在Dictionary<String, Entity>类型的字典中时,出现错误。

class Entity<T> {
    var _value: T
    var value: T { get { return _value } set {_value = newValue}}

    init (defaultValue: T) {
        _value = defaultValue
    }
}

class FloatEntity: Entity<Float> {
}

class StringEntity: Entity<String> {
}

func run () {
    let variable1: Entity = FloatEntity (defaultValue: 1)
    let variable2: Entity = StringEntity (defaultValue: "")
    var dictionary: Dictionary<String, Entity> = [
        "One": FloatEntity (defaultValue: 1),
        "Two": StringEntity (defaultValue: ""),
    ]
    print (variable1)
    print (variable2)
    print (dictionary)
}

错误:

  

无法将“ FloatEntitiy”类型的值转换为预期的字典值   输入“实体”

有人可以建议我如何使用字典来存储这样的泛型吗?

3 个答案:

答案 0 :(得分:2)

import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; public class Testing extends JPanel implements ActionListener { private int x, y, xinc, yinc; public void paintComponent(Graphics g) { // added this statement. super.paintComponent(g); Graphics2D gimg = (Graphics2D) g; Rectangle2D rect = new Rectangle2D.Double(50, 50, 10, 300); CubicCurve2D cub1 = new CubicCurve2D.Double(60, 50, x, 10, y, 100, 200, 50); gimg.draw(cub1); CubicCurve2D cub2 = new CubicCurve2D.Double(60, 100, x, 60, y, 150, 200, 100); gimg.draw(cub2); CubicCurve2D cub3 = new CubicCurve2D.Double(60, 150, x, 110, y, 200, 200, 150); gimg.draw(cub3); Line2D l1 = new Line2D.Double(200, 50, 200, 150); gimg.draw(l1); GeneralPath gp1 = new GeneralPath(); GeneralPath gp2 = new GeneralPath(); gp1.moveTo(60, 50); gp1.curveTo(x, 10, y, 100, 200, 50); gp1.lineTo(200, 100); gp1.curveTo(y, 150, x, 60, 60, 100); gp1.lineTo(60, 50); gimg.setColor(Color.WHITE); gimg.fill(gp1); gimg.setColor(Color.GRAY); gimg.fill(rect); gp2.moveTo(60, 100); gp2.curveTo(x, 60, y, 150, 200, 100); gp2.lineTo(200, 150); gp2.curveTo(y, 200, x, 110, 60, 150); gp2.lineTo(60, 100); gimg.setColor(Color.RED); gimg.fill(gp2); } public void moveFlag() { // waving animation Timer timer = new Timer(20, this); timer.start(); // moved the next four statements from paintComponents. They // are initializations. In paintComponent you kept resetting them. xinc = 10; yinc = 10; x = 101; y = 151; repaint(); } @Override public void actionPerformed(ActionEvent e) { //moved all of these from moveFlag to here. This is called by //the timer event to update the x and y coordinates. x = x + xinc; y = y + yinc; if (x <= 60 || (x + xinc) >= 130) xinc *= -1; if (y <= 50 || (y + yinc) >= 230) yinc *= -1; // revalidate(); repaint(); } public static void main(String[] args) { JFrame frame = new JFrame(); Testing test = new Testing(); frame.setContentPane(test); frame.setSize(500, 500); frame.setVisible(true); test.moveFlag(); } } variable1的类型不是variable2,但实际上是两种不相关的类型EntityEntity<Float>。使用Entity<String>来团结他们:

protocol

答案 1 :(得分:1)

从您到目前为止提供的内容来看,这似乎不太适合泛型使用。如果想法是将值存储在字典中,其中每个值可能是字符串或可能是浮点数,这听起来更像是带有关联值的枚举(这是游乐场代码):

enum StringOrFloat {
    case string(String)
    case float(Float)
}
let v1 = StringOrFloat.string("meaning of life")
let v2 = StringOrFloat.float(42)
var d = Dictionary<String,StringOrFloat>()
d["hey"] = v1
d["ho"] = v2

这是一种提取值的方法:

if let val = d["hey"] {
    switch val {
    case .string(let s):
        print("it is a string, namely", s)
    case .float(let f):
        print("it is a float, namely", f)
    }
}

答案 2 :(得分:0)

Swift泛型是不变的,因此Entity<B>并不是Entity<A>的子类,即使B是A的子类也是如此。因此,您无法使用{{1}的超类来存储相同的类型}在字典中。

通过使用Entity作为字典值,例如使用以下代码:-

AnyObject

我认为您不能从中提取经过类型擦除的var dictionary: Dictionary<String, AnyObject> = [ "One": FloatEntity (defaultValue: 1), "Two": FloatEntity (defaultValue: 2), ] (很高兴得到纠正)。但是您可以执行以下操作:-

Entity