Xcode报错:Cannot call value of non-function type 'CGFloat' 求助
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

