微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift switch case编译错误

以下代码是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
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐