gpt4 book ai didi

swift - 无法将类型 'String' 的返回表达式转换为返回类型 'String'

转载 作者:行者123 更新时间:2023-11-30 12:17:25 30 4
gpt4 key购买 nike

我遇到了一种情况,我想通过使其采用 CustomStringConvertible 协议(protocol)来扩展一个简单的类。然而,当我按照 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]

请说明为什么我从编译器收到以下响应:无法将类型“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

编辑

更改了这个问题的标题,以更好地反射(reflect)我真正的难题。

最佳答案

NobodyNada的评论和 Alexander ,我明白了。

出于某种原因,我认为我必须使 Dog 的泛型类型等于 Elementtypealias 类型被分配给...,我想我必须这样做才能让 ExpressibleByArrayLiteralDog 类型工作。

...好吧,我不知道。

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

也就是说,我修改后的代码可以正常工作。具体来说,

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]

关于swift - 无法将类型 'String' 的返回表达式转换为返回类型 'String',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45221517/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com