0

I want add a Swift class in my Objective-C project.

I using bridging header to access Swift class.

But I can not find some property in Swift class.

Sample Swift Code:

@objc class Swift: NSObject {
    var name: String!
    var index: Int!

    required init(name: String, index: Int)
    {
        self.name = name;
        self.index = index;
    }
}

Code in Bridging Header:

@interface Swift : NSObject
@property (nonatomic, copy) NSString * name;

- (instancetype)initWithName:(NSString *)name index:(NSInteger)index OBJC_DESIGNATED_INITIALIZER;
@end

That property "index" is missing.

How can i change code to access the missing property?

Darktt
  • 184
  • 2
  • 7
  • 2
    The answer at that Q has a fix for this problem - optional value types (`Int!`) don't bridge into Objective-C. – Nate Cook Oct 29 '14 at 03:33
  • As Nate pointed out, the problem is the optional `Int!`. Either redefine this "value type" to be non-optional (e.g. `Int`; given that the `index` parameter of `init` is non-optional, that suggests you might be ok with non-optional property) or change it to be a "reference type" (e.g. `NSNumber`) for which optionals are fine. – Rob Oct 29 '14 at 05:04
  • And by the way, there is no reason why (in the excerpt you've given) `name` and `index` would be implicitly unwrapped optionals. You're setting their value in `init`, so they can be plain-old `String` and `Int` without the `!` annotation. – radex Oct 29 '14 at 19:54
  • I know how to change it, Thanks guys. – Darktt Oct 30 '14 at 02:43

0 Answers0