【问题标题】:Consecutive Declarations on Line Must be Separated By ";"行上的连续声明必须用“;”分隔
【发布时间】:2015-11-29 06:29:35
【问题描述】:
func createButtonWithTitle(title: String) -> UIButton {

  let button = UIButton.buttonWithType(.System) as UIButton
  button.frame = CGRectMake(0, 0, 20, 20)
  button.setTitle(title, forState: .Normal)
  button.sizeToFit()
  button.titleLabel.font = UIFont.systemFontOfSize(15)
  button.setTranslatesAutoresizingMaskIntoConstraints(false)
  button.backgroundColor = UIColor(white: 1.0, alpha: 1.0)
  button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)

  button.addTarget(self, action: "didTapButton:", forControlEvents: .TouchUpInside)

  return button
}

UIButton 之前的第 1 行引发错误。 我该如何解决?

【问题讨论】:

  • 什么意思?!只需将 -&t; 更改为 -> 就可以了!这只是一个错字!我猜你是从网上复制的文字。

标签: ios swift swift2


【解决方案1】:

只需将 -> 替换为 ->,这就是它应该的样子。

附录:

您还没有显示您仍然遇到的后续错误;但是,我决定测试您的代码并设法在 Xcode 7.1(在 Swift 2 中)为您修复这些问题。

请查看 cmets 中所做的更改。

func createButtonWithTitle(title: String) -> UIButton 
{
    //buttonWithType is no longer available; please change it to
    //UIButton(type:)
    let button = UIButton(type: .System) as UIButton

    button.frame = CGRectMake(0, 0, 20, 20)
    button.setTitle(title, forState: .Normal)
    button.sizeToFit()

    //unwrap titleLabel using "!"
    button.titleLabel!.font = UIFont.systemFontOfSize(15)

    //translatesAutoresizingMaskIntoConstraints is a property and
    //it is no longer a member function
    button.translatesAutoresizingMaskIntoConstraints = false

    button.backgroundColor = UIColor(white: 1.0, alpha: 1.0)
    button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)

    button.addTarget(self, action: "didTapButton:", forControlEvents: .TouchUpInside)

    return button
}

【讨论】:

  • 好的,请问如何联系您?因为我将其更改为 -> 后仍然出现错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多