我已经看了很多关于这个话题的帖子,但我仍然看不出我做错了什么。我在objective-c中有一个视图控制器,它试图访问Swift中的NSObject代码片段。我有Debugs Module YES,objective-c桥头和Swift头都在Build Settings中的正确位置。以下是代码的相关部分:
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.
}
@endSwift代码:
//
// 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)
}
}在ViewController.m中,开始输入MyClass会收到来自编译器的以下警告:
未声明的标识符'MyClass‘的/Users/nelson/swiftTest/swiftTest/ViewController.m:20:5:用法。
请注意,我将Swift类声明为@objc和public。我从Apple的文档中得到的理解是,这应该使任何变量和函数对同一目标中的objective-c代码可见。项目模块名称为‘swiftTest’。
有谁能解释一下这件事吗?我已经试着让它工作了差不多一整天了。谢谢!
发布于 2017-08-02 21:30:03
try @objc public var property:String = "“
@objc public有一个编译器错误的好处,如果你尝试做一些无法用objc表示的东西,你会得到
属性不能标记为@objc,因为它的类型不能用Objective-C表示
如果你没有objc可访问的符号,那么objc bridger会把你的类去掉。这就是你所看到的。
https://stackoverflow.com/questions/42331188
复制相似问题