0

I've got a big problem !

I try to use a Swift property in ObjC++ class but nothing works !

My Swift Class :

let wST:CGFloat = 200

@objc class G: NSObject {
    private override init() {}

    class func sizeST() -> CGSize { return CGSize(width: wST, height: 200)}
    // Another test
    static var hST:CGFloat = 200
}

In ObjC++ class .h :

@class G;

In ObjC++ class .mm :

#import "PROJECT-Swift.h"
...

- (id)initWithFrame:(CGRect)frame {
    NSLog(@"Property : %@", [G sizeST]);

    // With static var
    NSLog(@"Property : %f", G.hST);
}

What's wrong ? Thanks for your help...

kopacabana
  • 437
  • 3
  • 17

2 Answers2

2

"G" in your Objective C code is a class, so what you appear to be trying to do is get a property from a class, not an instance of that class.

You can access a property from an instance (or object) of a class, so G needs to be a parameter in your initWithFrame or it needs to be its own property in your Obj-C object (e.g. self.G.hST).

Another thing you can do is declare your hST property as a class function instead (e.g. look at this related question). That is:

@objc class G: NSObject {

    class func hST() -> CGFloat {
       return 200
    }
}
Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
1

Thanks for your response. It's work ! But I change a little because I want to keep property as static property.

In my class G:

static var _hST:CGFloat = 300.0
static func hST() -> CGFloat { return _hST }

So I can modify the value inside another Swift class.

In my ObjC class, I use :

NSLog(@"hST -> %f", [G hST]);

Thanks a lot !

kopacabana
  • 437
  • 3
  • 17