使用保留关键字枚举案例

时间:2015-07-09 10:55:47

标签: swift enums

是否可以将保留关键字用作枚举案例?

例如:

enum MyEnum {
  case Self // compiler complains here
  case AnotherCase
}

在其他语言中,这可以通过以某种方式转义关键字来实现,例如在scala中我们使用反引号,例如。

`type`

可以用作标识符,尽管type是保留关键字。

swift中有类似的东西吗?

1 个答案:

答案 0 :(得分:21)

来自Swift语言指南(Naming Constants & Variables section

如果您需要为保留的Swift关键字指定与常量或变量相同的名称,请在将该关键字用作名称时将其包含在后面的刻度线(`)中。 但是,除非您绝对没有选择,否则请避免使用关键字作为名称。

var fill = new ol.style.Fill({color: 'GREEN'});
var stroke = new ol.style.Stroke({color: 'GREEN'});

var styles = [new ol.style.Style({
    image: new ol.style.Circle({
        fill: fill,
        stroke: stroke,
        radius: 5
    })
})];


var fires = new ol.layer.Image({
    name: 'fires',
    source: new ol.source.ImageWMS({
        url: 'https://firms.modaps.eosdis.nasa.gov/wms/',
        params: {
            'LAYERS': 'fires24',
            'VERSION': '1.1.1'
        }
    }),
    style: styles
});

使用或不使用反引号

add_cart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          String colliValue = colli.getText().toString());
          String prezzoValue = prezzo.getText().toString());
          String quantitaValue = quantita.getText().toString());
        }
});
相关问题