Swift中#Selector掉用小技巧

场景:不同场景掉用相同方法的场景,方法内部需要区分打点事件;

代码示例:

    	let tapGesture = UITapGestureRecognizer(target: self, action: #selector(goPeanutPage))
    	// 方式2:
		headerView.peanutEnterBtn.addTarget(self, action: #selector(goPeanutPage), for: .touchUpInside)
	@objc func goPeanutPage(sender: AnyObject!) {
		if sender.isKind(of: UITapGestureRecognizer.self) {
			DiscoveryEventsHandler.background_paywall_click_background()
		} else {
			DiscoveryEventsHandler.background_paywall_click_background_icon()
		}
	}

注意:当然使用is也是可以的,根据情况而定;

你可能感兴趣的:(印象笔记,小技巧,swift)