Skip to content

Commit 9a70468

Browse files
Tyler FoxTyler Fox
authored andcommitted
Resolve issue with constants in Swift
By making the (inferred type) Double constants an explicit CGFloat type, they can be used as expected.
1 parent 7bbdfd6 commit 9a70468

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

TableViewCellWithAutoLayout/TableViewController/TableViewCell.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import UIKit
99

1010
class TableViewCell: UITableViewCell
1111
{
12-
let kLabelHorizontalInsets = 15.0
13-
let kLabelVerticalInsets = 10.0
12+
// The CGFloat type annotation is necessary for these constants because they are passed as arguments to bridged Objective-C methods,
13+
// and without making the type explicit these will be inferred to be type Double which is not compatible.
14+
let kLabelHorizontalInsets: CGFloat = 15.0
15+
let kLabelVerticalInsets: CGFloat = 10.0
1416

1517
var didSetupConstraints = false
1618

@@ -64,19 +66,16 @@ class TableViewCell: UITableViewCell
6466
self.bodyLabel.autoSetContentCompressionResistancePriorityForAxis(.Vertical)
6567
}
6668

67-
// FIXME 7-Jun-14 Xcode 6b1: The Double literals below should refer to the defined constants kLabelHorizontalInsets and kLabelVerticalInsets.
68-
// However, there are currently intermittent compiler failures when the literals are replaced with these constants. For example, 'Could not find member .Top'
69-
70-
titleLabel.autoPinEdgeToSuperviewEdge(.Top, withInset: 10.0)
71-
titleLabel.autoPinEdgeToSuperviewEdge(.Leading, withInset: 15.0)
72-
titleLabel.autoPinEdgeToSuperviewEdge(.Trailing, withInset: 15.0)
69+
titleLabel.autoPinEdgeToSuperviewEdge(.Top, withInset: kLabelVerticalInsets)
70+
titleLabel.autoPinEdgeToSuperviewEdge(.Leading, withInset: kLabelHorizontalInsets)
71+
titleLabel.autoPinEdgeToSuperviewEdge(.Trailing, withInset: kLabelHorizontalInsets)
7372

7473
// This constraint is an inequality so that if the cell is slightly taller than actually required, extra space will go here
7574
bodyLabel.autoPinEdge(.Top, toEdge: .Bottom, ofView: titleLabel, withOffset: 10.0, relation: .GreaterThanOrEqual)
7675

77-
bodyLabel.autoPinEdgeToSuperviewEdge(.Leading, withInset: 15.0)
78-
bodyLabel.autoPinEdgeToSuperviewEdge(.Trailing, withInset: 15.0)
79-
bodyLabel.autoPinEdgeToSuperviewEdge(.Bottom, withInset: 10.0)
76+
bodyLabel.autoPinEdgeToSuperviewEdge(.Leading, withInset: kLabelHorizontalInsets)
77+
bodyLabel.autoPinEdgeToSuperviewEdge(.Trailing, withInset: kLabelHorizontalInsets)
78+
bodyLabel.autoPinEdgeToSuperviewEdge(.Bottom, withInset: kLabelVerticalInsets)
8079

8180
didSetupConstraints = true
8281
}

0 commit comments

Comments
 (0)