加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift switch case编译错误

发布时间:2020-12-14 04:27:25 所属栏目:百科 来源:网络整理
导读:以下代码是WWDC中的中级 Swift谈话示例的派生.我要做的是从一个键属性列表初始化一个模型类,它来自某种API. class Movie { var title: String init(title: String) { self.title = title }}func movieFromDictionary(dict: DictionaryString,AnyObject) - Mo
以下代码是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
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读