1

Trying to set a scrollView's contentSize and I've run across this issue (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 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)'

let testing = 1000
scrollView.contentSize = CGSizeMake(self.view.frame.width, testing)

Error: Cannot invoke 'CGSizeMake' with an argument list of type '(CGFloat, Int)'

4

2 回答 2

2

将语句更改let为以下内容:

let testing:CGFloat = 1000

您需要这样做,因为该CGSizeMake函数需要两个相同类型的参数,因此您可以将两者都设为ints 或将它们都设为CGFloats。在这种情况下,首先将testing其用作 a可能更容易CGFloat。在其他情况下,您可能想尝试类似

let testing = 1000
scrollView.contentSize = CGSizeMake(Int(self.view.frame.width), testing)

或者:

let testing = 1000
scrollView.contentSize = CGSizeMake(self.view.frame.width, CGFloat(testing))

所以两者都是同一类型。

于 2015-08-01T04:36:41.200 回答
0
  • 数字文字没有明确的类型。它的类型是在编译器评估它的时候推断出来的。

  • 数字变量必须具有显式类型。默认类型是Int

    let testing : CGFloat = 1000.0
    scrollView.contentSize = CGSize(width:self.view.frame.width, height:testing)
    
于 2015-08-01T04:40:57.780 回答