其他分享
首页 > 其他分享> > SwiftUI ViewModel error All In One

SwiftUI ViewModel error All In One

作者:互联网

SwiftUI ViewModel error All In One

Cannot convert value of type 'Binding<Subject>' to expected argument type 'Optional<(IndexSet) -> Void>'

error ❌

//
//  ListViewModel.swift
//  TodoList
//
//  Created by xgqfrms on 2022/5/20.
//

import Foundation

// ViewModel
class ListViewModel: ObservableObject {
  @Published var items: [ListModel] = [];
  init() {
    getItems();
  }
  func getItems() {
    let listData: [ListModel] = ListData;
    items.append(contentsOf: listData);
  }
  // 封装 utils function
  func deteleItem (index: IndexSet) {
    items.remove(atOffsets: index);
  }
  func moveItem (fromIndex: IndexSet, toIndex: Int) {
    items.move(fromOffsets: fromIndex, toOffset: toIndex);
  }
}

solution ✅

typo, change deteleItem to deleteItem

//
//  ListViewModel.swift
//  TodoList
//
//  Created by xgqfrms on 2022/5/20.
//

import Foundation

// ViewModel
class ListViewModel: ObservableObject {
  @Published var items: [ListModel] = [];
  init() {
    getItems();
  }
  func getItems() {
    let listData: [ListModel] = ListData;
    items.append(contentsOf: listData);
  }
  // 封装 utils function
  func deleteItem (index: IndexSet) {
    items.remove(atOffsets: index);
  }
  func moveItem (fromIndex: IndexSet, toIndex: Int) {
    items.move(fromOffsets: fromIndex, toOffset: toIndex);
  }
}

EmptyView

This rather confusing error message usually happens because you created a container but forgot to include some content inside it – you were probably just experimenting, and wrote code like this:

    VStack {
       // nothing ❌
    }

If you just want a placeholder while you’re working on something else, use an EmptyView to keep the compiler happy, like this:

    VStack {
       // ✅
       EmptyView()
    }

https://www.hackingwithswift.com/quick-start/swiftui/how-to-fix-cannot-convert-value-of-type-to-expected-argument-type

refs


Flag Counter

©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载

标签:index,error,listData,items,ViewModel,fromIndex,SwiftUI,func,xgqfrms
来源: https://www.cnblogs.com/xgqfrms/p/16296059.html