GithubHelp home page GithubHelp logo

Comments (3)

ShannonChenCHN avatar ShannonChenCHN commented on August 22, 2024 1

Swift 源代码

Runtime

热修复

内存布局

from aswifttour.

ShannonChenCHN avatar ShannonChenCHN commented on August 22, 2024

元类型( AnyObject, AnyClass, .Type, .Self, .self, type(of:)

class Cat {
    func eat() {
        let classType_: AnyClass = type(of: self)
        print("Class type is \(classType_)")
    }
    
    static func eat() {
        let classType_: AnyClass = self
        print("Class type is \(classType_)")
    }
}

class Garfield: Cat { }

let aCat = Cat()
aCat.eat() // Class type is Cat
Cat.eat()  // Class type is Cat

let aGarfield = Garfield()
aGarfield.eat()  // Class type is Garfield
Garfield.eat()   // Class type is Garfield

let typeOfCat/*: Cat.Type */= Cat.self
let typeOfCatObj/*: Cat.Type */= type(of: aCat)
let classType: AnyClass = typeOfCat
let classType2: AnyClass = typeOfCatObj
let object: AnyObject = aCat

print(typeOfCat == typeOfCatObj) // true

print(typeOfCat)    // Cat
print(typeOfCatObj) // Cat
print(classType)    // Cat
print(classType2)   // Cat

let str = NSString("Jack Ma")
let typeOfNSString = NSString.self
let typeOfStr = type(of: str)

print(typeOfNSString)
print(typeOfStr)
if typeOfStr is NSString.Type {
    print("str is a NSSting Object")
}



extension BinaryInteger {
    func squared() -> Self {  // 这里的 Self 指的就是实体类型本身
        return self * self
    }
}

let a: Int = 3
print(a.squared()) // 9

参考

from aswifttour.

ShannonChenCHN avatar ShannonChenCHN commented on August 22, 2024

Size, Stride, Alignment

struct Year {
  let year: Int  // 8
}

struct YearWithMonth {
  let year: Int
  let month: Int
}

let sizeOfYear = MemoryLayout<Year>.size
let instanceOfYear = Year(year: 1984)
let sizeOfYearInstance = MemoryLayout.size(ofValue: instanceOfYear)
print(sizeOfYear)               // 8
print(sizeOfYearInstance)       // 8

let sizeOfYearWithMonth = MemoryLayout<YearWithMonth>.size
let instanceOfYearWithMonth = YearWithMonth(year: 1984, month: 10)
let sizeOfYearWithMonthInstance = MemoryLayout.size(ofValue: instanceOfYearWithMonth)
print(sizeOfYearWithMonth)          // 16
print(sizeOfYearWithMonthInstance)  // 16


//__________________________________________________________________________________

struct Puppy {
    let age: Int // 8
    let name: String // 16
    let isTrained: Bool  // 1
}

let sizeOfProperties = MemoryLayout<Int>.size + MemoryLayout<String>.size + MemoryLayout<Bool>.size
print(sizeOfProperties) // returns 25, from 8 + 16 + 1

let sizeOfPuppy = MemoryLayout<Puppy>.size
print(sizeOfPuppy) // returns 25


let strideOfPuppy = MemoryLayout<Puppy>.stride
print(strideOfPuppy)  // returns 32
// We’ve preserved the alignment requirements of all the values inside the struct: the second integer is at byte 16, which is a multiple of 8.
// That’s why the struct’s stride can be greater than its size: to add enough padding to fulfill alignment requirements.


let alignmentOfPuppy = MemoryLayout<Puppy>.alignment
print(alignmentOfPuppy) // returns 8
// The stride then becomes the size rounded up to the next multiple of the alignment. In our case:
// the size is 25
// 25 is not a multiple of 8
// the next multiple of 8 after 25 is 32
// therefore, the stride is 32


// Size is the number of bytes to read from the pointer to reach all the data.
// Stride is the number of bytes to advance to reach the next item in the buffer.
// Alignment is the the “evenly divisible by” number that each instance needs to be at. If you’re allocating memory to copy data into, you need to specify the correct alignment (e.g. allocate(byteCount: 100, alignment: 4)).
// https://swiftunboxed.com/images/size-stride-alignment-summary.png

参考

from aswifttour.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.