0
        let name = file.name
        withUnsafePointer(to: &file) {
            print ("GETDEPOSIT: \(name) \(file.nameSuffix)   file has address: \($0)")
        }

I really, really, really hate swift, its runtime and its "integration" with xcode

I would really appreciate if anyone can tell me where there is modification and why this new piece of shit technology requires such a weird dance just to print a variable address. I already spent an hour trying to figure this out.

class BVFile { ... }
class OperationCell: UICollectionViewCell {
    var file: BVFile!
}

UPD Thanks Martin R and Sweeper. Sweeper's answer fixes the crash indeed but the addresses do differ:

GETDEPOSIT: Сберегательный вкл@д 166,63 BYN Optional(" 166,63 BYN")   file has address: 0x00007fd8fc555578 0x0000600003194840
GETDEPOSIT: Зручны анл@йн 0 BYN Optional(" 0 BYN")   file has address: 0x00007fd8fc55e138 0x00006000031ea040

that was produced by this

        let address = Unmanaged.passUnretained(file).toOpaque()
        withUnsafePointer(to: &file) {
            print ("GETDEPOSIT: \(name) \($0.pointee?.nameSuffix)   file has address: \($0) \(address)")
        }
Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66
  • file is a force unwrapped var in OperationCell. amended the question – Anton Tropashko Jul 02 '20 at 08:05
  • got address like this let address = Unmanaged.passUnretained(file).toOpaque() but still am curious why old swift way failed – Anton Tropashko Jul 02 '20 at 08:06
  • What happens if you remove the address-of operator? `withUnsafePointer(to: file) {...}` – Martin R Jul 02 '20 at 08:10
  • @MartinR That makes it a different address, doesn't it? – Sweeper Jul 02 '20 at 08:12
  • @MartinR [With and without the `&` does seem to produce different addresses.](https://repl.it/repls/CarefulDotingDemoware#main.swift) Don't know which one the OP needs... – Sweeper Jul 02 '20 at 08:24
  • 1
    @Sweeper: You are right. And none of them is equal to the value of `Unmanaged.passUnretained(file).toOpaque()` – Martin R Jul 02 '20 at 08:28
  • Indeed, the addresses mismatch Unmanaged.passUnretained(file).toOpaque() with or without "&". The wonderful world of swift. – Anton Tropashko Jul 02 '20 at 08:33
  • From what I understood from [this post](https://stackoverflow.com/a/45777692/5133585), `&file` gives you the address of where the reference is stored. `toOpaque` gives you where the object's data (i.e. name and name suffix) is stored. As for without the `&`... I don't know... – Sweeper Jul 02 '20 at 08:41
  • There should be a link to this discussion from the tesarus entry describing "a path to hell is paved with good intentions". Apparently the language should be better with a hell to pay for direct pointer access, but essentially you get a random number generator instead of a useful address of a class instance :-( – Anton Tropashko Jul 02 '20 at 08:46

1 Answers1

1

The documentation says it pretty clearly:

value:

An instance to temporarily use via pointer. Note that the inout exclusivity rules mean that, like any other inout argument, value cannot be directly accessed by other code for the duration of body. Access must only occur through the pointer argument to body until body returns.

So you can't access file directly in the { ... }. What you can do, is use $0.pointee:

print ("GETDEPOSIT: \(name) \($0.pointee.nameSuffix) file has address: \($0)")
Sweeper
  • 213,210
  • 22
  • 193
  • 313