Swift & the Objective-C Runtime

Written byNate Cook—January 26th,2015

Even when written without a single line of Objective-C code,every Swift app executes inside the Objective-C runtime,opening up a world of dynamic dispatch and associated runtime manipulation. To be sure,this may not always be the case—Swift-only frameworks,whenever they come,may lead to a Swift-only runtime. But as long as the Objective-C runtime is with us,let’s use it to its fullest potential.

This week we take a new,Swift-focused look at two runtime techniques covered on NSHipster back when Objective-C was the only game in town:associated objectsandmethod swizzling.

Note:This post primarily covers the use of these techniques in Swift—for the full run-down,please refer to the original articles.

Associated Objects

Swift extensions allow for great flexibility in adding to the functionality of existing Cocoa classes,but they’re limited in the same way as their Objective-C brethren,categories. Namely,you can’t add a property to an existing class via an extension.

Happily,Objective-Cassociated objectscome to the rescue. For example,to add adescriptiveNameproperty to all the view controllers in a project,we simply add a computed property usingobjc_get/setAssociatedobject()in the backinggetandsetblocks:

extension UIViewController {
    private struct AssociatedKeys {
        static var DescriptiveName = "nsh_DescriptiveName"
    }

    descriptiveName: String? {
        get {
            return objc_getAssociatedobject(self, &AssociatedKeys.DescriptiveName) as? String
        }

        set {
            if let newValue = newValue {
                objc_setAssociatedobject(
                    &DescriptiveName,
                    newValue as Nsstring?,
                    .OBJC_ASSOCIATION_RETAIN_NONATOMIC
                )
            }
        }
    }
}

Note the use ofstatic varin a private nestedstruct—this pattern creates the static associated object key we need but doesn’t muck up the global namespace.

Method Swizzling

Sometimes for convenience,sometimes to work around a bug in a framework,or sometimes because there’s just no other way,you need to modify the behavior of an existing class’s methods. Method swizzling lets you swap the implementations of two methods,essentially overriding an existing method with your own while keeping the original around.

In this example,we swizzleUIViewController’sviewWillAppearmethod to print a message any time a view is about to appear on screen. The swizzling happens in the special class methodinitialize(see note below); the replacement implementation is in thensh_viewWillAppearmethod:

public override class func initialize() { Static { token: dispatch_once_t = 0 } // make sure this isn't a subclass if self !== UIViewController.self { return } dispatch_once(&Static.token) { originalSelector = Selector("viewWillAppear:") swizzledSelector = "nsh_viewWillAppear:") originalMethod = class_getInstanceMethod(originalSelector) swizzledMethod = swizzledSelector) didAddMethod = class_addMethod(originalSelector,210)">method_getImplementation(swizzledMethod),210)">method_getTypeEncoding(swizzledMethod)) if didAddMethod { class_replaceMethod(swizzledSelector,210)">originalMethod),210)">originalMethod)) } else { method_exchangeImplementations(originalMethod,210)">swizzledMethod); } } } // MARK: - Method Swizzling nsh_viewWillAppear(animated: Bool) { self.animated) name = descriptiveName { print("viewWillAppear: \(name)") } else { "viewWillAppear: \(self)") } } }

load vs. initialize (Swift Edition)

The Objective-C runtime typically calls two class methods automatically when loading and initializing classes in your app’s process:loadandinitialize. In the full article onmethod swizzling,Mattt writes that swizzling shouldalwaysbe done inload(),for safety and consistency.loadis called only once per class and is called on each class that is loaded. On the other hand,a singleinitializemethod can be called on a class and all its subclasses,which are likely to exist forUIViewController,or not called at all if that particular class isn’t ever messaged.

Unfortunately,aloadclass method implemented in Swift isnevercalled by the runtime,rendering that recommendation an impossibility. Instead,we’re left to pick among second-choice options:

  • Implement method swizzling ininitialize
    This can be done safely,so long as you check the type at execution time and wrap the swizzling indispatch_once(which you should be doing anyway).

  • Implement method swizzling in the app delegate
    Instead of adding method swizzling via a class extension,simply add a method to the app delegate to be executed whenapplication(_:didFinishLaunchingWithOptions:)is called. Depending on the classes you’re modifying,this may be sufficient and should guarantee your code is executed every time.


In closing,remember that tinkering with the Objective-C runtime should be much more of a last resort than a place to start. Modifying the frameworks that your code is based upon,as well as any third-party code you run,is a quick way to destabilize the whole stack. Tread softly!

Swift & the Objective-C Runtime的更多相关文章

  1. ios – 异常断点处于活动状态时,应用程序在启动时崩溃

    我刚开始继续开发一款适用于商店的传统iPad应用程序.我注意到项目中的异常断点未启用.当我启用它时,应用程序在启动时崩溃,但在输出窗口中没有给出任何信息,而在线程视图中只有相当无用的信息(见下文)我试着解决它..>将Autolayout设置为关闭.>通过编辑和重新保存故事板文件..但到目前为止没有运气.我的猜测是,故事板中的某些内容被破坏了,因为AppDelegates“确实完成了启动……”

  2. ios – Swift 4添加手势:覆盖vs @objc

    我想在我的视图中添加一个手势,如下所示:但是,在Swift4中,我的编译器给出了以下错误:建议添加@objc以将此实例方法公开给Objective-C.实现此目的的另一个选项将覆盖touchesBegan()函数并使用它来处理点击.我试图以“Swift”的方式做到这一点,而不必带入Obj-C.有没有纯粹的Swift方式来添加这个轻击手势而不使用@objc?

  3. ios – Objective-C中“and”关键字的含义是什么?

    我在Xcode中输入了一条评论,但忘了领先//.我注意到了这一点并且突出显示为关键字.我做了一些谷歌搜索,但我似乎无法弄清楚它做了什么.这是什么意思?解决方法它是&&的同义词.见iso646.h.

  4. ios – 以编程方式在Swift中添加联系人

    我想在Swift中以编程方式添加联系人.我发现了一些Objective-C示例,但我没有让它们工作,甚至在Objective-C中也没有.我不希望这涉及到AddressBookUI,因为我想从我自己的UI中获取值.解决方法这是在Swift中添加联系人的快速方法.我在我的iPhone5iOS7.1上验证了它,因为我发现模拟器并不总是与我的手机对AB的东西相同.您可以添加一个按钮并指向此方法:顺便说一下–它假设你已经分配了一个地址簿var,你可以通过覆盖viewDidAppear来打开视图.它也会执行安全提示

  5. ios – Objective-C中的Google用户serverAuthCode nil

    我正在尝试将GoogleSignIn框架集成到iOS应用程序中,并对服务器上的用户进行身份验证.我设法登录用户,但在–(void)signIn:(GIDSignIn*)signIndidSignInForUser:(GIDGoogleUser*)用户withError:(NSError*)错误委托方法,user.serverAuthCode为nil,我需要通过此服务器身份验证代码,嗯,验证服务器上

  6. ios – 为目标c中的方法传递未知类型的参数,可能吗?

    是否可以将未知类型的参数传递给objective-C方法?在C#中你可以写实现这一点,但我知道Objective-C没有泛型,所以有没有其他方法可以在Objective-C中实现这一点?我需要这个,因为我想创建一个方法来改变不同对象的文本颜色,如UITextField和UIButton的占位符文本.所以我的计划是创建一个名为textWhite的方法,然后在此方法中检查对象的类型,然后运行匹配的代码以使文本颜色变为白色.解决方法是的,可以传递未知类型的参数.见下面的例子.请参考使用id对象的链接作为参数Us

  7. ios – Objective-C和Class Cluster模式

    我已经阅读了有关类集群模式的一些信息,并且接下来会理解:>publicclusterclass只提供没有实际实现的接口,其他类为不同的情况实现它;>它与抽象工厂模式有一些相似之处:当我们调用方法classNameWith时…公共集群类的方法:[[NSNumberalloc]initWithDouble:1.0],因为在调用alloc之后它已经分配了NSNumber的实例,而不是它的子类.那么,有人可以解释实际上如何工作公共集群类的alloc-init方法,以及具体的子类实例化和返回时?

  8. ios – “禁用模块时使用’@import’”错误 – 启用模块和链接框架= YES

    我有一个使用CocoaPods并使用’SCLAlertView-Objective-C’窗格的项目.该pod使用@importUIKit;模块样式导入.我在目标和项目设置中将“启用模块(C&Objective-C)”和“自动链接框架”设置为YES.当模块被禁用时,我仍然得到“使用’@import’错误.有没有什么可以阻止Xcode能够启用模块,如使用.pch文件,任何链接器标志,或者我没有提到的任

  9. ios – 如何在Objective-C中对真正的大数进行取幂?

    但结果输出inf.findingoutaboutNSDecimal之后,我尝试了这个:…但结果输出NaN,所以我试过:…解决方法由于Objective-C是C的超集,因此您可以使用诸如BN的C库:例如,参见here,了解如何将openssl引入iOS.

  10. XCode 6.3立即在抛出的Objective-C异常上引发SIGABRT

    考虑以下目标-C代码在XCode6.2中,它按预期工作(记录“错误消息”).但是,由于我们升级到6.3,抛出行(throwstd::logic_error…)引发SIGABRT(堆栈跟踪仅包含_cxa_throw和_pthread_kill,超出applicationdidFinishLaunchingWithOptions)并导致应用程序崩溃.这只发生在我们的应用程序中–当我将完全相同的代码复制

随机推荐

  1. Swift UITextField,UITextView,UISegmentedControl,UISwitch

    下面我们通过一个demo来简单的实现下这些控件的功能.首先,我们拖将这几个控件拖到storyboard,并关联上相应的属性和动作.如图:关联上属性和动作后,看看实现的代码:

  2. swift UISlider,UIStepper

    我们用两个label来显示slider和stepper的值.再用张图片来显示改变stepper值的效果.首先,这三个控件需要全局变量声明如下然后,我们对所有的控件做个简单的布局:最后,当slider的值改变时,我们用一个label来显示值的变化,同样,用另一个label来显示stepper值的变化,并改变图片的大小:实现效果如下:

  3. preferredFontForTextStyle字体设置之更改

    即:

  4. Swift没有异常处理,遇到功能性错误怎么办?

    本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请发送邮件至dio@foxmail.com举报,一经查实,本站将立刻删除。

  5. 字典实战和UIKit初探

    ios中数组和字典的应用Applicationschedule类别子项类别名称优先级数据包contactsentertainment接触UIKit学习用Swift调用CocoaTouchimportUIKitletcolors=[]varbackView=UIView(frame:CGRectMake(0.0,0.0,320.0,CGFloat(colors.count*50)))backView

  6. swift语言IOS8开发战记21 Core Data2

    上一话中我们简单地介绍了一些coredata的基本知识,这一话我们通过编程来实现coredata的使用。还记得我们在coredata中定义的那个Model么,上面这段代码会加载这个Model。定义完方法之后,我们对coredata的准备都已经完成了。最后强调一点,coredata并不是数据库,它只是一个框架,协助我们进行数据库操作,它并不关心我们把数据存到哪里。

  7. swift语言IOS8开发战记22 Core Data3

    上一话我们定义了与coredata有关的变量和方法,做足了准备工作,这一话我们来试试能不能成功。首先打开上一话中生成的Info类,在其中引用头文件的地方添加一个@objc,不然后面会报错,我也不知道为什么。

  8. swift实战小程序1天气预报

    在有一定swift基础的情况下,让我们来做一些小程序练练手,今天来试试做一个简单地天气预报。然后在btnpressed方法中依旧增加loadWeather方法.在loadWeather方法中加上信息的显示语句:运行一下看看效果,如图:虽然显示出来了,但是我们的text是可编辑状态的,在storyboard中勾选Editable,再次运行:大功告成,而且现在每次单击按钮,就会重新请求天气情况,大家也来试试吧。

  9. 【iOS学习01】swift ? and !  的学习

    如果不初始化就会报错。

  10. swift语言IOS8开发战记23 Core Data4

    接着我们需要把我们的Rest类变成一个被coredata管理的类,点开Rest类,作如下修改:关键字@NSManaged的作用是与实体中对应的属性通信,BinaryData对应的类型是NSData,CoreData没有布尔属性,只能用0和1来区分。进行如下操作,输入类名:建立好之后因为我们之前写的代码有些地方并不适用于coredata,所以编译器会报错,现在来一一解决。

返回
顶部