当前位置 : 主页 > 手机开发 > 其它 >

Swift switch case编译错误

来源:互联网 收集:自由互联 发布时间:2021-06-11
以下代码是WWDC中的中级 Swift谈话示例的派生.我要做的是从一个键属性列表初始化一个模型类,它来自某种API. class Movie { var title: String init(title: String) { self.title = title }}func movieFromDictiona
以下代码是WWDC中的中级 Swift谈话示例的派生.我要做的是从一个键属性列表初始化一个模型类,它来自某种API.

class Movie {
  var title: String

  init(title: String) {
    self.title = title
  }
}

func movieFromDictionary(dict: Dictionary<String, AnyObject>) -> Movie? {
  switch dict["title"] {
  case .Some(let movieTitle as String):
    return Movie(title: movieTitle)
  default:
    return nil
  }
}

当我尝试编译这些时,我收到以下错误:

Bitcast requires both operands to be pointer or neither
  %38 = bitcast i8* %37 to %SS, !dbg !161
Invalid operand types for ICmp instruction
  %39 = icmp ne %SS %38, null, !dbg !161
PHI nodes must have at least one entry.  If the block is dead, the PHI should be removed!
  %42 = phi i64 , !dbg !161
PHI node operands are not the same type as the result!
  %41 = phi i8* [ %38, %34 ], !dbg !161
LLVM ERROR: Broken function found, compilation aborted!
Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 1

有趣的是编辑器似乎对代码没问题.这是编译器错误还是代码有问题?

我同意评论者这是一个编译器错误,你应该向苹果报告.但是,您也可以通过这种方式实现它,这更简单,应该可以正常工作:

func movieFromDictionary(dict: Dictionary<String, AnyObject>) -> Movie? {
  if let title = dict["title"] as? String {
    return Movie(title: title)
  }
  else {
    return nil
  }
}
网友评论