swift – 对Enum的成员’init(from :)’的模糊引用

前端之家收集整理的这篇文章主要介绍了swift – 对Enum的成员’init(from :)’的模糊引用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
假设我们有这个枚举:
  1. enum NumberEnumSpecial: Int32 {
  2. case two = 2,three = 3
  3. }

我想用Int32初始化它,所以我用它:

  1. let myEnum = NumberEnumSpecial.init(rawValue: 2)

这适用于游乐场项目,但不适用于我的常规App项目.我收到此错误的完全相同的代码

  1. Ambiguous reference to member 'init(from:)'

  1. /Users/sjoerd/GitHub/flitsmeister-ios/app/Flitsmeister7/Model/Melding/DangerZone.swift:91:22: error: ambiguous reference to member 'init(from:)'
  2. let myEnum = NumberEnumSpecial.init(rawValue: 2)
  3. ^~~~~~~~~~~~~~~~~
  4. Swift.RawRepresentable:2:24: note: found this candidate
  5. public convenience init(from decoder: Decoder) throws
  6. ^
  7. Swift.RawRepresentable:2:24: note: found this candidate
  8. public convenience init(from decoder: Decoder) throws
  9. ^
  10. Swift.RawRepresentable:2:24: note: found this candidate
  11. public convenience init(from decoder: Decoder) throws
  12. ^
  13. Swift.RawRepresentable:2:24: note: found this candidate
  14. public convenience init(from decoder: Decoder) throws
  15. ^
  16. Swift.RawRepresentable:2:24: note: found this candidate
  17. public convenience init(from decoder: Decoder) throws
  18. ^
  19. Swift.RawRepresentable:2:24: note: found this candidate
  20. public convenience init(from decoder: Decoder) throws
  21. ^
  22. Swift.RawRepresentable:2:24: note: found this candidate
  23. public convenience init(from decoder: Decoder) throws
  24. ^
  25. Swift.RawRepresentable:2:24: note: found this candidate
  26. public convenience init(from decoder: Decoder) throws
  27. ^
  28. Swift.RawRepresentable:2:24: note: found this candidate
  29. public convenience init(from decoder: Decoder) throws
  30. ^
  31. Swift.RawRepresentable:2:24: note: found this candidate
  32. public convenience init(from decoder: Decoder) throws
  33. ^
  34. Swift.RawRepresentable:2:24: note: found this candidate
  35. public convenience init(from decoder: Decoder) throws
  36. ^
  37. Swift.RawRepresentable:2:24: note: found this candidate
  38. public convenience init(from decoder: Decoder) throws
  39. ^
  40. Swift.RawRepresentable:2:24: note: found this candidate
  41. public convenience init(from decoder: Decoder) throws
  42. ^
  43. Swift.RawRepresentable:2:24: note: found this candidate
  44. public convenience init(from decoder: Decoder) throws
  45. ^
  46.  
  47.  
  48.  
  49. Build Failed 13/10/2017,09:32

点击候选人没有任何效果.

如果你问我,代码中的某个Enum似乎有一个init(from)实现在我的枚举上导致这个错误.但是搜索这个文本没有给我带来任何结果.

这个错误是什么?如何找出造成这种情况的原因?

使用Swift 3.2和XCode9.0

目前解决方法

  1. enum NumberEnumSpecial: Int32 {
  2. case two = 2,three = 3
  3.  
  4. init?(withSpecialNumber number : Int32) {
  5. self.init(rawValue: number)
  6. }
  7. }
我使用Xcode 9.2 beta(9C32c)时遇到了同样的问题,如果它是一个bug,它仍然没有在这个版本中修复.我发现了一种解决方法,可以在不覆盖init的情况下使错误消失.

我改变了这个:

  1. NumberEnumSpecial.init(rawValue: 2)

对此:

  1. NumberEnumSpecial(rawValue: 2)

猜你在找的Swift相关文章