I have several Swift classes CclassA, ClassB), all implementing the BitField 
protocol:

protocol BitField
{
        init?(limit: UInt64, verbose: Int)
}

My AppDelegate Swift class has:

var a: BitField?
switch mm
{
        case .A:        a = ClassA(limit: maxp, verbose: verbose )      
        case .B:        a = ClassB(limit: maxp, verbose: verbose 
        //case .C:      a = ObjC(limit: maxp, verbose: verbose )                
Cannot assign value of type ‘ObjC!’ to type 'BitField?'
}

This works fine.
But now I want to add an Objective-C class.
I created BitFieldProtocol.h with:
@protocol BitField 
- (instancetype)initWithPrimeLimit: (uint64_t)primeLimit verbose: 
(NSInteger)verbose;
@end

and ObjC.h,m with:
@import Foundation;
#import "BitField.h"
@interface ObjC : NSObject <BitField>  
- (instancetype)initWithLimit: (uint64_t)limit verbose: (NSInteger)verbose;
@end

I can create it in AppDelegate:
let ax = ObjC(limit: maxp, verbose: verbose )
print(“ax = \(ax)")

But I cannot put it into my switch statement. The compiler says: Cannot assign 
value of type ‘ObjC!’ to type 'BitField?'
How can I convince the compiler that the ObjC really implements the BitField 
protocol?

This compiles:
case .C:        a = ObjC(limit: maxp, verbose: verbose ) as! BitField?

but crashes at run-time:
Could not cast value of type 'ObjC' (0x10bb0c298) to 'Primes.BitField’ 
(0x10bb10120).

Gerriet.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to