• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Mixing Swift and Objective-C protocols
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Mixing Swift and Objective-C protocols


  • Subject: Mixing Swift and Objective-C protocols
  • From: "Gerriet M. Denkmann" <email@hidden>
  • Date: Wed, 07 Sep 2016 16:43:26 +0700

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 (email@hidden)

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:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Mixing Swift and Objective-C protocols
      • From: Quincey Morris <email@hidden>
  • Prev by Date: Re: Window Opened Notification?
  • Next by Date: Re: Odd File Truncation
  • Previous by thread: Re: Odd File Truncation
  • Next by thread: Re: Mixing Swift and Objective-C protocols
  • Index(es):
    • Date
    • Thread