Skip to content

Commit ab6919c

Browse files
authored
Merge pull request #64 from zintus/retain-cycle
Fix retain cycle
2 parents c880dc1 + c8fc426 commit ab6919c

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

FlexLayoutTests/FlexLayoutTests.swift

+9-20
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,15 @@ import XCTest
1515

1616
class FlexLayoutTests: XCTestCase {
1717

18-
override func setUp() {
19-
super.setUp()
20-
// Put setup code here. This method is called before the invocation of each test method in the class.
21-
}
22-
23-
override func tearDown() {
24-
// Put teardown code here. This method is called after the invocation of each test method in the class.
25-
super.tearDown()
26-
}
27-
28-
func testExample() {
29-
// This is an example of a functional test case.
30-
// Use XCTAssert and related functions to verify your tests produce the correct results.
31-
}
32-
33-
func testPerformanceExample() {
34-
// This is an example of a performance test case.
35-
self.measure {
36-
// Put the code you want to measure the time of here.
37-
}
18+
func testRetainCycle() {
19+
weak var weakView: UIView? = nil
20+
_ = { _ in
21+
let strongView = UIView()
22+
strongView.flex.direction(.column)
23+
weakView = strongView
24+
}()
25+
26+
XCTAssertNil(weakView, "Creation of flex should not lead to retain cycle")
3827
}
3928

4029
}

Sources/FlexLayout.swift

+13-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public final class Flex {
3131
/**
3232
Flex items's UIView.
3333
*/
34-
public let view: UIView
34+
private(set) weak var view: UIView?
3535
private let yoga: YGLayout
3636

3737
/**
@@ -73,8 +73,12 @@ public final class Flex {
7373
*/
7474
@discardableResult
7575
public func addItem(_ view: UIView) -> Flex {
76-
self.view.addSubview(view)
77-
return view.flex
76+
if let host = self.view {
77+
host.addSubview(view)
78+
return view.flex
79+
} else {
80+
preconditionFailure("Trying to modify deallocated host view")
81+
}
7882
}
7983

8084
/**
@@ -954,8 +958,12 @@ public final class Flex {
954958
*/
955959
@discardableResult
956960
public func backgroundColor(_ color: UIColor) -> Flex {
957-
view.backgroundColor = color
958-
return self
961+
if let host = self.view {
962+
host.backgroundColor = color
963+
return self
964+
} else {
965+
preconditionFailure("Trying to modify deallocated host view")
966+
}
959967
}
960968

961969
// MARK: Enums

0 commit comments

Comments
 (0)