Cannot call value of non-function type 'SwipeCard' in the function error in Swift

Cannot call value of non-function type 'SwipeCard' in the function error in Swift

我目前正在尝试实现一个 cocoa pod,但是在使用它时,我在函数“func cardStack”中收到错误“无法调用非函数类型 'SwipeCard' 的值”。这可能是一个愚蠢的错误,因为我只是在学习如何在 swift.

中编码
import UIKit
import Shuffle

class CardsViewViewController: UIViewController, SwipeCardStackDataSource {
    
    let card = SwipeCard()
    
    let cardStack = SwipeCardStack()
    
    let cardImages = [
        UIImage(named: "cardImage1"),
        UIImage(named: "cardImage2"),
        UIImage(named: "cardImage3")
    ]
    
    
    override func viewDidLoad() {
              super.viewDidLoad()
              view.addSubview(cardStack)
              cardStack.frame = view.safeAreaLayoutGuide.layoutFrame
        
                cardStack.dataSource = self
          }
    
    func cardStack(_ cardStack: SwipeCardStack, cardForIndexAt index: Int) -> SwipeCard {
      return card(fromImage: cardImages[index])
    }

    func numberOfCards(in cardStack: SwipeCardStack) -> Int {
      return cardImages.count
    }
}

包中也包含一个函数,但根据安装指南,这是我应该做的(但我选择只实现 card = SwipeCard() 而不是放置整个函数) :

1 - 通过子类化 SwipeCard 或直接设置其属性来创建您自己的卡片:

func card(fromImage image: UIImage) -> SwipeCard {
  let card = SwipeCard()
  card.swipeDirections = [.left, .right]
  card.content = UIImageView(image: image)
  
  let leftOverlay = UIView()
  leftoverlay.backgroundColor = .green
  
  let rightOverlay = UIView()
  rightOverlay.backgroundColor = .red
  
  card.setOverlays([.left: leftOverlay, .right: rightOverlay])
  
  return card
}

2 - 从 card(fromImage:) 返回的卡片显示图像,可以向左或向右滑动,并且具有两个方向的叠加视图。

我哪里错了?

非常感谢!

您必须删除 let card = SwipeCard() 并仅保留以下功能:

func card(fromImage image: UIImage) -> SwipeCard {
  let card = SwipeCard()
  card.swipeDirections = [.left, .right]
  card.content = UIImageView(image: image)
  let leftOverlay = UIView()
  leftoverlay.backgroundColor = .green
  let rightOverlay = UIView()
  rightOverlay.backgroundColor = .red
  card.setOverlays([.left: leftOverlay, .right: rightOverlay])
  return card
}

这基本上就是你在里面调用的那个

 func cardStack(_ cardStack: SwipeCardStack, cardForIndexAt index: Int) -> SwipeCard

错误告诉您您正在尝试调用 SwipeCard 实例而不是方法。

我认为你指的是常量 let card 而不是上面函数中的函数。