Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift .OnDelete无法使用领域数据,如何修复此问题?_Swift_Swiftui_Realm - Fatal编程技术网

Swift .OnDelete无法使用领域数据,如何修复此问题?

Swift .OnDelete无法使用领域数据,如何修复此问题?,swift,swiftui,realm,Swift,Swiftui,Realm,当我运行我的应用程序并尝试刷卡时,onDelete不会出现,也不会工作。我还没有机会真正测试它是否删除,因为当我刷卡时,它不允许我尝试删除它。我正在使用RealmSwift,并发布了视图和我使用的ViewModel的代码。抱歉,如果代码不够,请告诉我,我将链接我的GitHub repo,或共享更多代码 import SwiftUI import RealmSwift import Combine enum ActiveAlert{ case error, noSauce } stru

当我运行我的应用程序并尝试刷卡时,onDelete不会出现,也不会工作。我还没有机会真正测试它是否删除,因为当我刷卡时,它不允许我尝试删除它。我正在使用RealmSwift,并发布了视图和我使用的ViewModel的代码。抱歉,如果代码不够,请告诉我,我将链接我的GitHub repo,或共享更多代码

import SwiftUI
import RealmSwift
import Combine

enum ActiveAlert{
    case error, noSauce
}

struct DoujinView: View {
    @ObservedObject var doujin: DoujinAPI
    //    @ObservedResults(DoujinInfo.self) var doujinshis
    @State private var detailViewShowing: Bool = false
    @State private var selectedDoujin: DoujinInfo?
    
    
    @StateObject var doujinModel = DoujinInfoViewModel()
    var body: some View {
        //Code if there are any Doujins
        ScrollView(.vertical) {
            LazyVStack(spacing: 0) {
                ForEach(doujinModel.doujins, id: \.UniqueID) { doujinshi in
                    Button(action: {
                        self.detailViewShowing = true
                        self.doujinModel.selectedDoujin = doujinshi
                        
                    }) {
                        DoujinCell(image: convertBase64ToImage(doujinshi.PictureString))
                    }
                }
                
                
                .onDelete(perform: { indexSet in
                                        self.doujinModel.easyDelete(at: indexSet)
                })
                
                
                //This will preseent the sheet that displays information for the doujin
                .sheet(isPresented: $detailViewShowing, onDismiss: {if doujinModel.deleting == true {doujinModel.deleteDoujin()}}, content: {
                    DoujinInformation(theAPI: doujin, doujinModel: doujinModel)
                })
                
                //                Loading circle
                if doujin.loadingCircle == true{
                    LoadingCircle(theApi: doujin)
                }
                
            }
            
        }
    }
}
enum colorSquare:可识别{
变量id:Int{
哈希值
}
果岭
壳黄
壳红
}
类DoujinInfoViewModel:ObserveObject{
var theDoujin:doujinfo?=nil
var领域:领域?
var标记:NotificationToken?=nil
@ObservedResults(doujinfo.self)var doujins
@已发布的变量删除:Bool=false
@已发布变量selectedDoujin:DoujinInfo?=nil
@已发布的变量加载:Bool=false
init(){
让realm=try?realm()
self.realm=领域
token=doujins。观察({(更改)中的
开关变化{
case.error(u):中断
案例。首字母(u):break
case.update(u,删除:u,插入:u,修改:u):
self.objectWillChange.send()}
})
}
脱硝{
令牌?.invalidate()
}
变量名称:String{
得到{
已选择Doujin!.Name
}
}
变量id:String{
得到{
已选择Doujin!.Id
}
}
变量mediaID:String{
得到{
已选择Doujin!.MediaID
}
}
变量numPages:Int{
得到{
已选择Doujin!.NumPages
}
}
var pictureString:字符串{
得到{
已选择Doujin
}
}
var uniqueId:String{
得到{
已选择Doujin
}
}
var相似性:双重{
得到{
选择Doujin!。相似性
}
}
颜色:colorSquare{
得到{
选择的开关为Doujin{
案例0…50:
返回,红色
案例50…75:
返回,黄色
案例75…100:
返回,绿色
违约:
返回,绿色
}
}
}
var Dougs:列表{
得到{
已选择Doujin!。标签
}
}
func deleteDoujin(){
尝试?领域?写作{
领域?.delete(选择Doujin!)
}
删除=假
}
func easyDelete(在索引集:索引集处){
如果let index=indexSet.first{
让realm=doujins[indexSet.first!].realm
尝试?领域?写作({
realm?.delete(doujins[indexSet.first!))
})
}
}
func addDoujin(doujin:doujin信息){
尝试?领域?写作({
领域?.add(theDoujin)
})
}
}

.onDelete
仅适用于
列表
。对于
LazyVStack
我们需要创建自己的刷卡删除操作

这是示例演示。您可以根据需要对其进行修改

SwipeDeleteRow视图

struct SwipeDeleteRow<Content: View>: View {
    
    private let content: () -> Content
    private let deleteAction: () -> ()
    private var isSelected: Bool
    @Binding private var selectedIndex: Int
    private var index: Int
    
    init(isSelected: Bool, selectedIndex: Binding<Int>, index: Int, @ViewBuilder content: @escaping () -> Content, onDelete: @escaping () -> Void) {
        self.isSelected = isSelected
        self._selectedIndex = selectedIndex
        self.content = content
        self.deleteAction = onDelete
        self.index = index
    }
    
    @State private var offset = CGSize.zero
    @State private var offsetY : CGFloat = 0
    @State private var scale : CGFloat = 0.5
    
    var body : some View {
        HStack(spacing: 0){
            content()
                .frame(width : UIScreen.main.bounds.width, alignment: .leading)
            
            Button(action: deleteAction) {
                Image("delete")
                    .renderingMode(.original)
                    .scaleEffect(scale)
            }
        }
        .background(Color.white)
        .offset(x: 20, y: 0)
        .offset(isSelected ? self.offset : .zero)
        .animation(.spring())
        .gesture(DragGesture(minimumDistance: 30, coordinateSpace: .local)
                    .onChanged { gestrue in
                        self.offset.width = gestrue.translation.width
                        print(offset)
                    }
                    .onEnded { _ in
                        self.selectedIndex = index
                        if self.offset.width < -50 {
                            self.scale = 1
                            self.offset.width = -60
                            self.offsetY = -20
                        } else {
                            self.scale = 0.5
                            self.offset = .zero
                            self.offsetY = 0
                            
                        }
                    }
        )
    }
}
辅助函数

//Help to preventing delete row from index out of bounds.
extension Collection where Indices.Iterator.Element == Index {
    subscript (safe index: Index) -> Iterator.Element? {
        return indices.contains(index) ? self[index] : nil
    }
}

.onDelete work with List no for LazyVStack。LazyVStack/vstack是否有类似的功能?但是,我发现了这个错误。我正在尝试允许用户随意添加单元格。您可以添加ForEach代码吗?只需在这行“DoujinCell(image:convertBase64ToImage(item.PictureString))”中添加一条注释,然后添加普通文本并检查doujins是否为数组?
struct Model: Identifiable {
    var id = UUID()
}

struct CustomSwipeDemo: View {
    
    @State var arr: [Model] = [.init(), .init(), .init(), .init(), .init(), .init(), .init(), .init()]
    
    @State private var listCellIndex: Int = 0
    
    var body: some View {
        ScrollView(.vertical) {
            LazyVStack(spacing: 0) {
                ForEach(arr.indices, id: \.self) { index in
                    SwipeDeleteRow(isSelected: index == listCellIndex, selectedIndex: $listCellIndex, index: index) {
                        if let item = self.arr[safe: index] {
                            Text(item.id.description)
                        }
                    } onDelete: {
                        arr.remove(at: index)
                        self.listCellIndex = -1
                    }
                    .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
                }
            }
        }
    }
}
//Help to preventing delete row from index out of bounds.
extension Collection where Indices.Iterator.Element == Index {
    subscript (safe index: Index) -> Iterator.Element? {
        return indices.contains(index) ? self[index] : nil
    }
}