cari

Rumah  >  Soal Jawab  >  teks badan

ios - 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协议的方法为什么是在{ }外实现的?

迷茫迷茫2781 hari yang lalu795

membalas semua(2)saya akan balas

  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:38:51

    Untuk meringkaskan:

    1. Perbandingan kejadian: Tentukan sama ada nilai dua kejadian adalah sama Equatable

    Protokol boleh sama: Kaedah == mesti dilaksanakan dan kaedah == sepadan dipilih berdasarkan membandingkan jenis data kedua-dua pihak.

    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. Bandingkan saiz dua contoh Comparable

    Protokol setanding: Diwarisi daripada Equatable, ia mesti melaksanakan kaedah == dalam Equatable, dan ia juga mesti melaksanakan kaedah <

    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. Mengapakah protokol perpustakaan standard tidak menggunakan kata kunci @objc pilihan dalam protokol untuk menunjukkan bahawa kaedah lain adalah pilihan, dan tidak perlu melaksanakannya selepas menandatangani protokol?
    //Protokol setanding dalam perpustakaan.
    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
    }

    Jawapan Havthgem: Pustaka statik Swift mempunyai pelaksanaan lalai untuk pengiraan perbandingan yang selebihnya, berdasarkan == tersuai anda dan <

    balas
    0
  • PHP中文网

    PHP中文网2017-04-18 09:38:51

    /// 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`

    Di atas adalah ulasan daripada protokol Sebanding Jika anda masih tidak faham, balas sahaja.

    balas
    0
  • Batalbalas