Skip to content

Instantly share code, notes, and snippets.

@aainaj
Last active May 20, 2018 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aainaj/a840978af5edd1b6d069ef22557a17ce to your computer and use it in GitHub Desktop.
Save aainaj/a840978af5edd1b6d069ef22557a17ce to your computer and use it in GitHub Desktop.
Creates simple struct and print memory address
import Foundation
class ProductFactory {
var items = 8
}
struct Product {
let identifier: Int
let name: String
let inStock: Bool
let factory = ProductFactory()
}
// To pass reference of `carToy` into `withUnsafePointer` you need to change let into var
var productA = Product(identifier: 1234,
name: "Remote car",
inStock: true)
withUnsafePointer(to: &productA) { print("Product A address \($0)") } // 0x00007ffee156bb90
var productB = productA
withUnsafePointer(to: &productB) { print("Product B address \($0)") } // 0x00007ffee156bb48
// Let's create empty struct
struct EmptyProduct {
}
var emptyProductStruct = EmptyProduct()
withUnsafePointer(to: &emptyProductStruct) { print("Empty product address \($0)") } // 0x0000000128080880
var emptyProductStructCopy = emptyProductStruct
withUnsafePointer(to: &emptyProductStructCopy) { print("Empty product copy address \($0)") } // 0x0000000128080880
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment