티스토리 뷰

Auto Reference Counting - Part 6

( a.k.a ARC )

 

 

 

Swift 5.0 - The Swift Programming Language (Apple Inc.)  번역 

 

 

 

ARC 1편 - ARC 의 개요와 작동 원리 (https://atelier-chez-moi.tistory.com/37)

ARC 2편 - 강한 순환 참조 (https://atelier-chez-moi.tistory.com/40)

ARC 3편 - 강한 순환 참조 해결하기(1) : 약한 참조 (https://atelier-chez-moi.tistory.com/41)

ARC 4편 - 강한 순환 참조 해결하기(2) : 미소유 참조 (https://atelier-chez-moi.tistory.com/48)

ARC 5편 - 미소유 참조와 암시적 언래핑된 옵셔널 프로퍼티 (https://atelier-chez-moi.tistory.com/50)

ARC 6편 - 클로저에서의 강한 순환 참조 (현재 글)

 

Strong Reference Cycles for Closures

You saw above how a strong reference cycle can be created when two class instance properties hold a strong reference to each other. You also saw how to use weak and unowned references to break these strong reference cycles.

 

위에서 두 클래스 인스턴스 프로퍼티가 서로를 강한 참조하고 있을 때, 어떻게 강한 순환 참조가 만들어질 수 있는지 보았다. 또한 약한 참조와 미소유 참조를 이 강한 순환 참조를 부수기 위해서 어떻게 사용하는지 또한 보았다.

 

A strong reference cycle can also occur if you assign a closure to a property of a class instance, and the body of that closure captures the instance. This capture might occur because the closure’s body accesses a property of the instance, such as self.someProperty, or because the closure calls a method on the instance, such as self.someMethod(). In either case, these accesses cause the closure to “capture” self, creating a strong reference cycle.

 

강한 순환 참조는 또한 발생하는데, 만약 우리가 클래스 인스턴스의 프로퍼티에 클로저를 할당할 경우, 그 클로저의 바디가 인스턴스를 붙잡고 있는 경우가 그렇다. 이러한 캡쳐(capture)는 발생하게 될 수 있는데 클로저의 바디가 인스턴스의 프로퍼티(self.someProperty)에 접근하기 때문이거나, 클로저가 인스턴스의 메소드(self.someMethod())를 호출하기 때문이다. 어떠한 경우던, 이러한 접근은 클로저가 강한 순환 참조를 만들어내는 self 를 "캡쳐하도록(to "capture") 만들어 낼 수 있다. (+ 이 부분 뭔가 깔끔하지 않네요..)

 

This strong reference cycle occurs because closures, like classes, are reference types. When you assign a closure to a property, you are assigning a reference to that closure. In essence, it’s the same problem as above—two strong references are keeping each other alive. However, rather than two class instances, this time it’s a class instance and a closure that are keeping each other alive.

 

강한 순환 참조가 발생하는 이유는 클래스 처럼 클로저들은 참조 타입 (reference types이기 때문이죠. 우리가 클로저를 프로퍼티에 할당할 때면, 우리는 그 클로저에 참조 를 할당하고 있는 것이죠. 본질적으로, 이것은 위에서 본 것과 같은 문제입니다. 두 개의 강한 참조가 서로가 살아있는 상태를 유지하는 것이었어요. 그러나, 두 개의 클래스 인스턴스라기보다 이번에는 클래스 인스턴스와 클로저가 서로를 살아있도록 유지합니다. 

 

Swift provides an elegant solution to this problem, known as a closure capture list. However, before you learn how to break a strong reference cycle with a closure capture list, it’s useful to understand how such a cycle can be caused.

 

Swift 는 이 문제에 대해 엘레강스한 솔루션을 제공합니다. 클로저 캡쳐 리스트(closure capture list) 라 알려진 것이죠. 하지만 클로저 캡처 리스트로 강한 순환 참조를 부수는 방법을 학습하기 전에, 어떻게 그러한 순환이 유발될 수 있는 지를 이해하는 것은 유용할 것입니다.  

 

The example below shows how you can create a strong reference cycle when using a closure that references self. This example defines a class called HTMLElement, which provides a simple model for an individual element within an HTML document:

 

아래의 예제는 클로저를 사용하여 self 를 참조할 때 우리가 어떻게 강한 순환 참조를 만들어 낼 수 있는지를 보여줍니다. 이 예시는 HTMLElement 라는 클래스를 정의하고 있으며, HTML 문서 내 개별 요소에 대한 간단한 모델을 제공하고 있어요 : 

 

The HTMLElement class defines a name property, which indicates the name of the element, such as "h1" for a heading element, "p" for a paragraph element, or "br" for a line break element. HTMLElement also defines an optional text property, which you can set to a string that represents the text to be rendered within that HTML element.

 

HTMLElement 클래스는 name 프로퍼티를 정의하고 있어요. "h1", "p", "br" (+ HTML을 잘몰라서..) 과 같은 요소의 이름을 나타내고 있어요. HTMLElement 는 또한 옵셔널인 text 프로퍼티를 정의하고 있는데, 해당 HTML 요소 내에서 렌더링 되는 텍스트를 나타내는 문자열로 설정할 수 있습니다.

 

In addition to these two simple properties, the HTMLElement class defines a lazy property called asHTML. This property references a closure that combines name and text into an HTML string fragment. The asHTML property is of type () -> String, or “a function that takes no parameters, and returns a String value”.

 

이 두개의 단순한 프로퍼티에 더하여, HTMLElement 클래스는 asHTML 이라는 lazy(+ 호출되는 순간에 초기화) 프로퍼티를 정의합니다. 이 프로퍼티는 name 과 text  를 HTML 문자열 조각으로 결합하는 클로저를 참조하 고 있어요. asHTML 프로퍼티는 () -> String 타입이거나 "파라미터를 가지지 않으며 문자열 값을 리턴하는 함수" 입니다. 

 

By default, the asHTML property is assigned a closure that returns a string representation of an HTML tag. This tag contains the optional text value if it exists, or no text content if text does not exist. For a paragraph element, the closure would return "<p>some text</p>" or "<p />", depending on whether the text property equals "some text" or nil.

 

기본적으로 asHTML 프로퍼티는 HTML 태그의 문자열 표현을 리턴하는 클로저로 할당됩니다. 이 태그는 텍스트가 있는 경우 옵셔널 타입의 텍스트 값을 포함하며, 혹은 텍스트가 없으면 텍스트의 컨텐츠는 없습니다. 단락 요소의 경우, 클로저는 "<p>some text</p>" 나 "<p />" 둘 중 하나를 반환하는데, 이는 text 프로퍼티가 "some text" 또는 nil 인지 여부에 따라 결정됩니다.

 

The asHTML property is named and used somewhat like an instance method. However, because asHTML is a closure property rather than an instance method, you can replace the default value of the asHTML property with a custom closure, if you want to change the HTML rendering for a particular HTML element.

 

asHTML 프로퍼티는 이름이 지정되고 다소간 인스턴스 메소드처럼 사용됩니다. 하지만 asHTML 이 인스턴스 메소드라기 보다는 클로저 프로퍼티이기 때문에, 우리는 asHTML 프로퍼티의 디폴트 값을 커스텀 클로저로 대체할 수 있어요. 만약 우리가 특정 HTML 요소에 대해 HTML 렌더링을 변경하기를 원한다면요. 

 

For example, the asHTML property could be set to a closure that defaults to some text if the text property is nil, in order to prevent the representation from returning an empty HTML tag:

 

예를 들면 asHTML 프로퍼티는 만약 text 프로퍼티가 nil 이라면, 표현식(representation)이 빈 HTML 태그를 리턴하는 것을 예방하기 위해서 일부 텍스트를 기본값으로 가지는 클로저를 설정할 수 있습니다.

 

 

 

.. HTML 때문에 뭔가 너무 빡세서 나머지는 나중에 좀 이어 가겠습니다...

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함