1

I have looked at a lot of posts on this topic, and I still can't see what I am doing wrong. I have a view controller in objective-c that is trying to access an NSObject code snippet in Swift. I have Debugs Module YES and both the objective-c Bridging Header and the Swift header are at the correct places in Build Settings. Here are the relevant parts of the code:

View Controller.m

    //
//  ViewController.m
//  swiftTest
//
//  Created by Nelson Capes on 2/19/17.
//  Copyright © 2017 Nelson Capes. All rights reserved.
//

#import "ViewController.h"
#import "swiftTest-Swift.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    MyClass
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

Swift code:

    //
//  MyClass.swift
//  swiftTest
//
//  Created by Nelson Capes on 2/19/17.
//  Copyright © 2017 Nelson Capes. All rights reserved.
//

import Foundation
@objc public class MyClass:NSObject{
    var property:String = ""
    func method() {
        print(self.property)
    }
}

In ViewController.m, starting to type MyClass receives the following warning from the compiler:

/Users/nelson/swiftTest/swiftTest/ViewController.m:20:5: Use of undeclared identifier 'MyClass'.

Note that I declare the Swift class as @objc and public. My understanding from Apple's documentation is that this should make any vars and functions visible to objective-c code in the same target. Project module name is 'swiftTest.'

Can anyone shed some light on this? I've been trying to get it to work for almost an entire day. Thanks!

4

1 回答 1

0

尝试@objc public var property:String = ""

@objc public 如果您尝试做一些无法在 objc 中表示的事情,那么您会得到编译器错误的好处

属性不能被标记为@objc,因为它的类型不能在 Objective-C 中表示

如果您没有 objc 可访问的符号,那么 objc 桥接器会将您的课程排除在外。这就是你所看到的。

于 2017-08-02T13:30:03.367 回答