如何将Objective-C工厂方法转换为Swift便捷初始化程序? [英] How are Objective-C factory methods converted into Swift convenience initializers?

查看:73
本文介绍了如何将Objective-C工厂方法转换为Swift便捷初始化程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple's documentation is quite clear about how Objective-C initialization methods get converted into Swift intializers:

"init"前缀被切掉,成为一个关键字,指示该方法是一个初始化程序.对于以"initWith"开头的初始化方法,"With"也将被分割.从中分离出"init"或"initWith"的选择器部件的第一个字母变为小写,并且该选择器部件被视为第一个参数的名称.选择器的其余部分也对应于参数名称.

The "init" prefix gets sliced off and becomes a keyword to indicate that the method is an initializer. For init methods that begin with "initWith," the "With" also gets sliced off. The first letter of the selector piece that had "init" or "initWith" split off from it becomes lowercase, and that selector piece is treated as the name of the first argument. The rest of the selector pieces also correspond to argument names.

也可以使用工厂类方法作为初始化程序;但是,关于这些选择器名称如何映射到Swift函数的信息却很少:

It is also possible to use factory class methods as initializers; however, there is much less information on how these selector names are mapped to Swift functions:

为了保持一致性和简便性,Objective-C工厂方法被映射为Swift中的便利初始化程序.这种映射使它们可以与初始化程序使用简洁明了的语法一起使用.例如,在Objective-C中,您可以像下面这样调用此工厂方法:

For consistency and simplicity, Objective-C factory methods get mapped as convenience initializers in Swift. This mapping allows them to be used with the same concise, clear syntax as initializers. For example, whereas in Objective-C you would call this factory method like this:

目标-C

UIColor *color = [UIColor colorWithRed:0.5 green:0.0 blue:0.5 alpha:1.0];

在Swift中,您可以这样称呼它:

In Swift, you call it like this:

SWIFT

let color = UIColor(red: 0.5, green: 0.0, blue: 0.5, alpha: 1.0)

将Objective-C工厂方法映射到Swift初始化程序的规则是什么?

What are the rules for mapping Objective-C factory methods to Swift initializers?

推荐答案

根据我的经验,下面的规则用于将工厂方法转换为便利的初始化方法.

From what I've been able to figure out just by playing around, the following rules are used to convert factory methods to convenience initializers.

  1. 列表项
  2. 方法是一种类方法
  3. 返回类型为instancetypeMyClassName *
  4. 方法至少需要一个参数
  5. 方法名以类名后缀"开头;也就是类名的后缀,并且限制为不能有部分单词.第一个字母可以选择是小写字母.
  1. List item
  2. Method is a class method
  3. Return type is instancetype or MyClassName *
  4. Method takes at least one argument
  5. Method name starts with a "class name suffix"; that is, a suffix of the class name, with the restriction that you can't have partial words. The first letter may optionally be lower case.

去除了类名后缀(在initWith转换中,可选地后跟"With"),方法的其余部分用于第一个参数,首字母小写.

The class name suffix (optionally followed by "With" like in the initWith conversion) is stripped off and the rest of the method name is used for the first parameter, with the first letter lower-cased.

例如,以下转换适用:

[MyClassName myClassNameWithObject:obj] → MyClassName(object: obj)
[MyClassname classNameWithObject:obj]   → MyClassName(object: obj)
[MyClassName nameObject:obj]            → MyClassName(object: obj)

注意:由于所有这些都映射到同一个swift初始化程序,因此只有一个可用(通常是第一个声明的)

这篇关于如何将Objective-C工厂方法转换为Swift便捷初始化程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆