4

I'm implementing a class but I'm getting this error:

Use of undeclared type 'myProtocol'

Here is my code:

class LocalContactService: myProtocol{

Any of you knows why I'm getting this error?

Wyetro
  • 8,439
  • 9
  • 46
  • 64
user2924482
  • 8,380
  • 23
  • 89
  • 173
  • 1
    Have you declared a protocol "myProtocol"? If so, please show us the declaration. If not, I would recommend you to read https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html – K. Biermann Jul 21 '16 at 22:45

6 Answers6

6

i had the same error .in my case i found accidentally that i added my swift files into "copy bundle resource" in "build phase" i removed all swift file except assets then everything worked fine.

enter image description here

Hamed Nova
  • 1,061
  • 9
  • 15
4

the issue is caused when the classes in question do not belong to the same targets, usually the test target is missing. Just make sure the following check boxes are ticked. you can refer to original answer: https://stackoverflow.com/a/30737191/3992606

enter image description here

mohsen
  • 4,698
  • 1
  • 33
  • 54
  • 1
    Thanks, that solved my issue! Every time this is so insanely unexpected. I am hopping between kotlin and swift all the time and I don't expect such errors from not marking the unit tests. Imo very unintuitive. – ecth Jan 23 '20 at 18:41
3

Perhaps you've never defined myProtocol. Or if you have, maybe it's out of scope in the LocalContactService class

Yonah Karp
  • 581
  • 7
  • 22
  • How do I define the protocol? – user2924482 Jul 21 '16 at 22:49
  • Here are some examples: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html A basic definition (without any useful content) would be `protocol myProtocol {}` – K. Biermann Jul 21 '16 at 22:54
  • This was exactly my problem! And I had it on the same file, so it wasn't obvious. Thanks – rr1g0 Dec 01 '16 at 06:02
2

I got this error when someone pushed changes that they made to the project.pbxproj file.

Select the file hit delete and choose the option to Remove Reference

Add the file back to the project, clean (cmd + shift + k), and rebuild

Matthew Harries
  • 630
  • 1
  • 6
  • 9
  • I ran into this issue while working through this SwiftUI tutorial: https://developer.apple.com/tutorials/swiftui/handling-user-input and following the steps you provided fixed it. Thanks! – Subcreation Sep 08 '19 at 04:02
1

Go to your myProtocol class, and on the right side (If you are using XCode), click the Tests of your class. This is what I mean.

"Use of undeclared type" in Swift, even though type is internal, and exists in same module

Community
  • 1
  • 1
impression7vx
  • 1,728
  • 1
  • 20
  • 50
1

I my case I had to clean the build folder, close the project, run pod install, and then the issue disappeared.

Ben
  • 521
  • 1
  • 4
  • 7