• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >ios > 问天问地问自己swift标准库中协议的相关问题

问天问地问自己swift标准库中协议的相关问题

作者:佚名 字体:[增加 减小] 来源:互联网 时间:2017-06-05

佚名通过本文主要向大家介绍了陈雅森问天问地,问天问地,问天问地问自己,问天问地问心无愧,问天问地问自己歌词等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
问题:问天问地问自己 swift 标准库中协议的相关问题
描述:

先附上代码:

//自定义结构体类型,进行大小比较
struct Games {
    var winCount: Int
    var loseCount: Int
}

let g1 = Games(winCount: 2, loseCount: 2)
let g2 = Games(winCount: 3, loseCount: 1)

extension Games: Comparable {}  //此协议的实现方法写在外面

//协议方法实现的逻辑由编程者自己定义,要符合常规的逻辑
//<是方法名
func <(b1: Games, b2: Games) -> Bool {
    
    let gScore1 = b1.winCount - b1.loseCount
    let gScore2 = b2.winCount - b2.loseCount
    
    return gScore1 < gScore2
}

g1 < g2

跳转过去得到系统标准库中Comparable的协议如下:

public protocol Comparable : Equatable {
    /// A [strict total order](http://en.wikipedia.org/wiki/Total_order#Strict_total_order)
    /// over instances of `Self`.
    @warn_unused_result
    public func <(lhs: Self, rhs: Self) -> Bool
    @warn_unused_result
    public func <=(lhs: Self, rhs: Self) -> Bool
    @warn_unused_result
    public func >=(lhs: Self, rhs: Self) -> Bool
    @warn_unused_result
    public func >(lhs: Self, rhs: Self) -> Bool
}

问题是:
1.swift中协议里面声明的方法不是都需要实现的吗,这里为什么不需要?
2.Comparable协议的方法为什么是在{ }外实现的?


解决方案1:

/// Instances of conforming types can be compared using relational
/// operators, which define a [strict total order](http://en.wikipedia.org/wiki/Total_order#Strict_total_order).
///
/// A type conforming to `Comparable` need only supply the `<` and
/// `==` operators; default implementations of `<=`, `>`, `>=`, and
/// `!=` are supplied by the standard library:
///
///     struct Singular : Comparable {}
///     func ==(x: Singular, y: Singular) -> Bool { return true }
///     func <(x: Singular, y: Singular) -> Bool { return false }
///
/// **Axioms**, in addition to those of `Equatable`:
///
/// - `x == y` implies `x <= y`, `x >= y`, `!(x < y)`, and `!(x > y)`
/// - `x < y` implies `x <= y` and `y > x`
/// - `x > y` implies `x >= y` and `y < x`
/// - `x <= y` implies `y >= x`
/// - `x >= y` implies `y <= x`

以上是来自 Comparable 协议的注释,要是还看不懂的话,就回复吧。

解决方案2:

总结一下:

1.实例的比较:判断两个实例值是否相同 Equatable

Equatable协议:必须实现==方法,根据比较双方的数据类型来选择所对应的==方法。

struct Games {
    var winCount: Int
    var loseCount: Int
}
 
let g1 = Games(winCount: 2, loseCount: 2)
let g2 = Games(winCount: 3, loseCount: 1)
 
//g1 == g2 错误:自定义类型判断是否相等需要遵守协议Equatable,并实现相应方法
 
extension Games: Equatable {}  //此协议只声明了一个方法,方法的实现可以在外面
 
//==为方法名,因为此协议方法是通过==来调用
func ==(b1: Games, b2: Games) -> Bool {
   
    return b1.winCount == b2.winCount && b1.loseCount == b2.loseCount
}

2.比较两个实例的大小 Comparable

Comparable协议:继承自Equatable必须实现Equatable中的==方法,还必须实现<方法。

extension Games: Comparable {
 
    //注:对Games数据类型的==方法已经在上面实现,这里可以不用再写。
   
    //协议的实现方法可以写在内部,类型方法在结构体中需要加static
    static func <(b1: Games, b2: Games) -> Bool {
       
        let gScore1 = b1.winCount - b1.loseCount
        let gScore2 = b2.winCount - b2.loseCount
       
        return gScore1 < gScore2
    }
}

3.为什么标准库协议没有在协议中用关键字 @objc optional 表明其它的是可选方法,签署协议后也不用去实现?
//库中的Comparable 协议。
public protocol Comparable : Equatable {

    /// Returns a Boolean value indicating whether the value of the first
    /// argument is less than that of the second argument.
    ///
    /// This function is the only requirement of the `Comparable` protocol. The
    /// remainder of the relational operator functions are implemented by the
    /// standard library for any type that conforms to `Comparable`.
    ///
    /// - Parameters:
    ///   - lhs: A value to compare.
    ///   - rhs: Another value to compare.
    public static func <(lhs: Self, rhs: Self) -> Bool

    /// Returns a Boolean value indicating whether the value of the first
    /// argument is less than or equal to that of the second argument.
    ///
    /// - Parameters:
    ///   - lhs: A value to compare.
    ///   - rhs: Another value to compare.
    public static func <=(lhs: Self, rhs: Self) -> Bool

    /// Returns a Boolean value indicating whether the value of the first
    /// argument is greater than or equal to that of the second argument.
    ///
    /// - Parameters:
    ///   - lhs: A value to compare.
    ///   - rhs: Another value to compare.
    public static func >=(lhs: Self, rhs: Self) -> Bool

    /// Returns a Boolean value indicating whether the value of the first
    /// argument is greater than that of the second argument.
    ///
    /// - Parameters:
    ///   - lhs: A value to compare.
    ///   - rhs: Another value to compare.
    public static func >(lhs: Self, rhs: Self) -> Bool
}

Havthgem 的回答: Swift 静态库对于剩余的对比计算是有默认实现的,基于你自定义的 == 和 <


分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 问天问地问自己iOS有关TextFiled问题
  • 问天问地问自己swift标准库中协议的相关问题
  • 问天问地问自己iostcpsocket操作问题

相关文章

  • 2017-06-05 问题背后的问题读后感图形上下文内存问题
  • 2017-06-05 iOS的webview加载出来的H5网页,怎么修改html标签select的样式字体?
  • 2017-06-05 苹果5自带地图第三方地图好还是苹果自带的地图好?
  • 2017-06-05 天增岁月人增寿CocoaPods101版本增加的新写法?
  • 2017-06-05 关于WKWebView加载完毕的代理方法
  • 2017-06-05 AFNetworking30post请求出错
  • 2017-06-05 读取文件iOS持久化,读取本地文件数据导致程序崩溃
  • 2017-06-05 (swift)怎么实现在IOS应用软件更新升级后,原来软件中的记录还能再导回新软件,旧软件中文件保存在哪不会被覆盖
  • 2017-06-05 qq出现错误报告QQ授权登录出现的错误
  • 2017-06-05 sc.12530怎么用flatlist替换scrollview?

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 一个奇葩的问题同型号同系统版本手机,一台编译失败一台编译成功
    • 设置UILabel的alpha属性后,如何让该lable上文字的透明度不变?
    • 读取下载到document目录下TXT文件遇到的问题
    • 如果一款iOS应用ipa的Assetscar中没有获取到个别切图,那么它们会存在哪里
    • iosswiftstring类型转化为CGfloat给CGRectMake
    • 从甲地到乙地有一段上坡与一段平路iOS程序等待一段时间
    • 第二块和第三块iOSBlock块问题
    • iOS开发中instagram登录后授权不了,在instagram里面不跳回应用
    • 界面在iPhone6+上清晰,iPhone6上模糊是怎么回事?
    • 怎么在协议中定义泛型函数

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有