English 中文(简体)
empty empty
原标题:Init Swift Class with empty object
  • 时间:2023-08-13 19:49:14
  •  标签:
  • swift

我对快速和快速倡议非常新。 我试图把另一物体作为参数的舱位。 这里指阶级。

import Foundation
import SwiftUI
import SwiftData
//
//
@Model final class AF_PenName {
    var dateCreated: Date
    var dateUpdated: Date
    var id: UUID
    var name: String
    var myBook: Book?
    
    init(dateCreated: Date, dateUpdated: Date, id: UUID, name: String, myBook: Book?) {
        self.dateCreated = .now
        self.dateUpdated = .now
        self.id = id
        self.name = name
        self.myBook = myBook
    }    
}

在这里,我是如何试图把它 in。

 private func CreateFirstName()
    {
         let penName = AF_PenName(dateCreated: .now, dateUpdated: .now, id: UUID(), name: "Pen Name", myBook: Book)
        
        modelContext.insert(penName)        
    }

它给我留下我的错误: 这本书是因为我不给它一个实际的书目,但在本阶段,没有图书标语,因此,我不敢肯定如何把AF_PenName的标语混入?

问题回答

There are two edge cases in your code.

  • Regardless of the parameters dateCreated and dateUpdated in the init method both dates are always set to the current date. If this is intended omit the parameters, if not set self.dateCreated to dateCreated and self.dateUpdated to dateUpdated
  • The value of the parameter myBook must be a Book instance (or nil) but not the type.

我的建议是增加<条码>init中的缺省值。 方法

@Model final class AF_PenName {
    var dateCreated: Date
    var dateUpdated: Date
    var id: UUID
    var name: String
    var myBook: Book?
    
    init(dateCreated: Date = .now, 
         dateUpdated: Date = .now, 
         id: UUID = UUID(), 
         name: String, 
         myBook: Book? = nil) {

           self.dateCreated = dateCreated
           self.dateUpdated = dateUpdated
           self.id = id
           self.name = name
           self.myBook = myBook
    }    
}

之后,请从下几封信开始的指定职能——可减少至

private func createFirstName() {
    let penName = AF_PenName(name: "Pen Name")        
    modelContext.insert(penName)        
}

由于in s myBook,参数可以接受选用,请通过nil<>>。 你们应该做的是:

 private func CreateFirstName() {
     let penName = AF_PenName(dateCreated: .now, dateUpdated: .now, id: UUID(), name: "Pen Name", myBook: nil)
    
    modelContext.insert(penName)        
}

That being said, the way your init function is written could be cleaned up a bit. You pass many parameters, but you don t actually use them. Consider using the following:

init(id: UUID, name: String, myBook: Book?) {
    self.dateCreated = .now
    self.dateUpdated = .now
    self.id = id
    self.name = name
    self.myBook = myBook
}

已通过的<代码> 日期/代码>和<代码> 日期/代码>的数值从未使用过,这些变量将始终定在<代码>上。

Edit:我想支持使用缺省值清理权的方法。 视您今后如何开始使用<代码>init的功能而定,他的做法更为灵活。 此外,还加入了命名公约。 (此外,如果你想读一下关于 Apple果如何想看你的代码的一些bor子,> 这里的 pe。)

问题如下:

let penName = AF_PenName(dateCreated: .now, dateUpdated: .now, id: UUID(), name: "Pen Name", myBook: Book)

myBook: Book which passes the type. But you have to pass the object, and for that you need to initialize it like this: myBook: Book() which is a default initializer for any new object.

有些人建议采用更清洁的法典,即冰,有些人建议穿甲,那类工作,但理解宣布某种类型和初始化之间的区别,将有助于你今后。





相关问题
SwiftUI: How do I position a button x points above a sheet?

I am trying to recreate a Maps-style UI with a persistent bottom sheet and controls fixed above. Apple Maps UI with controls above ☝️ Example here, the "Look Around" binoculars button and ...

BraintreeDropIn deprecated functions error

I m getting this error on the latest release of BraintreeDropIn, I really don t get it since based on the patch notes, this should ve been addressed already, I have already tried cleaning derived data,...

how to limit zoom level in map swift ui?

how to limit zoom level in map swift ui i have map and need to make zoom level limited Map(coordinateRegion: $region,annotationItems: viewModel.nearbyGyms) { item in ...

UIImageView - How to get the file name of the image assigned?

Is it possible to read the name of an UIImageView s UIImage that s presently stored in the UIImageView? I was hoping you could do something kind of like this, but haven t figured it out. NSString *...