无法从 Objective-C 视图控制器访问 Swift var - iOS [英] Cannot access Swift var from Objective-C View controller - iOS

查看:50
本文介绍了无法从 Objective-C 视图控制器访问 Swift var - iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 Objective-C 和 Swift 的视图控制器的应用程序.我正在从我的一个基于 Objective-C 的视图控制器中以编程方式打开一个基于 Swift 的视图控制器.我遇到的问题是我无法从我的 Objective-C 代码访问 Swift 变量.

I have an app with both Objective-C and Swift based view controllers. I am programatically opening a Swift based view controller from one of my Objective-C based view controllers. The problem I am having is that I can't access the Swift variables from my Objective-C code.

我的 Swift 代码:

My Swift code:

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

我的 Objective-C 代码:

My Objective-C code:

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

我可以访问像 IBOutlets 这样的 @ 属性,但我无法访问和编辑 var 对象.

I am able to access @ properties like IBOutlets, but I can't access and edit the var objects.

我这样做是因为我想在呈现基于 Swift 的视图控制器时将数据从基于 Objective-C 的视图控制器传递到基于 Swift 的视图控制器中.

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.

为什么我不能访问 var 对象?

Why can't I access the var objects?

这是我得到的错误:

错误如下:

在类型的对象上找不到属性passedUser"'MyProfileViewController *'

Property 'passedUser' not found on object of type 'MyProfileViewController *'

感谢您抽出宝贵时间,丹.

Thanks for your time, Dan.

推荐答案

您必须将变量声明为 public.

You will have to declare the variable as public.

默认情况下,变量具有 internal 访问权限,这意味着它们只能从其模块内部访问.Obj-C 代码不在他们的模块中.

By default, the variables have internal access, that means they can be accessed only from inside their module. Obj-C code is not in their module.

这篇关于无法从 Objective-C 视图控制器访问 Swift var - iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆