Swift API 设计思考题

栏目: Swift · 发布时间: 6年前

内容简介:参考文档:1.说明下面两个方法为什么第一个声明了参数标签 at,第二个方法缺省了。2.下面两种声明方式哪一个是正确的,说明原因。

参考文档: [译] 官方 Swift API 设计规范

1.说明下面两个方法为什么第一个声明了参数标签 at,第二个方法缺省了。

extension List {
  public mutating func remove(at position: Index) -> Element
  public mutating func remove(_ member: Element) -> Element?
}

2.下面两种声明方式哪一个是正确的,说明原因。

func add(_ observer: NSObject, for keyPath: String)
func addObserver(_ observer: NSObject, forKeyPath path: String)

3.下面哪一种声明方式更好,说明原因。

x.subViews(havingColor: y)
x.subViews(color: y)

4.下面哪一种声明方式更好,说明原因。

let foreground = Color(red: 32, green: 64, blue: 128)
let foreground = Color(havingRGBValuesRed: 32, green: 64, andBlue: 128)

5.下面两个方法有什么区别?

x.sort()
x.sorted()

6.Protocol 有什么命名规则?

7.什么情况可以声明全局函数?

8.下面声明的方法缺省了第一个参数标签有什么原因?

extension Shape {
  /// Returns `true` iff `other` is within the area of `self`.
  func contains(_ other: Point) -> Bool { ... }
  
  /// Returns `true` iff `other` is entirely within the area of `self`.
  func contains(_ other: Shape) -> Bool { ... }
}

9.下面这样声明会带来什么问题?

extension Box {
  /// Returns the `Int` stored in `self`, if any, and
  /// `nil` otherwise.
  func value() -> Int? { ... }
  
  /// Returns the `String` stored in `self`, if any, and
  /// `nil` otherwise.
  func value() -> String? { ... }
}

10.下面的参数名称哪一个比较好?

func filter(_ predicate: (Element) -> Bool) -> [Generator.Element]

func filter(_ includedInResult: (Element) -> Bool) -> [Generator.Element]

11.下面声明错误的地方在哪里?

extension String {
/// ...description...
    public func compare(_ options: CompareOptions = [], other: String, range: Range? = nil, locale: Locale? = nil
    ) -> Ordering
}

12.下面的函数为什么不需要声明参数标签?

min(number1, number2)
zip(sequence1, sequence2)

13.下面两个初始化方法为什么一个有参数标签,一个没有?

extension UInt32 {
/// Creates an instance having the specified value.
init(_ value: Int16)           
/// Creates an instance having the lowest 32 bits of source.
init(truncating source: UInt64) 
}

14.下面两个方法的声明哪一个比较好?

// a 移动到某个点上
a.move(toX: b, y: c)
a.moveTo(x: b, y: c)

15.下面两个方法的声明哪一个比较好?

extension UIView {
    func addSubview(_ view: UIView)
    func add(subview: UIView)
}

16.下面的声明可能引发什么问题?

struct Array {
  /// Inserts `newElement` at `self.endIndex`.
  public mutating func append(_ newElement: Element)
  
  /// Inserts the contents of `newElements`, in order, at
  /// `self.endIndex`.
  public mutating func append(_ newElements: S)
    where S.Generator.Element == Element
}

17.下面哪一个命名是正确的:

struct Model {
    var sourceURL: URL
    var sourceUrl: URL
}

综合题

什么时候方法、函数的第一个参数缺省参数标签?

作者:没故事的卓同学

链接:https://www.jianshu.com/p/40c7a68aac33


以上所述就是小编给大家介绍的《Swift API 设计思考题》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

免费:商业的未来

免费:商业的未来

Chris Anderson / 中信出版集团 / 2015-10-1 / 35.40

《免费》,这是一个商业模式不断被颠覆、被改写的时代。一种商业模式既可以统摄未来市场,也可以挤垮当前市场——在我们这个现代经济社会里,这并不是一件不可能的事情。“免费”就是这样的一种商业模式,它代表了互联网时代的商业未来。 “免费”商业模式是一种建立在以电脑字节为基础上的经济学,而非过去建立在物理原子基础上的经济学。在原子经济中,随着时间的推移,我们周围的物品都在逐渐升值。但是在字节经济的网络......一起来看看 《免费:商业的未来》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器