You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xcode报错:Cannot call value of non-function type 'CGFloat' 求助

Fix for "Cannot call value of non-function type 'CGFloat'" in touchesBegan

Hey there! That error is popping up because of a small type mismatch in your loop. Let me break it down for you:

You’ve declared touch as AnyObject in your for loop, but touches is actually a Set<UITouch>. When you use AnyObject, the compiler can’t correctly recognize that location(_:) is a method belonging to UITouch — instead, it gets confused and treats location like a CGFloat property, which triggers the error you’re seeing.

The fix is straightforward: either remove the AnyObject type annotation (since Swift can infer the UITouch type automatically from the set) or explicitly type touch as UITouch. Here’s your corrected code snippet:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches { // Or use `for touch: UITouch in touches` for explicit typing
        let pointOfTouch = touch.location(self)
        let nodeITapped = atPoint(pointOfTouch)
        // Rest of your button/game launch logic here...
    }
}

A quick pro tip: In Swift, avoid using AnyObject when you know the specific type of elements in a collection. This lets the compiler catch type-related errors early and gives you proper autocompletion for methods like location(_:).

内容的提问来源于stack exchange,提问作者Aidan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:38:02