swiftcgsize

How to fix 'Expression type 'CGSize' is ambiguous without more context' in swift?


This is my code :

@IBOutlet weak var imageScroll: UIScrollView!
...
imageScroll.contentSize = CGSize(width: imageScroll.visibleSize.width * 5, height: imageScroll.visibleSize.height)

It's OK.

If I chang it as below:

let ct = 5

imageScroll.contentSize = CGSize(width: imageScroll.visibleSize.width * ct, height: imageScroll.visibleSize.height)

It prompt an error : Expression type 'CGSize' is ambiguous without more context

When I post my question, stackoverflow prompt me a similar question: How to fix "Expression type '@lvalue CGRect/CGSize' is ambiguous without more context"?

I changed let to var, the error is same.

Any help? Thanks.


Solution

  • The issue is because ct = 5 is inferred as type int.

    imageScroll.visibleSize.height the height property is a CGFloat

    They need to be the same type so if you change your code to this it should work

    let ct: CGFloat = 5