2

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

nhgrif
  • 61,578
  • 25
  • 134
  • 173
cyril
  • 3,020
  • 6
  • 36
  • 61
  • 1
    Have you added the `damerauLevenshteinTo` member to your custom class? – Rashwan L Jan 06 '16 at 11:32
  • 1
    Post the source code of your String extension. Preferably just the part where you declare this method – Arc676 Jan 06 '16 at 11:36
  • change class CustomClass: AnyObject to class CustomClass: NSObject – Hitendra Solanki Jan 06 '16 at 11:43
  • @RashwanL not good with terminology, mind explaining what you mean? – cyril Jan 06 '16 at 11:54
  • @HitendraHckr still the same error :( – cyril Jan 06 '16 at 11:55
  • @cyril, in you class that you don´t have an error were you can use `damerauLevenshteinTo`you must have some reference to be able to get that. Add the same reference to your own class. – Rashwan L Jan 06 '16 at 11:56
  • @RashwanL nope there's nothing :/ What's more is that copy pasting the class to an empty project works fine as well – cyril Jan 06 '16 at 12:01
  • Is it a String extension or NSString extension? Is the extension inside a framework? Is it public? – Sulthan Jan 06 '16 at 12:06
  • @Sulthan it's technically an NSString extension (so the error is `Value of type NSString has no member 'damerauLevenshteinTo'`) but I wanted to simply things for the sake of the question and no and yes to the rest of the questions – cyril Jan 06 '16 at 12:08
  • 2
    Most probably your custom class source file is "disconnected" from the rest of you project. I'd try recreating the class source file in Xcode, making sure it's included into your XCode project. – dfrib Jan 06 '16 at 12:14
  • @cyril dont simplify. That makes us unable to answer. – Sulthan Jan 06 '16 at 12:17
  • 1
    did you try cleaning your xcode project? mysteriously and very rarely this helps in some cases but I think the problem is not like that. – Stefan Jan 06 '16 at 12:19
  • @Sulthan my bad, didn't think it'd make a difference here – cyril Jan 06 '16 at 12:21
  • @Stefan I've tried many times :( – cyril Jan 06 '16 at 12:21
  • @dfri that seemed to have solved it.... Weird, thanks!! – cyril Jan 06 '16 at 12:25
  • 1
    @cyril That's good to hear, happy to help. – dfrib Jan 06 '16 at 12:33

2 Answers2

2

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.

strangeluck
  • 846
  • 9
  • 10
0

In my case I removed extension file and create new one then paste my code in, it worked.

replapp
  • 31
  • 3