我有一个Objective-C UIView类:
ChoosePersonView.h
@class Person; @interface ChoosePersonView : MDCSwipetochooseView @property (nonatomic,strong,readonly) Person *person; - (instancetype)initWithFrame:(CGRect)frame person:(Person *)person options:(MDCSwipetochooseViewOptions *)options; @end
MDCSwipetochooseView.m
@class MDCSwipetochooseViewOptions; @interface MDCSwipetochooseView : UIView @property (nonatomic,strong) UIImageView *imageView; @property (nonatomic,strong) UIView *likedView; @property (nonatomic,strong) UIView *nopeView; - (instancetype)initWithFrame:(CGRect)frame options:(MDCSwipetochooseViewOptions *)options; @end
通常我会子类化并使用它如下:
ChoosePersonView.m
@implementation ChoosePersonView #pragma mark - Object Lifecycle - (instancetype)initWithFrame:(CGRect)frame person:(Person *)person options:(MDCSwipetochooseViewOptions *)options { self = [super initWithFrame:frame options:options]; if (self) { _person = person; self.imageView.image = _person.image; self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin; self.imageView.autoresizingMask = self.autoresizingMask; [self construct@R_301_4045@ionView]; } return self; } ... @end
我面临的问题是当我尝试在Swift中执行此操作时,属性self.imageView始终为nil.
我不确定如何重新实现initWithFrame,因为它在Swift中的Objective-C中实现.我认为使用init(frame:CGRect,person:Person,options:MDCSwipetochooseViewOptions)是正确的方法,但我想我可能是错的.
ChoosePersonView.swift
class ChoosePersonView: MDCSwipetochooseView { var @R_301_4045@ionView: UIView! var nameLabel: UILabel! var cameraimageLabelView: ImageLabelView! var interestsImageLabelView: ImageLabelView! var friendsImageLabelView: ImageLabelView! var person: Person! let ChoosePersonViewImageLabelWidth = CGFloat(42.0) // #pragma mark - Object Lifecycle init(frame: CGRect,person: Person,options: MDCSwipetochooseViewOptions) { super.init(frame: frame) self.person = person self.imageView.image = self.person.image // self.imageView is nil. self.autoresizingMask = .FlexibleHeight | .FlexibleWidth | .FlexibleBottomMargin self.imageView.autoresizingMask = self.autoresizingMask self.construct@R_301_4045@ionView() } .... }
解决方法
self.imageView是零,因为你没有做任何事情使它不是零.你没有显示任何会使它不是零的代码,所以你很难想象你应该怎么想.
我的猜测是,它是MDCSwipetochooseView的初始化程序的工作,您可以将其称为super.init(frame:options :),以创建imageView.很难说,因为你没有显示代码.当然,这是在Objective-C中编写ChoosePersonView时调用的初始化程序.那么为什么你现在也没有调用它,现在它是用Swift编写的?出于某种原因,您选择不调用该初始化程序 – 您正在调用super.init(frame :).这似乎是一件非常奇怪的事情.也许这就是你出错的地方.
但无论如何,我可以向你保证,如果你不调用一些导致imageView不为nil的代码,它将是nil,因为这是Objective-C中所有对象属性的默认值.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。