[M1 Mac, Ventura 13.3.1, Xcode 14.3]
macOS版にメモを10行より多く書くとアプリが落ちてしまいました。
行数を無制限にするには以下のようにframeというビュー修飾子で設定します。
import SwiftUI
struct Draft: View {
@State var text = ""
@FocusState var nameFieldIsForcused: Bool
@Environment(\.managedObjectContext)var viewContext
var note: Note?
var body: some View {
TextEditor(text:$text)
.frame(minHeight: 0, maxHeight: .infinity) // 行数無制限
.focused($nameFieldIsForcused)
.onAppear{
DispatchQueue.main.asyncAfter(deadline:DispatchTime.now()+0.5){
nameFieldIsForcused = true
}
}
.toolbar{
ToolbarItem(placement:.navigationBarTrailing){
if(nameFieldIsForcused){
Button(action:{
if note != nil{
updateContent(note: self.note!)
}else{
addContent()
}
}){
Text("完了")
}
}
}
}
}
func updateContent(note:Note){
note.content=text
do{
try viewContext.save()
}catch{
fatalError("セーブに失敗")
}
nameFieldIsForcused = false
}
func addContent(){
let newContent = Note(context:viewContext)
newContent.content = text
do{
try viewContext.save()
}catch{
fatalError("セーブに失敗")
}
nameFieldIsForcused = false
}
}
struct Draft_Previews: PreviewProvider {
static var previews: some View {
Draft()
}
}