从objC访问swift属性 [英] Accessing a swift property from objC

查看:156
本文介绍了从objC访问swift属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

- (void) tapGesture: (id)sender
{
    UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
    NSInteger userID = gesture.view.tag;

    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main"
                                                         bundle:nil];
    OthersProfile *vc = (OthersProfile*)[storyboard instantiateViewControllerWithIdentifier:@"othersprofile"];
    NSString *strUserID = [NSString stringWithFormat:@"%ld", (long)userID];

    vc.userID = strUserID;
    [self.viewController.navigationController pushViewController:vc
                                                        animated:YES];
}

它曾经与Xcode 8 / Swift 3一起使用,但似乎有一个Xcode 9和swift 4的问题。

It used to work with Xcode 8 / Swift 3 but seems like there is a problem with Xcode 9 and swift 4.

我找不到属性userID错误。

I am having property userID not found error.

在Swift文件中:

var userID : String = ""
override func viewDidLoad() {
    print(userID) //no value here
}

任何有想法的人?

推荐答案

请改为尝试:

@objc var userID : String = "

将Swift代码暴露给Objective-C的规则已经更改在Swift 4中。因此,您的代码在Swift 3中没问题,因此您面临着混乱;)

The rules for exposing Swift code to Objective-C have changed in Swift 4. As such, your code was okay in Swift 3 and hence the confusion you are facing ;)

新设计在相应的Swift Evolution提案。

这篇关于从objC访问swift属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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