无法转换类型' String'的返回表达式返回类型' String'

时间:2017-07-20 17:33:24

标签: swift protocols

我遇到了希望通过使用CustomStringConvertible协议来扩展简单类的情况。但是,当我执行API指示我做的事情时,编译器会遇到一系列错误。

这是代码的一部分......

struct Dog<String> {
    typealias Element = String
    var dogName: String
}

extension Dog: ExpressibleByArrayLiteral {
    init(arrayLiteral: Element...) {
        let startIndex = arrayLiteral.startIndex
        let stringName = arrayLiteral[startIndex]
        self.init(dogName: stringName)
    } //end of init(arrayLiteral: Element...) for extension Domino
}


var d: Dog = ["Roofus", "Tony", "Rover"]
print("The dog's name is \(d.dogName)")
var d2: Dog = [1]
print("The dog's name is \(d2.dogName)")
var d3: Dog = [true]
print("The dog's name is \(d3.dogName)")

以上所有内容都可以很好地编辑,而且打印语句可以满足您的期望。但是,当我添加CustomStringConvertible扩展名时,就像这样......

extension Dog: CustomStringConvertible {
    var description: String {
        get {
            return "testing with a constant string"
        }
    }
}

我收到以下错误:

error: type 'Dog<String>' does not conform to protocol 'CustomStringConvertible'
extension Dog: CustomStringConvertible {
^
Swift.CustomStringConvertible:67:16: note: protocol requires property 'description' with type 'String'; do you want to add a stub?
    public var description: String { get }
               ^
.../temp.swift:23:9: note: candidate has non-matching type 'String'
    var description: String {
        ^
.../temp.swift:25:20: error: cannot convert return expression of type 'String' to return type 'String'
            return "testing with a constant string"
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                    as! String
[Finished in 0.1s with exit code 1]

请详细说明我为何从编译器获得以下响应:cannot convert return expression of type 'String' to return type 'String'

修改

注意:当我将原始String表达式更改为实际的String值时,我得到类似的编译器反馈。也就是说,以下......

extension Dog: CustomStringConvertible {
    var description: String {
        get {
            let s: String = "testing with a constant string"
            return s
        }
    }
}

......只是改变反馈意见......

.../temp.swift:25:20: error: cannot convert return expression of type 'String' 
to return type 'String'
            return "testing with a constant string"
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                    as! String

.../temp.swift:25:20: error: cannot convert value of type 'String' 
to specified type 'String'
            return "testing with a constant string"
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                    as! String

修改

更改了这个问题的标题,以更好地反映我真正的难题。

1 个答案:

答案 0 :(得分:0)

查看NobodyNadaAlexander的评论,我发现了它。

由于某种原因,我认为我必须使Dog的通用类型等于typealias所分配的Element的类型...我是我必须这样做才能让ExpressibleByArrayLiteral适用于Dog类型。

......好吧,我没有。

事实上,人们甚至不需要泛型来使ExpressibleByArrayLiteral起作用(尽管我确信有一些方便的方法可以融合泛型和ExpressibleByArrayLiteral协议)。

那就是说,我修改后的代码工作正常。具体地,

struct Dog {
    typealias Element = String
    var dogName: String
}

extension Dog: ExpressibleByArrayLiteral {
    init(arrayLiteral: Element...) {
        let startIndex = arrayLiteral.startIndex
        let stringName = arrayLiteral[startIndex]
        self.init(dogName: stringName)
    } //end of init(arrayLiteral: Element...) for extension Dog
}


var d: Dog = ["Roofus", "Tony", "Rover"]
print("The dog's name is \(d.dogName)")
var d2: Dog = ["1"]
print("The dog's name is \(d2.dogName)")
var d3: Dog = ["true"]
print("The dog's name is \(d3.dogName)")

extension Dog: CustomStringConvertible {
    var description: String {
        let s: String = "testing with a constant string"
        return s
    }
}

var d4: Dog = ["x"]
print("\(d4)")

的输出为:

The dog's name is Roofus
The dog's name is 1
The dog's name is true
testing with a constant string
[Finished in 0.1s]