I am learning SwiftUI.
Error:
SwiftUI/Environment+Objects.swift:32: Fatal error: No Observable object of type ViewModel found. A View.environmentObject(_:) for ViewModel may be missing as an ancestor of this view.
I do not get the point why this happens.
The error is shown in the line : "renderer.render {...
import SwiftData
import SwiftUI
@Observable
class ViewModel {
var groupNumber: String = "Gruppe"
var sumOfHours: Int = 0
var personsToPrint: [Person]? = nil
@MainActor func pdfReport(person: [Person]) {
if personsToPrint != nil {
for person in personsToPrint! {
let renderer = ImageRenderer(content: PersonDetailView(person: person))
let url = URL.documentsDirectory.appending(path: "\(person.lastname) \(person.firstname).pdf")
print(person.lastname, person.firstname)
renderer.render { size, context in
var box = CGRect(x: 0, y: 0, width: size.width, height: size.height)
guard let pdf = CGContext(url as CFURL, mediaBox: &box, nil) else {
return
}
pdf.beginPDFPage(nil)
context(pdf)
pdf.endPDFPage()
pdf.closePDF()
let data = try! Data(contentsOf: url) //pdf muss erst in Daten umgewandelt werden.
do {
try data.write(to: url)
print("Daten wurden hier gespeichert: \(url)")
} catch {
print("PDF could not be saved!")
}
}
}
}
}
}
Post
Replies
Boosts
Views
Activity
I would like to write an App, to store sensitive data.
Therefore I want to make sure, that the data is only saved and used on the local device.
How can I save/use data of an app only local even if I am using iCloud service generally?
Hello,
I would like to create an app, which helps people to learn a language. The app content I would like to change using text files.
As beginner in SWIFT programming I would need a hint how to pass new content in my app.
I thought to use text files, placed on a web site. If the text file is updated, the app should get the newest content automatically.
Does this make sense?
What would be the best solution to achieve this target?
Is there any helpful video in the "Developer" app or somewhere else?
Thanks a lot.
There are several posts already. But none helps me in my task. Sometimes error message appears:
"The file "Text.strings" couldn't be opened using text encoding Unicode (UTF-8)." Sometimes the text is shown in strange format.
Why?
import Foundation
func printLine() -> String {
let filename = "Text"
var text: String
var myCounter: Int
guard let file = Bundle.main.url(forResource: filename, withExtension: "strings")
else {
fatalError("Couldn't find \(filename) in main bundle.")
}
do {
let contents = try String(contentsOf: file, encoding: String.Encoding.utf8 )
let lines = contents.split(separator:"\n")
print(contents)
print(lines)
myCounter = lines.count
print(myCounter)
text = String(myCounter)
} catch {
return (error.localizedDescription)
}
return text
}
This is the content of the text file:
"Text 1";
"Text 2";
"Text 3";
I learn how to code.
The guided project "Personality Quiz" of the book "App development with swift" ios11 Edition Xcode 9 troubles me.
Those lines show no effect in the simulator:
singleStackView.isHidden = true
multipleStackView.isHidden = true
rangedStackView.isHidden = true
Why?