8

I have been trying refactor code by thanking functions and adding them in a separate file extension of a ViewController

What I have in this extension is a function that adds gesture recognizers to some views that have references to functions that I have placed in other file extension of the same ViewController. On build I am getting this error "Expected expression in Swift key path"

What is causing this error?

ISS
  • 406
  • 6
  • 24

3 Answers3

36

I got this error when I accidentally left a backslash after a bracket in my object init:

init(for note: Note, atAnchor anchor: ARAnchor) {\

    let billboardNode = note.type.basicNode

    self.node = billboardNode
    self.text = note.description ?? "[No Text]"
    self.type = note.type
    addText()
}

Removing the backslash fixed the error. Check out this answer by user eharo2 for details about why!

9

Maybe you have typo, for example:

\//
//  MyClass.swift
//  yyy
//
//  Created by xxx on 4/25/19.
//  Copyright © 2019 xxx. All rights reserved.
//

import Foundation

class MyClass {

}

In this Case I accidentally wrote '\' at start.

levan
  • 440
  • 1
  • 8
  • 19
3

This error message is a straight forward error, where the parser is expecting a keyPath, given the usage of the backslash (assuming that this is the issue causing the problem, considering that the previous answer from @Edmund Holderbaum was accepted).

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 5.0).”

Key-Path Expression. A key-path expression refers to a property or subscript of a type. You use key-path expressions in dynamic programming tasks, such as key-value observing. They have the following form:

\type name.path

There seems to be few places in the Swift grammar where a backslash is used, other than in string interpolation (interpolated-text-item) or in a escaping sequence (escape-sequence).

from Summary of the Grammar - Lexical Structure

“key-path-expression → \ type opt . key-path-components”

Take a look at this excellent post to learn more about keyPaths in Swift: https://www.swiftbysundell.com/posts/the-power-of-key-paths-in-swift

eharo2
  • 2,553
  • 1
  • 29
  • 39