Matt
Matt

Reputation: 793

Cannot assign this CGFloat to a property in XCode using Swift

I'm trying to set the thickness of a divider of a NSSplitView to a specified CGFloat value but I get the error: "Cannot assign to the result of this expression error in Swift".

I do it this way and it seems like correct, what do you think is wrong with that?

i.stack.imgur.com/2ylKS.png

splitView.dividerThickness is a CGFloat value as you can see in picture 2.

enter image description here

Upvotes: 0

Views: 1005

Answers (2)

Martin R
Martin R

Reputation: 539765

The property is read-only:

 var dividerThickness: CGFloat { get }

so you cannot assign a new value. The documentation states:

You can subclass NSSplitView and override this method to change the thickness of a split view’s dividers.

Upvotes: 2

lchamp
lchamp

Reputation: 6612

The error seems to be clear enough to fix it : dividerThickness requires a Float and not a CGFloat.

In order to fix it, you can :

  • Declare your value like so : var val : Float = 0.0
  • Keep your value as a CGFloat, and then use the Float() initializer : spitView.dividerThickness = Float(val)

That's because CGFloat has different size on 32bit and 64bit machines.

Unfortunately, the cast isn't done automatically. But you might be able to find (or write your own) operator overloading.

Upvotes: 0

Related Questions