[英]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.