代码之家  ›  专栏  ›  技术社区  ›  Jacob M. Barnard

无法将“String”类型的返回表达式转换为返回类型“String”

  •  1
  • Jacob M. Barnard  · 技术社区  · 7 年前

    这是代码中有效的部分。。。

    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'

    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 回复  |  直到 7 年前
        1
  •  1
  •   Jacob M. Barnard    7 年前

    查看的评论 NobodyNada Alexander ,我想出来了。

    Dog typealias 那个 Element 被分配到…我想我必须这样做才能 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]