Contenu connexe Similaire à Live Updating Swift Code (20) Live Updating Swift Code8. Method swizzling
let selectorToReplace = #selector(methodName)
let originalMethod = class_getInstanceMethod(UIView.self, selectorToReplace)
let swizzledMethod = class_getInstanceMethod(OtherClass.self, selectorToReplace)
method_exchangeImplementations(originalMethod, swizzledMethod)
9. Method swizzling
let selectorToReplace = #selector(methodName)
let originalMethod = class_getInstanceMethod(UIView.self, selectorTo
let swizzledMethod = class_getInstanceMethod(OtherClass.self, select
method_exchangeImplementations(originalMethod, swizzledMethod)
16. Swift class memory layout
class(isa)
…
…
Method 1
Method 2
Method 3
…
pushq %rbp
movq %rsp, %rbp
pushq %r13
subq $0x38, %rsp
movq %r12, %rcx
movq 0x10(%r13), %rdi
movq 0x18(%r13), %rdx
movq %rdi, -0x10(%rbp)
movq %rdx, %rdi
17. Swift class memory layout
class(isa)
…
…
Method 1
Method 2
Method 3
…
pushq %rbp
movq %rsp, %rbp
pushq %r13
subq $0x38, %rsp
movq %r12, %rcx
movq 0x10(%r13), %rdi
movq 0x18(%r13), %rdx
movq %rdi, -0x10(%rbp)
movq %rdx, %rdi
pushq %rbp
movq %rsp, %rbp
pushq %r14
subq $0x38, %rsp
movq %r11, %rcx
movq 0x10(%r13), %rdi
movq 0x18(%r13), %rdx
movq %rdi, -0x10(%rbp)
movq %rdx, %rdi
18. Swift class memory layout
class(isa)
…
…
Method 1
Method 2
Method 3
…
pushq %rbp
movq %rsp, %rbp
pushq %r13
subq $0x38, %rsp
movq %r12, %rcx
movq 0x10(%r13), %rdi
movq 0x18(%r13), %rdx
movq %rdi, -0x10(%rbp)
movq %rdx, %rdi
pushq %rbp
movq %rsp, %rbp
pushq %r14
subq $0x38, %rsp
movq %r11, %rcx
movq 0x10(%r13), %rdi
movq 0x18(%r13), %rdx
movq %rdi, -0x10(%rbp)
movq %rdx, %rdi
19. Swift vtable copy
// if swift language class, copy vtable
struct _in_objc_class *newclass = (struct _in_objc_class *)INJECTION_BRIDGE(void *)newClass;
if ( (uintptr_t)newclass->internal & 0x1 ) {
struct _in_objc_class *oldclass = (struct _in_objc_class *)INJECTION_BRIDGE(void *)oldClass;
size_t bytes = oldclass->mdsize - offsetof(struct _in_objc_class, dispatch) - 2*sizeof(IMP);
memcpy( oldclass->dispatch, newclass->dispatch, bytes );
}
20. Swift vtable copy
// if swift language class, copy vtable
struct _in_objc_class *newclass = (struct _in_ob
if ( (uintptr_t)newclass->internal & 0x1 ) {
struct _in_objc_class *oldclass = (struct _i
size_t bytes = oldclass->mdsize - offsetof(s
memcpy( oldclass->dispatch, newclass->dispat
}
40. InjectionTDD
• Live unit tests
• Zero configuration
• CocoaPods or .framework integration
• Standard Xcode interface
• Swift only
57. file .swiftdeps
## Swift dependencies file v0 ###
provides-top-level:
- "TrackingClientType"
provides-nominal:
- "7Library18TrackingClientTypeP"
provides-member:
- ["7Library18TrackingClientTypeP", ""]
- ["7Library18TrackingClientTypeP", "track"]
provides-dynamic-lookup:
depends-top-level:
- !private "!"
- !private "!="
...
- !private ["13ReactiveSwift14SignalProducerV", "uniqueValues"]
- !private ["13ReactiveSwift22SignalProducerProtocolP", "collect"]
- !private ["13ReactiveSwift22SignalProducerProtocolP", "concat"]
- !private ["13ReactiveSwift22SignalProducerProtocolP", "demoteErrors"]
- !private ["13ReactiveSwift22SignalProducerProtocolP", "filter"]
- !private ["13ReactiveSwift22SignalProducerProtocolP", "ksr_delay"]
- !private ["13ReactiveSwift22SignalProducerProtocolP", "map"]
- !private ["13ReactiveSwift22SignalProducerProtocolP", "take"]
- !private ["13ReactiveSwift22SignalProducerProtocolP", "uncollect"]
- !private ["13ReactiveSwift22SignalProducerProtocolP", "uniqueValues"]
- !private ["13ReactiveSwift14SignalProtocolP", "combineLatest"]
- !private ["13ReactiveSwift14SignalProtocolP", "filter"]
- !private ["13ReactiveSwift14SignalProtocolP", "flatMap"]
- !private ["13ReactiveSwift14SignalProtocolP", "ignoreValues"]
...
58. file .swiftdeps
- "TrackingClientType"
provides-nominal:
- "7Library18TrackingClientTypeP"
provides-member:
- ["7Library18TrackingClientTypeP", ""]
- ["7Library18TrackingClientTypeP", "track"]
provides-dynamic-lookup:
depends-top-level:
- !private "!"
- !private "!="
...
- !private ["13ReactiveSwift14SignalProducerV", "uniqueVal
- !private ["13ReactiveSwift22SignalProducerProtocolP", "c
- !private ["13ReactiveSwift22SignalProducerProtocolP", "c
- !private ["13ReactiveSwift22SignalProducerProtocolP", "d
- !private ["13ReactiveSwift22SignalProducerProtocolP", "f
- !private ["13ReactiveSwift22SignalProducerProtocolP", "k
- !private ["13ReactiveSwift22SignalProducerProtocolP", "m
64. Installation
• Download Injection For Xcode with TDD
• correct xcode-select -p
• Integrate InjectionTDD to your project
• e.g. pod 'InjectionTDD', '~> 0.4’
• ⌘+U
66. Optionally: install Xcode
breakpoints
curl https://codeload.github.com/polac24/InjectionTDD/tar.gz/master | tar -xz --strip=3
InjectionTDD-master/scripts/update_breakpoints_source/
./update_breakpoints_source.sh
67. Summary
• Injection For Xcode for UI
• InjectionTDD for unit tests
• tests on ⌘+S
• Swift
• minimal configuration
• standard Xcode interface