Type CCC doesnt conform to protocol 'NSObjectProtocol'
If you follow up the inheritance chain, NSURLSessionDataDelegate
inherits NSURLSessionTaskDelegate
, which inherits NSURLSessionDelegate
, which inherits, NSObjectProtocol
. This protocol has various required methods like isEqual(_:)
and respondsToSelector(_:)
which you class does not implement.
Generally what you would do here is make your class inherit NSObject
which conforms to NSObjectProtocol
:
class Test: NSObject, NSURLSessionDataDelegate {
...
}
Class does not conform NSObjectProtocol
See Why in swift we cannot adopt a protocol without inheritance a class from NSObject?
In short, WCSessionDelegate
itself inherits from NSObjectProtocol
therefore you need to implement methods in that protocol, too. The easiest way to implement those methods is to subclass NSObject
:
class BatteryLevel: NSObject, WCSessionDelegate
Note that you are dealing with Obj-C APIs here.
MFMailComposeViewControllerDelegate not conform to protocol NSObjectProtocol
Where ever you use MFMailComposeViewControllerDelegate
, The class your stating conforms to MFMailComposeViewControllerDelegate
also needs to conform to NSObjectProtocol.
You can just inherit from NSObject to fix this.
class CustomersDetailPresenter: NSObject
class MyViewController: UIViewController, NSObject
Why doesn’t my controller conform to NSObjectProtocol?
See this:
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html
You need to implement the protocol functions in order to conform to that protocol. Without that the protocol conformance is there useless, don't ya think? :) Anyway it's always better to extend the class via protocol in extension, like this:
extension SCViewController: AVCaptureMetadataOutputObjectsDelegate{
//code here
}
Anyway more extensive description: As you can see, the delegates implements as "superProtocol" NSObjectProtocol,
see this:
https://developer.apple.com/documentation/avfoundation/avcapturemetadataoutputobjectsdelegate
So just implement the NSObjectProtocol functions and you will be fine... Happy coding :)
Edit: After realising I am a complete idiot and forgetting the essential things -> this should work, because the UIViewController class is subclass to UIResponder which is subclass to NSObject, therefore there might be problem with the class name or something like that... Would try to force the compiler to do strange things what it would do... like this:
extension SCViewController: NSObject, AVCaptureMetadataOutputObjectsDelegate{
//code here
}
btw this above should definitely work.
Swift : Type XXX must conform to protocol 'NSObjectProtocol'
Self answered for sake of archiving.
When adding
override func isEqual(anObject: AnyObject?) -> Bool {
return super.isEqual(anObject)
}
to my class, it works.
This method should have been inherited from the base class.
Looks like a bug in Swift / Xcode 6.1 to me
Default protocol implementation causes 'does not conform to protocol' error
Solved it! My NavigationDelegate
and its extension were in a different target than the one that MyViewController
belongs to. Simply moving the extension to the same target worked.
Hope this helps someone in the future /p>
How to make a class conform to a protocol in Swift?
Type 'CellDatasDataSource' does not conform to protocol 'NSObjectProtocol'
You have to make your class inherit from NSObject
to conform to the NSObjectProtocol
. Vanilla Swift classes do not. But many parts of UIKit
expect NSObject
s.
class CustomDataSource : NSObject, UITableViewDataSource {
}
But this:
Type 'CellDatasDataSource' does not conform to protocol 'UITableViewDataSource'
Is expected. You will get the error until your class implements all required methods of the protocol.
So get coding :)
Swift class/struct implicitly conforms to NSObjectProtocol?
Apparently, it’s both bug and feature. https://bugs.swift.org/browse/SR-10495
Related Topics
How to Link Xctest Dependency to Production/Main Target
Get Location from Center of Screen Swift Mapkit
How to Add a Watermark to an Image Using This Code
How to Convert Delegate to Observable Rxswift
How to Make a Https Request to a Server in Swift
Converting .M4A File to .Aiff Using Audioconverter Swift
How to Convert a Swift String to Cfstring
Swift Codable Decode Manually Optional Variable
Can a Swift Property Wrapper Reference the Owner of the Property Its Wrapping
Binary Operator '+' Cannot Be Applied to Two 'T' Operands
Rxswift/Rxcocoa: Prevent Uitextfield from Having More Than ... Characters
How to Use Image Literal in Xcode 13
Local Notification While App Not Running
Can Swift Playgrounds See Other Source Files in the Same Project