Я пытаюсь преобразовать код цели c в быстрый. Я конвертирую многие из них, но у меня проблема с частью кода init.Super init in Swift
Как исправить мою ошибку 3. Легко исправить их удаление, если утверждение. Но для этой проблемы я не мог найти реального решения. Удаление if statement Это может вызвать проблему?
Мои коды:
My objective c code:
-(id)init
{
self = [super init];
if (self) // In swift I can delete this statement to solve my problem. Is it causing the problem?
{
[self commonInit];
}
return self;
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) //Also I can delete this if to solve the error but I am not sure is it the best way
{
[self commonInit];
}
return self;
}
-(void)commonInit
{
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor clearColor];
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.accessoryType = UITableViewCellAccessoryNone;
_textView = [[UITextView alloc] init];
_timeLabel = [[UILabel alloc] init];
_statusIcon = [[UIImageView alloc] init];
[self.contentView addSubview:_timeLabel];
[self.contentView addSubview:_statusIcon];
}
И мой скор код:
init() {
super.init() //Gives Error: Must call a designated initializer of the superclass 'UITableViewCell'
if self != nil { //Value of type 'MessageCell' can never be nil, comparison isn't allowed
self.commonInit()
}
}
//Added this init because Xcode gives error if not add
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
fatalError("init(coder:) has not been implemented")
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?){
super.init(style: style, reuseIdentifier: reuseIdentifier)
if self != nil { //Error: Value of type 'MessageCell' can never be nil, comparison isn't allowed
self.commonInit()
}
}
func commonInit() {
//set values
self.backgroundColor = UIColor.clearColor()
self.contentView.backgroundColor = UIColor.clearColor()
self.selectionStyle = .None
self.accessoryType = .None
self.timeLabel = UILabel()
self.statusIcon = UIImageView()
self.contentView.addSubview(timeLabel)
self.contentView.addSubview(statusIcon)
}
Какова ваша интерпретация сообщений? – Wain
@Wain Добавлены ошибки внутри кода. он говорит, что должен вызвать назначенный инициализатор суперкласса «UITableViewCell» в части init. И другая ошибка ниже этой ошибки – Arda
Я видел это, я спрашиваю, что вы думаете, что они означают – Wain