class 내에서의 mutating 키워드 사용 관련 질문

태그: 

1 답변 글타래를 보이고 있습니다
  • 글쓴이
    • c00012
      참가자
      • 글작성 : 16
      • 답글작성 : 5

      안녕하세요.

      swift로 링크드리스트를 구현하려고 하는데 질문이 생겨 글 올립니다.

      class로 링크드리스트를 구현하려고 다음과 같이 코드를 작성했습니다.

      public class LinkedList<Value>{

          public var head: Node<Value>?

             public var tail: Node<Value>?

             

             public init() {}

             

             public var isEmpty:Bool{

                 head == nil

             }

             public mutating func push(value:Value){

                 head = Node(value: value, next: head)

                 if tail == nil{

                     tail = head

                 }

             }

             

             public mutating func append(value:Value){

                 guard !isEmpty else{

                     push(value:value)

                     return

                 }

                 tail!.next = Node(value: value)

                 tail = tail!.next

             }

             

             public func node(at index:Int)->Node<Value>?{

                 var currentNode = head

                 var currentIndex = 0

                 

                 while currentNode != nil && currentIndex<index {

                     currentNode = currentNode!.next

                     currentIndex += 1

                 }

                 return currentNode

             }

             

             @discardableResult

             public mutating func insert(value:Value, after node:Node<Value>)->Node<Value>{

                 guard tail !== node else{

                     append(value: value)

                     return tail!

                 }

                 node.next = Node(value: value,next:node.next)

                 return node.next!

             }

             @discardableResult

             public mutating func insert(value:Value, before node:Node<Value>)->Node<Value>{

                 guard head !== node else{

                     append(value:value)

                     return head!

                 }

                 node.next = Node(value:value, next: node.next)

                 return node.next!

             }

             

             @discardableResult

             public mutating func pop()->Value?{

                 defer{

                     head = head?.next

                     if isEmpty{

                         tail = nil

                     }

                 }

                 return head?.value

             }

             

             @discardableResult

             public mutating func removeLast()->Value?{

                 guard let head = head else{

                     return nil

                 }

                 guard head.next != nil else{

                     return pop()

                 }

                 var prev = head

                 var current = head

                 

                 while let next = current.next{

                     prev = current

                     current = next

                 }

                 

                 prev.next = nil

                 tail = prev

                 return current.value

             }

             @discardableResult

             public mutating func remove(after node: Node<Value>)->Value?{

                 defer{

                     if node.next === tail{

                         tail = node

                     }

                     node.next = node.next?.next

                 }

                 return node.next?.value

             }

      }

      위와 같이 작성을 하고 컴파일을 돌려보니 

      ‘mutating’ is not valid on instance methods in classes 라는 에러 메시지가 뜨는데, 이게 왜 이런 메시지가 뜨는 건지 설명 좀 부탁드립니다.

    • rodxx
      참가자
      • 글작성 : 1
      • 답글작성 : 9

      ‘mutating’ 키워드는 클래스(참조 타입)의 메서드에서는 유효하지 않다는 의미입니다.

      그럼 ‘mutating’ 키워드는 언제 사용하는 것이고, 어떨 때 유효할까요?

      ‘mutating’ 키워드에 대해 찾아보시고 더 나아가서 구조체와 클래스가 메모리에 저장되는 방식의 차이를 살펴보시면 좋을 것 같습니다.

1 답변 글타래를 보이고 있습니다
  • 답변은 로그인 후 가능합니다.

logo landscape small

사업자번호 : 743-81-02195
통신판매업 신고번호 : 제 2022-충북청주-1278 호
고객센터 : 카카오톡채널 @yagom