swift枚举了Kotlin中的Objective-C typedefs



我正在将一些业务逻辑从iOS转移到Kotlin,这种结构对我来说似乎很奇怪

// AttachmentType.h
typedef NS_ENUM(NSUInteger, AttachmentType) {
AttachmentType1 = 0,
AttachmentType2 = 1,
AttachmentType3 = 2
}

// PhotoType.swift
enum PhotoType {
case t1(AttachmentType1), t2(AttachmentType1), t3(AttachmentType1)
var attachmentType: AttachmentType {
switch self {
case .t1(let type):
return type
case .t3(let type):
return type
case .t3(let type):
return type
}
}
}

我对这里的ivarattachmentType感到困惑

  1. 这本质上是AttachmentType类型的变量吗?

  2. 这是否允许这两种类型的全部9个排列。例如:我可以实例化一个PhotoType吗?它表示带t1的AttachmentType1、带t2的AttachmntType1、带有t3的Attachment Type1、具有t1的AttachmentType2等。

  3. Kotlin的等价结构是什么?9个密封类?

  1. PhotoType使用枚举"关联值">

  2. 它确实允许以类型安全的方式创建9个案例。

  3. Kotlin中的以下结构实现了相同的目标:

``

sealed class PhotoType {
abstract val type: AttachmentType
}
data class t1(override val type: AttachmentType) : PhotoType()
data class t2(override val type: AttachmentType) : PhotoType()
data class t3(override val type: AttachmentType) : PhotoType()

```

最新更新