Swift中的高级Java枚举 [英] Advanced Java enums in Swift
本文介绍了Swift中的高级Java枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
中的一个类的具有高级枚举:
公共枚举学生{
STUDENT_ONE (Steve,Jobs)
STUDENT_TWO(Tim,Cook)
private String _firstName;
private String _lastName;
}
如何在Swift中复制相同的行为?
解决方案
我正在尝试将Java代码转换为Swift,同样的事情,最终做了这样的事情:
公共枚举学生{
pre>
情况下STUDENT_ONE
情况下STUDENT_TWO
VAR姓:字符串{
获得{
开关自{
情况下.STUDENT_ONE:
返回 史蒂夫
情况下.STUDENT_TWO:
返回 添
$} b $ b}
}
var lastName:String {
get {
switch self {
case .STUDENT_ONE:
returnJobs
case .STUDENT_TWO:
returnCook
}
}
}
}
现在,这真是漫长而凌乱,我不是真的确定这是否是正确的方法,但我找不到有用的东西。我很想知道是否还有其他更好的方法。
I have a number of Java classes I need to convert to Swift code. One of the classes has an advanced enum:
public enum Student { STUDENT_ONE("Steve", "Jobs") STUDENT_TWO("Tim", "Cook") private String _firstName; private String _lastName; }
How can I replicate the same behavior in Swift?
解决方案I was trying to do the same thing with converting Java code to Swift, and ended up doing something like this :
public enum Student { case STUDENT_ONE case STUDENT_TWO var firstName: String { get { switch self { case .STUDENT_ONE: return "Steve" case .STUDENT_TWO: return "Tim" } } } var lastName: String { get { switch self { case .STUDENT_ONE: return "Jobs" case .STUDENT_TWO: return "Cook" } } } }
Now, this is really long and messy and I'm not really sure whether this is the right way to do it, but I couldn't find anything else that worked. I would love to know if there is some other better way to do it.
这篇关于Swift中的高级Java枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文