SwiftUI-错误 - 初始化器' init(_:RowContent:)'要求' customdatamodel'符合识别性'
在我的手表应用程序中,我希望显示事件列表。打开视图后,我将请求发送到iPhone应用程序以发送事件列表。到目前为止,这已经有效,我正在将活动列表返回观看。但是,当我尝试在列表中显示数据时,由于自定义对象类型,我会遇到错误。我想要的是在可用数据时显示事件列表,或者在没有事件时在视图上显示文本。
注意:观看与设备/OS之间传输数据的连接性不允许自定义数据类型,因此必须符合编码并在传输之前将数据从CustomDatamodel转换为数据类型,然后在我的手表项目中从数据转换为customDatamodel。
下面是我看到的代码和错误:
customdatamodel
import Foundation
struct CustomDataModel: Codable {
var startEndDate: String?
var startEndTime: String?
var locationDetails: String?
var actions: Dictionary<String, String> = [:]
var title: String?
// Has more properties
func encodeIt() -> Data {
let data = try! PropertyListEncoder.init().encode(self)
return data
}
static func decodeIt(_ data:Data) -> CustomDataModel {
let customDataModel = try! PropertyListDecoder.init().decode(CustomDataModel.self, from: data)
return customDataModel
}
}
customviewModel
import Foundation
import Combine
class CustomViewModel: ObservableObject, SessionCommands {
@Published var eventsList:[CustomDataModel] = []
init() {
NotificationCenter.default.addObserver(forName: Notification.Name("eventsListReady"), object: nil, queue: nil, using: self.processEventsList(_:))
}
@objc func processEventsList(_ notification: Notification) {
if let response = (notification.object as! CommandStatus).response as? [Data] {
response.forEach { event in
eventsList.append(CustomDataModel.decodeIt(event))
}
}
}
func getEventsList() {
self.eventsList = []
sendMessage(["functionality": Functionality.events.rawValue, "notificationName": Notification.Name("requestEventsList")])
}
}
customView
import SwiftUI
struct CustomView: View {
@ObservedObject var viewModel: CustomViewModel
var body: some View {
VStack {
if !viewModel.eventsList.isEmpty {
List(viewModel.eventsList) { item in
let _ = print("##### !viewModel.eventsList.isEmpty")
NavigationLink(destination: EventsDetailsView(eventItem: item)) {
EventRowView(eventItem: item)
}.navigationViewStyle(StackNavigationViewStyle())
}
} else {
let _ = print("##### viewModel.eventsList.isEmpty")
VStack {
Text("No events to display")
}.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}.navigationBarTitle("Back").onAppear() {
self.viewModel.getEventsList()
}
}
}
错误我在“ customView: initializer'init(_:rowcontent :)'要求“ customDatamodel”符合“可识别”
我在customViewModel中的EventList对象中获取数据,但无法显示它。
In my watch app, I wish to display list of events. When the view is opened, I send a request to iPhone app to send the events list. This is working so far and I am getting the events list back to Watch. However when I try to display the data on the list, I am getting errors due to custom object type. What I want is to display the events list when the data is available or just show a text on the view when there are no events.
Note: Watch connectivity to transfer data between devices/OS's does not allow custom data types, so have to conform to Codable and convert the data from CustomDataModel to Data type before transferring and then convert from Data to CustomDataModel in my Watch project.
Below is the code and error that I see:
CustomDataModel
import Foundation
struct CustomDataModel: Codable {
var startEndDate: String?
var startEndTime: String?
var locationDetails: String?
var actions: Dictionary<String, String> = [:]
var title: String?
// Has more properties
func encodeIt() -> Data {
let data = try! PropertyListEncoder.init().encode(self)
return data
}
static func decodeIt(_ data:Data) -> CustomDataModel {
let customDataModel = try! PropertyListDecoder.init().decode(CustomDataModel.self, from: data)
return customDataModel
}
}
CustomViewModel
import Foundation
import Combine
class CustomViewModel: ObservableObject, SessionCommands {
@Published var eventsList:[CustomDataModel] = []
init() {
NotificationCenter.default.addObserver(forName: Notification.Name("eventsListReady"), object: nil, queue: nil, using: self.processEventsList(_:))
}
@objc func processEventsList(_ notification: Notification) {
if let response = (notification.object as! CommandStatus).response as? [Data] {
response.forEach { event in
eventsList.append(CustomDataModel.decodeIt(event))
}
}
}
func getEventsList() {
self.eventsList = []
sendMessage(["functionality": Functionality.events.rawValue, "notificationName": Notification.Name("requestEventsList")])
}
}
CustomView
import SwiftUI
struct CustomView: View {
@ObservedObject var viewModel: CustomViewModel
var body: some View {
VStack {
if !viewModel.eventsList.isEmpty {
List(viewModel.eventsList) { item in
let _ = print("##### !viewModel.eventsList.isEmpty")
NavigationLink(destination: EventsDetailsView(eventItem: item)) {
EventRowView(eventItem: item)
}.navigationViewStyle(StackNavigationViewStyle())
}
} else {
let _ = print("##### viewModel.eventsList.isEmpty")
VStack {
Text("No events to display")
}.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}.navigationBarTitle("Back").onAppear() {
self.viewModel.getEventsList()
}
}
}
Error I get on "List(viewModel.eventsList)" in CustomView : Initializer 'init(_:rowContent:)' requires that 'CustomDataModel' conform to 'Identifiable'
I am getting data in eventList object in CustomViewModel, but am not able to display it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
给您的类型一个ID并使其可识别,如果您不需要确定,请不要添加它。
使用
Codable
只是添加编码
和可解码
的简写。因此,如果您不需要两者,请使用所需的。Give your type an id and make it identifiable and if you don’t need decidable don’t add it.
Using
Codable
is just shorthand for addingEncodable
andDecodable
. So if you don’t need both, then just use the one you need.