gpt4 book ai didi

swift - Java 中的静态 block 在 swift 中的等价物是什么

转载 作者:搜寻专家 更新时间:2023-10-31 23:01:30 25 4
gpt4 key购买 nike

如何在 Swift 中放置一个静态 block ,就像在 Java 中一样?我试过 static var block = {}但这行不通。它必须专门调用。

我想要的就像在 Java 中一样,静态大括号内的整个 block 在类初始化时执行。 Swift 中有类似的东西。我在整个互联网上搜索过,没有人有答案!类似的功能或解决方法也可以。

public class EnumRingLevel
{
public static final EnumRingLevel DEFAULT = new EnumRingLevel(
0, 0, "DEFAULT", 1000, 2000);

public static final EnumRingLevel SILENT = new EnumRingLevel(
10, 1, "SILENT", 1001, 2001);

public static final EnumRingLevel QUIET_BEEP = new EnumRingLevel(
20, 2, "QUIET_BEEP", 1002, 2002);

public static final EnumRingLevel NORMAL_BEEP = new EnumRingLevel(
30, 3, "NORMAL_BEEP",1003, 2003);

private final int gdbval;
private final int gindex;

public final String ginternalname;

private final int gcaptionId;
private final int gdisplaycaptionId;

private static EnumRingLevel[] gRingLevelsSortedOnIndex = null;
private static String[] gCaptionsSortedOnIndex = null;

static
{
gRingLevelsSortedOnIndex = new EnumRingLevel[6];

gRingLevelsSortedOnIndex[0] = DEFAULT;
gRingLevelsSortedOnIndex[1] = SILENT;
gRingLevelsSortedOnIndex[2] = QUIET_BEEP;
gRingLevelsSortedOnIndex[3] = NORMAL_BEEP;
gRingLevelsSortedOnIndex[4] = LOUD_BEEP;
gRingLevelsSortedOnIndex[5] = CUSTOM;


gCaptionsSortedOnIndex = new String[6];

for(int i=0;i<gRingLevelsSortedOnIndex.length;i++)
{
gCaptionsSortedOnIndex[i] = gRingLevelsSortedOnIndex[i].getCaption();
}
}

private EnumRingLevel(
int dbval, int index, String internalname
, int captionResource, int displaycaptionResource)
{
//private constructor

gdbval = dbval;
gindex = index;
ginternalname = internalname;
gcaptionId = captionResource;
gdisplaycaptionId = displaycaptionResource;

}
}

最佳答案

试试这个,

private static var gRingLevelsSortedOnIndex: [EnumRingLevel] = {
return [DEFAULT, SILENT, QUIET_BEEP, NORMAL_BEEP, LOUD_BEEP, CUSTOM]
}()

关于swift - Java 中的静态 block 在 swift 中的等价物是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34065585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com