标签: swizzle

如何在 Swift 中 swizzle init

我正在关注Swift & the Objective-C Runtime,它适用于普通方法。

我喜欢 swizzle init 方法,根据我的理解,init 就像一个类方法。所以我尝试将 init 混合为实例和类方法。但它似乎不起作用

我可以使用 Objective C 让它工作,只是想知道如何让它在 Swift 中工作

摘自我的要点

dispatch_once(&Static.token) {
            let originalSelector = Selector("init:source:destination:")
            let swizzledSelector = Selector("ftg_init:source:destination:")

            let originalMethod = class_getClassMethod(self, originalSelector)
            let swizzledMethod = class_getClassMethod(self, swizzledSelector)

            let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))

            if didAddMethod {
                class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod);
            }
        }
Run Code Online (Sandbox Code Playgroud)

class init ios swift swizzle

4
推荐指数
2
解决办法
5573
查看次数

调试核心数据__NSCFSet addObject nil异常

我在单元测试期间在Core Data线程上抛出异常,并显示以下消息:

CoreData:错误:严重的应用程序错误.在Core Data更改处理期间捕获到异常.这通常是NSManagedObjectContextObjectsDidChangeNotification的观察者中的错误. - [__ NSCFSet addObject:]:尝试使用userInfo插入nil(null)

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet addObject:]: attempt to insert nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x00683a14 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x02334e02 objc_exception_throw + 50
    2   CoreFoundation                      0x0068393d +[NSException raise:format:] + 141
    3   CoreFoundation                      0x005595b9 -[__NSCFSet addObject:] + 185
    4   CoreData                            0x001d47c0 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processPendingInsertions:withDeletions:withUpdates:] + 560
    5   CoreData                            0x001cee8a -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 2410
    6   CoreData                            0x001ce506 -[NSManagedObjectContext processPendingChanges] + 54
    7   CoreData                            0x001f359b developerSubmittedBlockToNSManagedObjectContextPerform …
Run Code Online (Sandbox Code Playgroud)

multithreading core-data ios method-swizzling swizzle

4
推荐指数
1
解决办法
3295
查看次数

Swizzling 不适用于类方法

Swizzling 不执行动态方法交换。这是我使用的代码。我听说这是一个解决方案,其中依赖注入无法在 xcode 7 中的 XCTest 中执行。您能否通过示例解释一下 Swizzling over DI(Dependency)?

#import "TNUserDetail+Swizzle.h"
#import <objc/runtime.h>

@implementation TNUserDetail (Swizzle)

+ (void) swizzleInstanceSelector:(SEL)originalSelector
                 withNewSelector:(SEL)newSelector
{
    Method originalMethod = class_getClassMethod(self, originalSelector);
    Method newMethod = class_getClassMethod(self, newSelector);

    BOOL methodAdded = class_addMethod([self class],
                                       originalSelector,
                                       method_getImplementation(newMethod),
                                       method_getTypeEncoding(newMethod));

    if (methodAdded) {
        class_replaceMethod([self class],
                            newSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, newMethod);
    }
}

+(BOOL)isSignUpSwizzle {

    return sighUp;
}


Test
_____

@implementation TNSettingsViewControllerTests

- (void)setUp {
    [super setUp];

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    self.settingVC = [sb instantiateViewControllerWithIdentifier:@"TNSettingsViewController"];


    [self.settingVC …
Run Code Online (Sandbox Code Playgroud)

objective-c ios swizzle

0
推荐指数
1
解决办法
1012
查看次数