英文:
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<T: Codable>(type: T.Type, from urlString: String) async throws -> T
}
扩展 GenericApi {
func fetch<T: Codable>(type: T.Type, from urlString: String) async throws -> 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..<300 ~= response.statusCode {
do {
return try decoder.decode(type, from: data)
} catch {
print(error.localizedDescription)
}
}
fatalError("HTTP status code: \(response.statusCode)")
}
}
这是我的协议。
我使用了泛型函数和不透明类型来创建这个协议,然后当我实现这段代码时,我遇到了错误:
> "无法将类型为 '\[Сategory\]' 的值分配给类型为 '\[Category\]'
> (也就是 'Array\<OpaquePointer\>')"
如何将一个结果分配给另一个?
// 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 = "image_url"
}
}
我的模型。
@Published private(set) var categories: [Category] = []
func fetchCategories() async {
do {
let response = try await networkManager.fetch(type: Categories.self,
from: "https://run.mocky.io/v3/058729bd-1402-4578-88de-265481fd7d54")
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<T: Codable>(type: T.Type, from urlString: String) async throws -> T }
}
extension GenericApi {
func fetch<T: Codable>(type: T.Type, from urlString: String) async throws -> 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..<300 ~= response.statusCode {
do {
return try decoder.decode(type, from: data)
} catch {
print(error.localizedDescription)
}
}
fatalError("HTTP status code: \(response.statusCode)")
}
}
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<OpaquePointer>')"
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 = "image_url"
}
}
My model.
@Published private(set) var categories: [Category] = []
func fetchCategories() async {
do {
let response = try await networkManager.fetch(type: Categories.self,
from: "https://run.mocky.io/v3/058729bd-1402-4578-88de-265481fd7d54")
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论