“Int不能转换为CGFloat”。子类化UIImageView时出错 [英] "Int is not convertible to CGFloat" error when subclassing 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 subclass:
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")
}
}
创建框架时出现Le错误。
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.
这篇关于“Int不能转换为CGFloat”。子类化UIImageView时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!