Search code examples
swiftswift-extensions

Why does my custom class not recognize swift extensions? (i.e. String)


I have a string extension in which I define a function that calculates the DamerauLevenshtein distance between two different strings. When adding the following code:

var result = "Hello"
result.damerauLevenshteinTo("Hellow") 

in any other class such as a UIViewController, UITableViewController, etc. the code compiles fine. As soon as I try adding it to my own class defined as class CustomClass: AnyObject I get the following error message.

Value of type String has no member 'damerauLevenshteinTo'

What am I missing? Thanks!

Update Copy pasting the entire class with the extension to an empty project compiles great... Moving it to the old project still gives me the error no matter what I try.

Update 2 It seems I can't use any custom classes or subclasses inside this class. Hm...

Update 3 Deleting the file and pasting the class in a new one got rid of the error after about the third time... Hurray


Solution

  • How did you add the class with the extension? I added some classes into my project which included extensions, and the rest of the code wouldn't recognize the extensions. It appears that this was because I had added the files using "Create folder references" option instead of "Create groups" option. I deleted what I had added, then added them again with "Create groups" option selected. This fixed it for me.

    I'm didn't pursue why using references might result in this behavior, because I had intended to add them with the "Create groups" option to begin with.