【问题标题】:Swift Editor Placeholder in source file源文件中的 Swift 编辑器占位符
【发布时间】:2017-05-04 12:23:36
【问题描述】:

您遇到快速错误“源文件中的 Swift 编辑器占位符”的问题 这是我的代码

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: <#T##IndexPath#>) as! CustomBrandCell

    let brandImage: UIImage = UIImage(named: self.brands[indexPath.row].name)!

    cell.brandImageView.image = brandImage

    return cell
}

【问题讨论】:

  • 你搜索过SO吗?
  • 看看这里,例如:stackoverflow.com/questions/37352738/…。在您的情况下,问题出在let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: &lt;#T##IndexPath#&gt;) as! CustomBrandCell 这一行。注意 ,它是一个占位符,您必须用一些“真实”代码替换它...在您的情况下是 indexPath 类型的参数
  • 错误非常很简单,第二行有一个明显的占位符。我已投票以“一个简单的印刷错误”结束这个问题。

标签: ios swift placeholder


【解决方案1】:

我在 SO 上多次发现相同的问题。但他们都没有给出我正在寻找的答案。

当您的代码中有其中一个(其中显示为蓝色背景的“字符串”)时,您会得到 Placeholder in source file

占位符是为我们程序员准备的。它说“这里应该是字符串类型的值”。您可以单击它并开始输入,以简单地将其替换为例如变量名称。您也可以按 Tab 键自动选择下一个占位符。这在您调用具有多个参数(因此需要多个占位符)的函数时非常有用。

占位符实际上只是普通文本 (),但 XCode 会将其“翻译”成它的样子。

在你的情况下,错误在第三行。

...withReuseIdentifier: "Cell", for: <#T##IndexPath#>) as! CustomBrandCell

如您所见,&lt;#T##IndexPath#&gt; 是一个占位符,就像我之前提到的普通文本一样。您可能希望这是indexPath

【讨论】:

    【解决方案2】:

    尝试 cmd + shift + k 清理项目并再次运行您的代码。这为我解决了这个问题。

    【讨论】:

      【解决方案3】:

      试试这个。希望能解决你的问题

      public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
      
            // get a reference to your storyboard cell
           let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) as! CustomBrandCell
      
           let brandImage: UIImage = UIImage(named: self.brands[indexPath.row].name)!
      
           cell.brandImageView.image = brandImage
      
           return cell
      }
      

      【讨论】:

        猜你喜欢
        • 2016-09-18
        • 2019-04-30
        • 1970-01-01
        • 2020-02-22
        • 1970-01-01
        相关资源
        最近更新 更多