[英]Swift ScrollView - contentSize can't take a variable Int
Trying to set a scrollView's contentSize and I've run across this issue (Xcode 6.4)... 试图设置一个scrollView的contentSize,我遇到了这个问题(Xcode 6.4)...
Both of these work perfectly: 两者都完美地工作:
scrollView.contentSize = CGSize(width:self.view.frame.width, height:1000)
scrollView.contentSize = CGSizeMake(self.view.frame.width, 1000)
Once a let (or var) gets involved, these do not work: 一旦让let(或var)介入,这些将不起作用:
let testing = 1000
scrollView.contentSize = CGSize(width:self.view.frame.width, height:testing)
Error: Cannot find an initializer for type 'CGSize' that accepts an argument list of type '(width: CGFloat, height: Int)' 错误:找不到类型为“ CGSize”的初始化程序,该初始化程序接受类型为“(width:CGFloat,height:Int)”的参数列表
let testing = 1000
scrollView.contentSize = CGSizeMake(self.view.frame.width, testing)
Error: Cannot invoke 'CGSizeMake' with an argument list of type '(CGFloat, Int)' 错误:无法使用类型为“(CGFloat,Int)”的参数列表调用“ CGSizeMake”
Change the let
statement to the following: 将
let
语句更改为以下内容:
let testing:CGFloat = 1000
You need to do this as the CGSizeMake
function requires two parameters of the same type, so you can either make both int
s or make them both CGFloat
s. 您需要执行此操作,因为
CGSizeMake
函数需要两个相同类型的参数,因此您既可以将它们都设置为int
,也可以将它们都设置为CGFloat
。 In this case it is probably easier to use testing
as a CGFloat
in the first place. 在这种情况下,首先将
testing
用作CGFloat
可能会更容易。 In other cases, you might want to try something like 在其他情况下,您可能想尝试类似
let testing = 1000
scrollView.contentSize = CGSizeMake(Int(self.view.frame.width), testing)
Or: 要么:
let testing = 1000
scrollView.contentSize = CGSizeMake(self.view.frame.width, CGFloat(testing))
So that both are of the same type. 因此,两者属于同一类型。
A number literal doesn't have an explicit type. 数字文字没有显式类型。 Its type is inferred at the point that it's evaluated by the compiler.
它的类型是在编译器评估时推断出来的。
A number variable must have an explicit type. 数字变量必须具有显式类型。 The default type is
Int
默认类型为
Int
let testing : CGFloat = 1000.0 scrollView.contentSize = CGSize(width:self.view.frame.width, height:testing)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.