7

I'm trying to import a Swift class into an Objective-C header file. I know that Project-Swift.h bridge can only be imported into implementation files (.m) but I've got an Objective-C header that needs to declare a property that's defined in Swift.

I read somewhere that this can be done by forward declaring the class in the header file and importing Project-Swift.h into the implementation file. When I do this, the errors are resolved, but I can't access any class properties.

Example:

// Square.swift

@objc class Square: NSObject {
  var width: Int?
  var height: Int?
}

// Shapes.h

@class Square;

@interface Shapes: NSObject {
  @property(nonatomic, strong) Square *square;
}

// Shapes.m

#import "Shapes.h"
#import "Product-Swift.h"

@implementation Shape

@end

// Somewhere else in the code

Shapes *shapes = [Shapes new];
NSLog(@"%@", @(shapes.square.width)); <-- Property 'width' not found on object of type 'Square *'

Anyone can offer an advice on how to access the Swift class and its properties ?

jscs
  • 63,694
  • 13
  • 151
  • 195
Shlomi Nissan
  • 113
  • 1
  • 4

0 Answers0