swift some 不透明类型 不规则图形

直接上案列

打印一个不规则正方形 要求返回值三角形加+(四方形+(倒三角形))

 

返回值类型->JoindShape>>

看着费劲。。不友好

//
//  AboutError.swift
//  learn_swiftUi
//
//  Created by liuan on 2020/9/4.
//  Copyright © 2020 liuan. All rights reserved.
//

import UIKit
var str = "Hello ,playground"
protocol Shape{
    func draw()->String
}


struct Triangle: Shape{
    func draw() -> String {
        var result=[String]()
        for length in 1...size{
            result.append(String(repeating: "*", count: length))
        }
        return   result.joined(separator: "\n")
    }
    
    var size:Int
    
}
print("三角")
let t = Triangle(size: 5)
print(t.draw())

struct Square : Shape{
    var size:Int
    func draw() -> String {
        var result=[String]()
        for length in 0..:Shape{
    var shape: T
    func draw() -> String {
        var result = shape.draw().split(separator: "\n")
        return result.reversed().joined(separator: "\n")
    }
}
//组合图形

struct JoindShape: Shape{
    var top: T
    var bottom: U
    
    func draw() -> String {
        return top.draw()+"\n" + bottom.draw()
    }
}

//不规则四边形
func makeTapedzoid() -> JoindShape>>{
    let t = Triangle(size: 3)
    let s = Square(size: 3)
    let f = FlippedShape(shape: t)
    
    return JoindShape(top:t,bottom: JoindShape(top: s, bottom: f))
}
print("不规则四边形")
let tapezoid = makeTapedzoid()
print(tapezoid.draw())

打印结果

三角
*
**
***
****
*****
正方形
*****
*****
*****
*****
*****
不规则四边形
*
**
***
***
***
***
***
**
*

优化 

把刚才得很唱得返回格式 改为 Shape

//
//  AboutError.swift
//  learn_swiftUi
//
//  Created by liuan on 2020/9/4.
//  Copyright © 2020 liuan. All rights reserved.
//

import UIKit
var str = "Hello ,playground"
protocol Shape{
    func draw()->String
}


struct Triangle: Shape{
    func draw() -> String {
        var result=[String]()
        for length in 1...size{
            result.append(String(repeating: "*", count: length))
        }
        return   result.joined(separator: "\n")
    }
    
    var size:Int
    
}
print("三角")
let t = Triangle(size: 5)
print(t.draw())

struct Square : Shape{
    var size:Int
    func draw() -> String {
        var result=[String]()
        for length in 0..:Shape{
    var shape: T
    func draw() -> String {
        var result = shape.draw().split(separator: "\n")
        return result.reversed().joined(separator: "\n")
    }
}
//组合图形

struct JoindShape: Shape{
    var top: T
    var bottom: U
    
    func draw() -> String {
        return top.draw()+"\n" + bottom.draw()
    }
}

//不规则四边形
func makeTapedzoid() -> Shape{
    let t = Triangle(size: 3)
    let s = Square(size: 3)
    let f = FlippedShape(shape: t)
    
    return JoindShape(top:t,bottom: JoindShape(top: s, bottom: f))
}
print("不规则四边形")
let tapezoid = makeTapedzoid()
print(tapezoid.draw())

返回结果不变。

那么 如果返回得类型不是固定得。假如要返回Square类型 但是我们仍旧声名为Shape


//不规则四边形
func makeTapedzoid(_ size: Int) ->  Shape{
    let t = Triangle(size:size)
    let s = Square(size: size)
    let f = FlippedShape(shape: t)
    if size > 3 {
        return f
    }


    return JoindShape(top:t,bottom: JoindShape(top: s, bottom: f))
}
print("不规则四边形")
let tapezoid = makeTapedzoid(3)
print(tapezoid.draw())
print("不规则四边形1")
let tapezoid2 = makeTapedzoid(4)
print(tapezoid2.draw())

他就有更多得可能。返回父类是shape得类

加上some 后

代码就会报错

swift some 不透明类型 不规则图形_第1张图片

这里得some也指的是只能返回一样得返回 类型。。。。

//不规则四边形
func makeTapedzoid(_ size: Int) -> some Shape{
    let t = Triangle(size:size)
    let s = Square(size: size)
    let f = FlippedShape(shape: t)


    return JoindShape(top:t,bottom: JoindShape(top: s, bottom: f))
}

那为啥设计的时候不叫only呢。。,, ,

英语不好的我不懂为啥

这个api显得好积累啊。。为了规范返回值?

其实还有一种用处

协议有自己的泛型关键字associatedtype 而不是<>

下面代码会报错。因为


protocol Container{
    associatedtype Item
    var count:Int {get}
    subscript(index:Int)->Item {get}
}
extension Array:Container{}
func makeProtocolContainer(item: T)->  Container{

    return [item]
}

协议“容器”只能用作通用约束,因为它具有“自身”或关联的类型要求

 some 就成了 刚需


protocol Container{
    associatedtype Item
    var count:Int {get}
    subscript(index:Int)->Item {get}
}
extension Array:Container{}
func makeProtocolContainer(item: T)-> some Container{

    return [item]
}

这样就不报错了

你可能感兴趣的:(#,swift)