For 循环中的 UITextView 缺少参数

UITextView in For Loop missing argument

Xcode 在调用 textShown1 = CustomTextView 行时抛出 missing argument for parameter textContainer。我很好奇为什么它只在 for 循环内抛出错误,而在循环外却不会。

for i in 1...paragraphCount {
    // Missing 'argument textContainer in call below', various other errors like 'consuective statements on a line must be separated by ;'
    textShown1 = CustomTextView(frame: CGRect(x: 0, y: 125 + (50*i)), width: CGRectGetWidth(self.frame), height: CGRectGetHeight(self.frame)-400))

    paragraphs.append(textShown1)
    self.view?.addSubview(textShown1)

}

    textShown1 = CustomTextView(frame: CGRect(x: 0, y: 50, width: CGRectGetWidth(self.frame), height: CGRectGetHeight(self.frame)-400))

我认为它可能会导致问题,因为它也不是 CGFloat,但在尝试转换它之后它仍然会抛出错误。我将 CustomTextView 更改为 UITextView 只是为了让代码不那么混乱:

for i in 1...8 {

    var converted: CGFloat = CGFloat(i)
    // Missing 'argument textContainer in call below', various other errors like 'consuective statements on a line must be separated by ;'
    textShown1 = UITextView(frame: CGRect(x: 0, y: (50 * converted)), width: CGRectGetWidth(self.frame), height: CGRectGetHeight(self.frame)-400)

    paragraphs.append(textShown1)
    self.view?.addSubview(textShown1)

}

该错误具有误导性。你的问题是i。这是一个 Int。您必须将其转换为 CGFloat 才能使其正常工作。

var converted:CGFloat = CGFloat(i)
textShown1 = UITextView(frame: CGRectMake(0, 50 * converted, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - CGFloat(400)))
CGRect(x: 0, y: 125 + (50*i)), width: CGRectGetWidth(self.frame), height: CGRectGetHeight(self.frame)-400)

应该是

CGRectMake(0, CGFloat(125 + (50*i)), CGRectGetWidth(self.view.frame), CGFloat(CGRectGetHeight(self.view.frame)-400))