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 で表現できない何かを実行しようとすると、コンパイラ エラーが発生するという利点があります。

オブジェクトの型を Objective-C で表すことができないため、プロパティを @objc とマークすることはできません

objc アクセス可能なシンボルがない場合、objc bridger はクラスを最適化します。これはあなたが見たものです。

于 2017-08-02T13:30:03.367 に答える