< Int不能转换为CGFloat& quot;继承UIImageView时出错 [英] "Int is not convertible to CGFloat" error when subclassing UIImageView

查看:88
本文介绍了< Int不能转换为CGFloat& quot;继承UIImageView时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一个奇怪的错误.

I have a weird bug here.

我将UIImageView子类化,以将一些自定义属性传递给我的视图. 如果我不向自己的子类中添加init方法,那么一切都很好.

I'm subclassing UIImageView to pass some custom properties to my views. If I don't add an init method to my subclass, everything's fine.

但是如果我确实添加了init(并且必须这样做),那么在创建框架视图时,编译器会抱怨Int无法转换为CGFloat.

But if I do add the init (and I'm required to do so), then when I'm creating the frame view the compiler complains that an Int is not convertible to CGFloat.

具有Le init的Le子类:

Le subclass with Le init :

class CustomUIImageView : UIImageView {


let someParameter : Bool

init(someParameter: Bool) {
    self.someParameter = someParameter
    println("\(someParameter) is being initialized")
}

//Since the release of the GM version of XCode 6, it's asking me to pass this init below 
//if I'm initializing a value. Frankly, I've no idea why, what's for nor what should I do about it. 
required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

deinit {
    println("\(someParameter) is being deinitialized")
}
}

创建框架时出现错误.

And Le error when creating the Frame.

 var myView: CustomUIImageView!
 myView = CustomUIImageView(frame: (CGRectMake(0, 0, 100, 100)))
 //compiler says "Int is not convertible to CGFloat". 

这是某种错误,还是我做错了什么(这很有可能)?

Is this some kind of bug, or did I do something wrong (which is highly plausible)?

我读到我需要通过执行以下操作直接转换为CGFloat: myView = CustomUIImageView(frame: (CGRectMake(CGFloat(0), 0, 100, 100))) 但是随后编译器再次抱怨说"Type()不符合协议'IntergerLiteralConvertible'".

Edit : I've read that I need to convert directly to a CGFloat by doing like this : myView = CustomUIImageView(frame: (CGRectMake(CGFloat(0), 0, 100, 100))) But then compiler complains again saying that "Type () does not conform to protocol 'IntergerLiteralConvertible'".

推荐答案

在您的初始化过程中,您忘记了调用父初始化器:

In your init you forgot to call the parent initializer:

init(someParameter: Bool) {
    self.someParameter = someParameter
    println("\(someParameter) is being initialized")

    super.init() // << this is missing
}

但是看起来好像有另一个初始化器是必需的,但没有明确说明:

However it looks like there's another initializer which is required but not said explicitly:

override init(frame: CGRect) {
    self.someParameter = false
    super.init(frame: frame)
}

那应该可以解决编译问题-剩下的就是弄清楚如何初始化您的类中实现的属性-也许您应该使用可选参数.

That should solve the compilation issue - what's left is figuring out how to initialize the properties implemented in your class - maybe you should use optionals.

这篇关于&lt; Int不能转换为CGFloat&amp; quot;继承UIImageView时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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