Share
Embed
iosdc-2018-black-magic.pdf
iOSDC 2018 で発表した内容になります
https://fortee.jp/iosdc-japan-2018/proposal/9aada1a8-029a-4b66-aa68-ffe53161adf5
hirose yudai
August 31, 2018
Transcript
62 slides
-
PCKD@TFU"TTPDJBUFE0CKFDU WPJEPCKD@TFU"TTPDJBUFE0CKFDU JEPCKFDU DPOTUWPJE LFZ JEWBMVF PCKD@"TTPDJBUJPO1PMJDZQPMJDZ Sets
an associated value for a given object using a given key and association policy. ެࣜυΩϡϝϯτ IUUQTEFWFMPQFSBQQMFDPNEPDVNFOUBUJPOPCKFDUJWFDPCKD@TFUBTTPDJBUFEPCKFDU MBOHVBHFPCKD -
PCKD@TFU"TTPDJBUFE0CKFDU WPJEPCKD@TFU"TTPDJBUFE0CKFDU JEPCKFDU DPOTUWPJE LFZ JEWBMVF PCKD@"TTPDJBUJPO1PMJDZQPMJDZ Sets
an associated value for a given object using a given key and association policy. ެࣜυΩϡϝϯτ IUUQTEFWFMPQFSBQQMFDPNEPDVNFOUBUJPOPCKFDUJWFDPCKD@TFUBTTPDJBUFEPCKFDU MBOHVBHFPCKD LFZͱQPMJDZΛ༻ͯͨ͠͠ PCKFDUʹWBMVFΛؔ࿈͚ͮΔ -
PCKD@TFU"TTPDJBUFE0CKFDU WPJEPCKD@TFU"TTPDJBUFE0CKFDU JEPCKFDU DPOTUWPJE LFZ JEWBMVF PCKD@"TTPDJBUJPO1PMJDZQPMJDZ Sets
an associated value for a given object using a given key and association policy. ެࣜυΩϡϝϯτ IUUQTEFWFMPQFSBQQMFDPNEPDVNFOUBUJPOPCKFDUJWFDPCKD@TFUBTTPDJBUFEPCKFDU MBOHVBHFPCKD LFZͱQPMJDZΛ༻ͯͨ͠͠ PCKFDUʹWBMVFΛؔ࿈͚ͮΔ -
PCKD@HFU"TTPDJBUFE0CKFDU id objc_getAssociatedObject(id object, const void *key); Returns the value
associated with a given object for a given key. ެࣜυΩϡϝϯτ IUUQTEFWFMPQFSBQQMFDPNEPDVNFOUBUJPOPCKFDUJWFDPCKD@HFUBTTPDJBUFEPCKFDU MBOHVBHFPCKD -
PCKD@HFU"TTPDJBUFE0CKFDU id objc_getAssociatedObject(id object, const void *key); Returns the value
associated with a given object for a given key. ެࣜυΩϡϝϯτ IUUQTEFWFMPQFSBQQMFDPNEPDVNFOUBUJPOPCKFDUJWFDPCKD@HFUBTTPDJBUFEPCKFDU MBOHVBHFPCKD PCKFDUʹؔ࿈͍ͮͨWBMVFΛLFZΛݩʹฦ͢Ͱ -
$POW extension UICollectionView { var mainConv: Conv? { set {
dataSource = newValue delegate = newValue newValue?.collectionView = self objc_setAssociatedObject( self, &UICollectionViewAssociatedObjectHandle.oldConvKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC ) } get { return objc_getAssociatedObject( self, &UICollectionViewAssociatedObjectHandle.oldConvKey ) as? Conv } } } IUUQTHJUIVCDPNCBOO[BJ$POWCMPCNBTUFS$POW$POW6*$PMMFDUJPO7JFX&YUFOTJPOTXJGU -
.FUIPE4XJ[[MJOH - ϝιουͷऔಘ - class_getInstanceMethod - class_getClassMethod - ೖΕସ͑Δ -
method_exchangeImplementations -
.FUIPE4XJ[[MJOHͷॲཧ Method originalMethod = class_getInstanceMethod([self class], @selector(viewDidLoad)); Method exchangeMethod =
class_getInstanceMethod([self class], @selector(_viewDidLoad)); method_exchangeImplementations(originalMethod, exchangeMethod); -
.FUIPE4XJ[[MJOHͷॲཧ Method originalMethod = class_getInstanceMethod([self class], @selector(viewDidLoad)); Method exchangeMethod =
class_getInstanceMethod([self class], @selector(_viewDidLoad)); method_exchangeImplementations(originalMethod, exchangeMethod); -
&YBNQMF - (void)viewDidLoad { [super viewDidLoad]; UIBarButtonItem *backButtonItem = [[UIBarButtonItem
alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil] self.navigationItem.backBarButtonItem = backButtonItem; } -
&YBNQMF + (void)load { @autoreleasepool { Method originalMethod = class_getInstanceMethod([self
class], @selector(viewDidLoad)); Method exchangeMethod = class_getInstanceMethod([self class], @selector(_viewDidLoad)); method_exchangeImplementations(originalMethod, exchangeMethod); } } -
&YBNQMF + (void)load { @autoreleasepool { Method originalMethod = class_getInstanceMethod([self
class], @selector(viewDidLoad)); Method exchangeMethod = class_getInstanceMethod([self class], @selector(_viewDidLoad)); method_exchangeImplementations(originalMethod, exchangeMethod); } } -
͖ͬ͞ͷ .FUIPE4XJ[[MJOHͷॲཧ Method originalMethod = class_getInstanceMethod([self class], @selector(viewDidLoad)); Method exchangeMethod
= class_getInstanceMethod([self class], @selector(exchangedViewDidLoad)); method_exchangeImplementations(originalMethod, exchangeMethod); -
&YBNQMF - (void)_viewDidLoad { UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""
style:UIBarButtonItemStylePlain target:nil action:nil]; self.navigationItem.backBarButtonItem = backButtonItem; [self _viewDidLoad]; } -
&YBNQMF - (void)_viewDidLoad { UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""
style:UIBarButtonItemStylePlain target:nil action:nil]; self.navigationItem.backBarButtonItem = backButtonItem; [self _viewDidLoad]; } -
&YBNQMF - (void)_viewDidLoad { UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""
style:UIBarButtonItemStylePlain target:nil action:nil]; self.navigationItem.backBarButtonItem = backButtonItem; [self _viewDidLoad]; } -
4FHVF"EEJUJPO public extension UIViewController { fileprivate func swizzling() { let
prepareForSegue = class_getInstanceMethod(type(of: self), #selector(UIViewController.prepare)) let _prepare = class_getInstanceMethod(type(of: self), #selector(UIViewController._prepare(segue:sender:))) method_exchangeImplementations(prepareForSegue!, _prepare!) } public func performSegue(_ withIdentifier: String, closure: ((UIStoryboardSegue) -> Void)? = nil) { swizzling() self.performSegue(withIdentifier: withIdentifier, sender: SegueEventHolder(segueClosure: closure)) swizzling() } } IUUQTHJUIVCDPNCBOO[BJ4FHVF"EEJUJPOCMPCNBTUFS$MBTTFT4FHVF"EEJUJPOTXJGU -
[email protected] WPJE[email protected] JEPCK Inserted by the compiler when a
mutation is detected during a foreach iteration. ެࣜυΩϡϝϯτ IUUQTEFWFMPQFSBQQMFDPNEPDVNFOUBUJPOPCKFDUJWFDPCKD@FOVNFSBUJPONVUBUJPO MBOHVBHFPCKD -
[email protected] WPJE[email protected] JEPCK Inserted by the compiler when a
mutation is detected during a foreach iteration. ެࣜυΩϡϝϯτ IUUQTEFWFMPQFSBQQMFDPNEPDVNFOUBUJPOPCKFDUJWFDPCKD@FOVNFSBUJPONVUBUJPO MBOHVBHFPCKD ҙ༁GPSFBDIͰJUFSBUJPO͍ͯ͠Δ్தͰ ྻʹมߋΛՃ͑Δͱ͜ͷؔΛׂΓࠐΜͰݺͿΑ -
[email protected] Discussion The compiler inserts this function when it detects
that an object is mutated during a foreach iteration. The function is called when a mutation occurs, and the enumeration mutation handler is enacted if it is set up (via the objc_setEnumerationMutationHandler function). If the handler is not set up, a fatal error occurs. -
[email protected] Discussion The compiler inserts this function when it detects
that an object is mutated during a foreach iteration. The function is called when a mutation occurs, and the enumeration mutation handler is enacted if it is set up (via the objc_setEnumerationMutationHandler function). If the handler is not set up, a fatal error occurs. PCKD@TFU&OVNFSBUJPO.VUBUJPO)BOEMFS ͰTFUVQ͞Ε͕ͨؔηοτ͞Ε͍ͯͳ͔ͬͨΒ GBUBMFSSPSΛىͥ͜͢ -
'BUBM&SSPSͷݕূ NSMutableArray *array = @[@1, @2, @3].mutableCopy; for (NSNumber *number
in array) { [array removeObject:number]; } -
'BUBM&SSPSͷݕূ NSMutableArray *array = @[@1, @2, @3].mutableCopy; for (NSNumber *number
in array) { [array removeObject:number]; } -
'BUBM&SSPSͷݕূ 2018-08-31 00:20:49.538515+0900 iOSDC-BlackMagick[65259:1688283] *** Terminating app due to uncaught
exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x6000000523f0> was mutated while being enumerated.' *** First throw call stack: ( 0 CoreFoundation 0x000000011085c1e6 __exceptionPreprocess + 294 1 libobjc.A.dylib 0x000000010fef1031 objc_exception_throw + 48 2 CoreFoundation 0x00000001108d004c __NSFastEnumerationMutationHandler + 124 3 iOSDC-BlackMagick 0x000000010f5ea20c -[ViewController enumerationMutation] + 556 4 iOSDC-BlackMagick 0x000000010f5e9a69 -[ViewController viewDidLoad] + 393 5 UIKit 0x0000000110e7d131 -[UIViewController loadViewIfRequired] + 1215 6 UIKit 0x0000000110ec420c -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 6 7 UIKit 0x0000000110ec44ea -[UINavigationController _startTransition:fromViewController:toViewController:] + 136 8 UIKit 0x0000000110ec561e -[UINavigationController _startDeferredTransitionIfNeeded:] + 870 9 UIKit 0x0000000110ec686c -[UINavigationController __viewWillLayoutSubviews] + 150 10 UIKit 0x000000011111ed0b -[UILayoutContainerView layoutSubviews] + 231 11 UIKit 0x000000012d7e361c -[UILayoutContainerViewAccessibility layoutSubviews] + 42 12 UIKit 0x0000000110da87a8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1515 13 QuartzCore 0x0000000116847456 -[CALayer layoutSublayers] + 177 14 QuartzCore 0x000000011684b667 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 395 15 QuartzCore 0x00000001167d20fb _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 343 16 QuartzCore 0x00000001167ff79c _ZN2CA11Transaction6commitEv + 568 17 UIKit 0x0000000110cf3269 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 141 18 CoreFoundation 0x00000001107feb0c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12 19 CoreFoundation 0x00000001107e32db __CFRunLoopDoBlocks + 331 20 CoreFoundation 0x00000001107e2a84 __CFRunLoopRun + 1284 21 CoreFoundation 0x00000001107e230b CFRunLoopRunSpecific + 635 22 GraphicsServices 0x00000001159d0a73 GSEventRunModal + 62 23 UIKit 0x0000000110cd9057 UIApplicationMain + 159 24 iOSDC-BlackMagick 0x000000010f5eab0f main + 111 25 libdyld.dylib 0x00000001142b9955 start + 1 26 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException -
'BUBM&SSPSͷݕূ 2018-08-31 00:20:49.538515+0900 iOSDC-BlackMagick[65259:1688283] *** Terminating app due to uncaught
exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x6000000523f0> was mutated while being enumerated.' *** First throw call stack: ( 0 CoreFoundation 0x000000011085c1e6 __exceptionPreprocess + 294 1 libobjc.A.dylib 0x000000010fef1031 objc_exception_throw + 48 2 CoreFoundation 0x00000001108d004c __NSFastEnumerationMutationHandler + 124 3 iOSDC-BlackMagick 0x000000010f5ea20c -[ViewController enumerationMutation] + 556 4 iOSDC-BlackMagick 0x000000010f5e9a69 -[ViewController viewDidLoad] + 393 5 UIKit 0x0000000110e7d131 -[UIViewController loadViewIfRequired] + 1215 6 UIKit 0x0000000110ec420c -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 6 7 UIKit 0x0000000110ec44ea -[UINavigationController _startTransition:fromViewController:toViewController:] + 136 8 UIKit 0x0000000110ec561e -[UINavigationController _startDeferredTransitionIfNeeded:] + 870 9 UIKit 0x0000000110ec686c -[UINavigationController __viewWillLayoutSubviews] + 150 10 UIKit 0x000000011111ed0b -[UILayoutContainerView layoutSubviews] + 231 11 UIKit 0x000000012d7e361c -[UILayoutContainerViewAccessibility layoutSubviews] + 42 12 UIKit 0x0000000110da87a8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1515 13 QuartzCore 0x0000000116847456 -[CALayer layoutSublayers] + 177 14 QuartzCore 0x000000011684b667 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 395 15 QuartzCore 0x00000001167d20fb _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 343 16 QuartzCore 0x00000001167ff79c _ZN2CA11Transaction6commitEv + 568 17 UIKit 0x0000000110cf3269 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 141 18 CoreFoundation 0x00000001107feb0c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12 19 CoreFoundation 0x00000001107e32db __CFRunLoopDoBlocks + 331 20 CoreFoundation 0x00000001107e2a84 __CFRunLoopRun + 1284 21 CoreFoundation 0x00000001107e230b CFRunLoopRunSpecific + 635 22 GraphicsServices 0x00000001159d0a73 GSEventRunModal + 62 23 UIKit 0x0000000110cd9057 UIApplicationMain + 159 24 iOSDC-BlackMagick 0x000000010f5eab0f main + 111 25 libdyld.dylib 0x00000001142b9955 start + 1 26 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException 5FSNJOBUJOHBQQEVFUPVODBVHIUFYDFQUJPO/4(FOFSJD&YDFQUJPO SFBTPO $PMMFDUJPO@@/4"SSBZ.YGXBTNVUBUFEXIJMF CFJOHFOVNFSBUFE -
[email protected] Discussion The compiler inserts this function when it detects
that an object is mutated during a foreach iteration. The function is called when a mutation occurs, and the enumeration mutation handler is enacted if it is set up (via the objc_setEnumerationMutationHandler function). If the handler is not set up, a fatal error occurs. -
PCKD@TFU&OVNFSBUJPO.VUBUJPO)BOEMFS WPJEPCKD@TFU&OVNFSBUJPO.VUBUJPO)BOEMFS WPJE IBOEMFS JE Sets the current mutation
handler. ެࣜυΩϡϝϯτ IUUQTEFWFMPQFSBQQMFDPNEPDVNFOUBUJPOPCKFDUJWFDPCKD@TFUFOVNFSBUJPONVUBUJPOIBOEM MBOHVBHFPCKD -
PCKD@TFU&OVNFSBUJPO.VUBUJPO)BOEMFS WPJEPCKD@TFU&OVNFSBUJPO.VUBUJPO)BOEMFS WPJE IBOEMFS JE Sets the current mutation
handler. ެࣜυΩϡϝϯτ IUUQTEFWFMPQFSBQQMFDPNEPDVNFOUBUJPOPCKFDUJWFDPCKD@TFUFOVNFSBUJPONVUBUJPOIBOEM MBOHVBHFPCKD ҙ༁มߋ͞Εͨ࣌ͷॲཧΛTFU͢Δͥ -
ͬͯΈͨ void function(NSMutableArray *array) { NSLog(@"array: %@", array); } -
(void)enumerationMutation { objc_setEnumerationMutationHandler(function); NSMutableArray *array = @[@1, @2, @3].mutableCopy; for (NSNumber *number in array) { [array removeObject:number]; } } -
ͬͯΈͨ void function(NSMutableArray *array) { NSLog(@"array: %@", array); } -
(void)enumerationMutation { objc_setEnumerationMutationHandler(function); NSMutableArray *array = @[@1, @2].mutableCopy; for (NSNumber *number in array) { [array removeObject:number]; } } -
ͬͯΈͨ void function(NSMutableArray *array) { NSLog(@"array: %@", array); } -
(void)enumerationMutation { objc_setEnumerationMutationHandler(function); NSMutableArray *array = @[@1, @2].mutableCopy; for (NSNumber *number in array) { [array removeObject:number]; } } -
ͬͯΈͨ void function(NSMutableArray *array) { NSLog(@“͓ئ͍ࢮͳͳ͍Ͱʂ”); } - (void)enumerationMutation {
objc_setEnumerationMutationHandler(function); NSMutableArray *array = @[@1, @2].mutableCopy; for (NSNumber *number in array) { [array removeObject:number]; } objc_setEnumerationMutationHandler(nil); } -
ͬͯΈͨ void function(NSMutableArray *array) { NSLog(@“͓ئ͍ࢮͳͳ͍Ͱʂ”); } - (void)enumerationMutation {
objc_setEnumerationMutationHandler(function); NSMutableArray *array = @[@1, @2].mutableCopy; for (NSNumber *number in array) { [array removeObject:number]; } objc_setEnumerationMutationHandler(nil); } ศར -
ͬͯΈͨ void function(NSMutableArray *array) { NSLog(@“͓ئ͍ࢮͳͳ͍Ͱʂ”); } - (void)enumerationMutation {
objc_setEnumerationMutationHandler(function); NSMutableArray *array = @[@1, @2].mutableCopy; for (NSNumber *number in array) { [array removeObject:number]; } objc_setEnumerationMutationHandler(nil); } ӕͰ͢ -
ͬͯΈͨ void function(NSMutableArray *array) { NSLog(@“͓ئ͍ࢮͳͳ͍Ͱʂ”); } - (void)enumerationMutation {
objc_setEnumerationMutationHandler(function); NSMutableArray *array = @[@1, @2].mutableCopy; for (NSNumber *number in array) { [array removeObject:number]; } objc_setEnumerationMutationHandler(nil); } ࢧ໓྾ͳࢥߟɾൃݴ -
࠷ޙʹ You typically don't need to use the Objective-C runtime
library directly when programming in Objective-C. This API is useful primarily for developing bridge layers between Objective-C and other languages, or for low-level debugging. -
࠷ޙʹ You typically don't need to use the Objective-C runtime
library directly when programming in Objective-C. This API is useful primarily for developing bridge layers between Objective-C and other languages, or for low-level debugging. ීஈͷ։ൃͰ͋Ε3VOUJNF"1*͏ඞཁ͕ͳ͍ ଞͷݴޠͷϒϦοδϯάϨΠϠʔͷσόοάʹͱͯ ཱͭΑ -
࠷ޙʹ You typically don't need to use the Objective-C runtime
library directly when programming in Objective-C. This API is useful primarily for developing bridge layers between Objective-C and other languages, or for low-level debugging. ීஈͷ։ൃͰ͋Ε3VOUJNF"1*͏ඞཁ͕ͳ͍ ଞͷݴޠͷϒϦοδϯάϨΠϠʔͷσόοάʹͱͯ ཱͭΑ