Accessing Swift protocol property implemented in an Obj-C class from another Swift class

Accessing Swift protocol property implemented in an Obj-C class from another Swift class

I've seen lots of questions regarding implementing Obj-C protocols in Swift, but not so much the other way around, and I haven't seen this specifically.

I've seen lots of questions regarding implementing Obj-C protocols in Swift, but not so much the other way around, and I haven't seen this specifically.

I am using a mixed Obj-C / Swift codebase. I have a Swift protocol defined as follows:

NamedItem.swift

@objc protocol NamedItem {
    var name: String { get }
}

I have an existing Objective-C class that currently has its own name property:

MyObjcClass.h

@interface MyObjcClass : NSObject
@property (nonatomic, strong, readonly) NSString* name;
@end

I have a couple other classes that have a name property, so obviously I'd like to associate them all with a protocol instead of typecasting to a bunch of different types. Now, if I try to switch my Obj-C class from having its own property to implementing the Swift protocol:

MyObjcClass.h

@protocol MyObjcProtocol
@property (nonatomic, strong, readonly) NSString* place;
@end

@interface MyObjcClass : NSObject @end

MyObjcClass.m

@interface MyObjcClass () <NamedItem>
@end

@implementation MyObjcClass @synthesize name = _name; @synthesize place = _place; @end

This works great, in my other Objective-C classes, but if I try to access the name property from a Swift class:

SomeSwiftClass.swift

let myObj = MyObjcClass()
myObj.name // error
myObj.place // no problem

I get the following error:

Value of type 'MyObjcClass' has no member 'name'

If I don't remove the existing @property declaration from MyObjcClass.h and omit the @synthesizestatement everything builds correctly. Which seems weird and wrong - If you adopt a Objc-C protocol from an Obj-C class you don't have to re-define the property, just the @synthesize statement is sufficient.

I've tried defining the Swift protocol about every way I could think of and have been able to find suggestions about, but I haven't been able to get around this.

So, what is the correct way to implement a Swift protocol property (maybe specifically a read-only property?) in an Objective-C class such that another Swift class can access it? Do I really have to re-declare the property in the Obj-C header? I know I could always give it up and just define the protocol in Objective-C, but... Swift is the future! (or something like that...)

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

How to use Objective C Cocoapods in Swift Framework

My swift framework has dependency of Objective C Framework. How do I use that ?

A guide to Object Detection with OpenCV and Swift

In this article, we’ll see how to create and launch a object detection algorithm using OpenCV and Swift

Passing a Swift protocol to an Objective-C pointer

<em>Using XCode 10.1 / Swift 4.2.</em>