Debugger error: refcount only available for class types

I want to check reference count for an object, using language swift refcount command in debugger.

Sample code:

import Foundation

class Person {
    let name: String
    init(name: String) { self.name = name }
    var apartment: Apartment?
    deinit { print("\(name) is being deinitialized") }
}

class Apartment {
    let unit: String
    init(unit: String) { self.unit = unit }
    weak var tenant: Person?
    deinit { print("Apartment \(unit) is being deinitialized") }
}

var john: Person?
var unit4A: Apartment?

john = Person(name: "John Appleseed")
unit4A = Apartment(unit: "4A")

john!.apartment = unit4A
unit4A!.tenant = john

john = nil // <-- set breakpoint here
unit4A = nil

But, when I put breakpoint into john = nil line, paste language swift refcount john command into lldb command line and hit Enter, an error is occured:

error: refcount only available for class types

Why? Is it broken in latest Xcode Version 14.0.1 (14A400) or am I doing something wrong?

Answered by DTS Engineer in 734341022

You've declared john to be of type Person?, which is not a class type even though Person is a class type.

Right. Which means the LLDB command you need is:

(lldb) language swift refcount john!
refcount data: (strong = 4, unowned = 1, weak = 1)

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

You've declared john to be of type Person?, which is not a class type even though Person is a class type. In Swift, optionals are implemented as a built-in enum type (with 2 cases: .none and .some(value)).

Accepted Answer

You've declared john to be of type Person?, which is not a class type even though Person is a class type.

Right. Which means the LLDB command you need is:

(lldb) language swift refcount john!
refcount data: (strong = 4, unowned = 1, weak = 1)

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Debugger error: refcount only available for class types
 
 
Q