How to solve the issue "Cannot assign value of type '[Сategory]' to type '[Category]' (aka 'Array<OpaquePointer>')"

huangapple go评论56阅读模式
英文:

How to solve the issue "Cannot assign value of type '[Сategory]' to type '[Category]' (aka 'Array<OpaquePointer>')"

问题

Here's the translated code:

协议 GenericApi {
    var session: URLSession { get }
    var decoder: JSONDecoder { get }
    func fetch&lt;T: Codable&gt;(type: T.Type, from urlString: String) async throws -&gt; T
}

扩展 GenericApi {
    func fetch&lt;T: Codable&gt;(type: T.Type, from urlString: String) async throws -&gt; T {
        guard let url = URL(string: urlString) else { throw URLError(.badURL) }

        let (data, response) = try await session.data(from: url)

        guard let response = response as? HTTPURLResponse else { throw URLError(.badServerResponse) }

        if 200..&lt;300 ~= response.statusCode {
            do {
                return try decoder.decode(type, from: data)
            } catch {
                print(error.localizedDescription)
            }
        }
        fatalError(&quot;HTTP status code: \(response.statusCode)&quot;)
    }
}

这是我的协议。

我使用了泛型函数和不透明类型来创建这个协议,然后当我实现这段代码时,我遇到了错误:

&gt; &quot;无法将类型为 &#39;\[Сategory\]&#39; 的值分配给类型为 &#39;\[Category\]&#39;
&gt; (也就是 &#39;Array\&lt;OpaquePointer\&gt;&#39;)&quot;

如何将一个结果分配给另一个?

 // MARK: - Categories
 结构体 Categories: Codable {
    let results: [Сategory]
 }

// MARK: - Сategory
 结构体 Сategory: Codable, Identifiable {
    let id: Int
    let name: String
    let imageURL: String

    枚举 CodingKeys: String, CodingKey {
        case id, name
        case imageURL = &quot;image_url&quot;
    }
 }

我的模型。

 @Published private(set) var categories: [Category] = []
 func fetchCategories() async {
    do {
        let response = try await networkManager.fetch(type: Categories.self,
                                       from: &quot;https://run.mocky.io/v3/058729bd-1402-4578-88de-265481fd7d54&quot;)
        categories = response.results
    } catch {
        print(error.localizedDescription)
    }
 }

Please note that the code contains placeholders like Сategory and Category that appear to be related to the error you mentioned. You may need to ensure that the types used in your protocol and implementation match correctly.

英文:
protocol GenericApi {
var session: URLSession { get }
var decoder: JSONDecoder { get }
func fetch&lt;T: Codable&gt;(type: T.Type, from urlString: String) async throws -&gt; T }
}
extension GenericApi {
func fetch&lt;T: Codable&gt;(type: T.Type, from urlString: String) async throws -&gt; T {
guard let url = URL(string: urlString) else { throw URLError(.badURL) }
let (data, response) = try await session.data(from: url)
guard let response = response as? HTTPURLResponse else { throw URLError(.badServerResponse) }
if 200..&lt;300 ~= response.statusCode {
do {
return try decoder.decode(type, from: data)
} catch {
print(error.localizedDescription)
}
}
fatalError(&quot;HTTP status code: \(response.statusCode)&quot;)
} 
}    

This is my protocol.

I've created the protocol with generic func and opaque type, and then when I've implemented that code I got the error:

> "Cannot assign value of type '[Сategory]' to type '[Category]'
> (aka 'Array&lt;OpaquePointer&gt;')"

How can I assign one result to another?

 // MARK: - Categories 
struct Categories: Codable {
let results: [Сategory] 
}
// MARK: - Сategory 
struct Сategory: Codable, Identifiable {
let id: Int
let name: String
let imageURL: String
enum CodingKeys: String, CodingKey {
case id, name
case imageURL = &quot;image_url&quot;
}
}

My model.

 @Published private(set) var categories: [Category] = []
func fetchCategories() async {
do {
let response = try await networkManager.fetch(type: Categories.self,
from: &quot;https://run.mocky.io/v3/058729bd-1402-4578-88de-265481fd7d54&quot;)
categories = response.results
catch {
print(error.localizedDescription)
}
}

And the function where I get the error.

答案1

得分: 1

There is a name conflict. The word Category in this line:

@Published private(set) var categories: [Category] = []

actually refers to this Category.

typealias Category = OpaquePointer

This is a type representing Objective-C categories (which is something similar to extensions in Swift). Clearly, that is not what you mean.

This Category typealias is being preferred over the one you declared.

You can stop this from happening by adding your module name as a prefix. Your module name is typically the same as your Xcode target's name.

@Published private(set) var categories: [YourApp.Category] = []

See also this if you are confused about what your module name is.

Or, you can just rename your Category to something else.

英文:

There is a name conflict. The word Category in this line:

@Published private(set) var categories: [Category] = []

actually refers to this Category.

typealias Category = OpaquePointer

This is a type representing Objective-C categories (which is something similar to extensions in Swift). Clearly, that is not what you mean.

This Category typealias is being preferred over the one you declared.

You can stop this from happening by adding your module name as a prefix. Your module name is typically the same as your Xcode target's name.

@Published private(set) var categories: [YourApp.Category] = []

See also this if you are confused about what your module name is.

Or, you can just rename your Category to something else.

huangapple
  • 本文由 发表于 2023年7月6日 20:25:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定