简体   繁体   English

无法从 Objective-C 视图控制器访问 Swift var - iOS

[英]Cannot access Swift var from Objective-C View controller - iOS

I have an app with both Objective-C and Swift based view controllers.我有一个基于 Objective-C 和 Swift 的视图控制器的应用程序。 I am programatically opening a Swift based view controller from one of my Objective-C based view controllers.我正在从我的一个基于 Objective-C 的视图控制器中以编程方式打开一个基于 Swift 的视图控制器。 The problem I am having is that I can't access the Swift variables from my Objective-C code.我遇到的问题是我无法从我的 Objective-C 代码访问 Swift 变量。

My Swift code:我的 Swift 代码:

@IBOutlet weak var profPicture: UIImageView!
@IBOutlet weak var profVerified: UIImageView!
@IBOutlet weak var profName: UILabel!
var passedUser:PFUser!

My Objective-C code:我的Objective-C代码:

MyProfileViewController *screen = [[MyProfileViewController alloc] initWithNibName:@"Main" bundle:nil];
self.dataPass = screen;
dataPass.profName.text = @"";
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:screen animated:YES completion:nil];

I am able to access @ properties like IBOutlets, but I can't access and edit the var objects.我可以访问像 IBOutlets 这样的 @ 属性,但我无法访问和编辑 var 对象。

I am doing this, because I want to pass data into the Swift based view controller from the Objective-C based view controller when I am presenting the Swift based view controller.我这样做是因为我想在呈现基于 Swift 的视图控制器时将数据从基于 Objective-C 的视图控制器传递到基于 Swift 的视图控制器中。

Why can't I access the var objects?为什么我不能访问 var 对象?

Here is the error I am getting:这是我得到的错误:

在此处输入图片说明

The error is as follows:错误如下:

Property 'passedUser' not found on object of type 'MyProfileViewController *'在“MyProfileViewController *”类型的对象上找不到属性“passedUser”

Thanks for your time, Dan.谢谢你的时间,丹。

Swift 4:斯威夫特 4:

Use @objc keyword to access the swift variable.使用@objc关键字访问 swift 变量。

class Car: NSObject {
    @objc let name: String
    @objc let numberOfWheels: Int
    let manufacturer: String
}

You will have to declare the variable as public .您必须将该变量声明为public

By default, the variables have internal access, that means they can be accessed only from inside their module.默认情况下,变量具有internal访问权限,这意味着只能从其模块内部访问它们。 Obj-C code is not in their module. Obj-C 代码不在他们的模块中。

I had the same issue with an ObjectiveC enum, that was a variable in a Swift class and wasn't accessible in ObjectiveC.我对 ObjectiveC 枚举有同样的问题,它是 Swift 类中的一个变量,在 ObjectiveC 中无法访问。

Declaring the variable as public didn't work, instead I had to give the enum a default value when declaring it, for it to be included in the [project]-Swift.h header.将变量声明为 public 不起作用,相反,我必须在声明它时给枚举一个默认值,以便将它包含在 [project]-Swift.h 标头中。

 var firstQuestion: AnswerResult = AnswerNone

The following didn't work:以下不起作用:

var firstQuestion: AnswerResult!

With these all answers, sometimes compiler doesn't generate appropriate obj-c code for our swift code.有了这些所有答案,有时编译器不会为我们的 swift 代码生成适当的 obj-c 代码。 In that case,在这种情况下,

Just clean & build project.只需清理和构建项目。

您需要对变量使用@objc public以将其公开给 Objective-C:

@IBOutlet @objc public weak var profName: UILabel!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Swift的Objective-c基本视图控制器继承 - Inheritance from an Objective-c base view controller from Swift 如何从iOS Objective-C中的控制器重绘自定义视图 - how to redraw custom view from the controller in ios objective-c iOS - 无法从 Objective-C 访问 Swift Singleton - iOS - Can't access Swift Singleton from Objective-C 无法从Objective-C文件访问Swift类中的变量 - Cannot access variables in a Swift class from Objective-C file 从Swift View Controller将数据传递到Objective-C View Controller - Pass Data To Objective-C View Controller From Swift View Controller ios从第二个视图控制器向第一个视图控制器发送Objective-c数据发送 - ios Delegate Objective-c data sending from 2nd view controller to 1st view controller 有什么方法可以使用Objective-C iOS中所有视图控制器上可以访问的全局静态字典? - Is there any way to use global Static dictionary could able to access on all view controller in objective-c ios? 在Objective-C中调用swift视图控制器文件的变量 - Calling to a variable of swift view controller file in Objective-C iOS / Objective-C:在SLComposer消息后关闭视图控制器 - IOS/Objective-C: Dismiss view controller after SLComposer message iOS MMDrawerController Objective-C登录视图控制器 - iOS MMDrawerController objective-c login view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM