Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-11339] Problems when naming classes/protocols using @objc #53740

Closed
swift-ci opened this issue Aug 21, 2019 · 0 comments
Closed

[SR-11339] Problems when naming classes/protocols using @objc #53740

swift-ci opened this issue Aug 21, 2019 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-11339
Radar None
Original Reporter barcoded (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

Xcode 10 and Xcode 11 beta

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 3a6000ace6d82ee762847b7fd7085767

duplicates:

  • SR-4827 Incorrect result resolving type for Obj-C block

Issue Description:

The problem is related when going from Swift->ObjC->Swift.

1. Create the Bar class and prefix the class for Objective C using @objc(...).

//Bar.swift
import Foundation

@objc(MKCBar)
public final class Bar: NSObject {
    var name: String?
}

2. Use forward declaration from the Objective C header file to avoid circular references.

//MKCFoo.h
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@class MKCBar;

@interface MKCFoo : NSObject

@property (nonatomic, strong) MKCBar *bar;

-(instancetype)initWithBar:(MKCBar *)bar;

@end

NS_ASSUME_NONNULL_END
  1. In the implementation use the autogenerated header file to get hold of the actual type.
//MKCFoo.m
#import "MKCFoo.h"
#import "ForwardAndBack-Swift.h"

@implementation MKCFoo

-(instancetype)initWithBar:(MKCBar *)bar {
    self = [super init];
    if (self) {
        self.bar = bar;
    }
    return self;
}

@end

4. Next add the Objective C header file to the bridging header to expose the class to Swift.

//ForwardAndBack-Bridging-Header.h
//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "MKCFoo.h"

5. Finally try to use the property (which is a Swift class exposed using @objc) defined on the Objective C file.

//Baz.swift
import Foundation

public final class Baz {

    func executeFoo(_ foo: MKCFoo) {
        // This line below will not work as long as Bar.swift has the following @objc(MKCBar) attribute.
        // foo.bar
    }
}

6. Last but not least try to remove the name from @objc and update accordingly

        // Now try to replace @objc(MKCBar) in Bar.swift with just @objc and update the forward decleration in MKCfoo.h and correct the type in MKCfoo.m and now the line below will work but I've lost the prefix :-(
        // foo.bar

Please see the attached project or the problem described in this blog post https://cjwirth.com/tech/circular-references-swift-objc

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

1 participant