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



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 發佈文章使用:只允許註冊用戶纔可以訪問!

原創文章,版權所有©️xgqfrms, 禁止轉載 🈲️,侵權必究⚠️!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章