Reputation: 21
i'm trying to run a function inside a body. i'm not familiar how functions work in swiftUI. here's the code below.
struct HomeView: View {
func getDirectory() -> String{
let fm = FileManager.default
let path = Bundle.main.resourcePath!
do {
let items = try fm.contentsOfDirectory(atPath: path)
for item in items {
print("Found \(item)")
}
} catch {
// failed to read directory – bad permissions, perhaps?
}
return path
}
let documentURL = Bundle.main.url(forResource: resourceURL, withExtension: "pdf")!
var body: some View {
let path = getDirectory()
print(path)
}
}
i'm getting an error saying.
''Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type''
how can i make this work?
Upvotes: 2
Views: 2067
Reputation: 181
var body: some View {
let noView: EmptyView = {
/*
Do what you want here
*/
return EmptyView()
}()
noView
}
Upvotes: 4
Reputation: 4245
I can't tell what you're trying to get from this document, but to answer your question directly, you need to provide a View within the body, not a String. You can add a Text that accepts a String parameter:
var body: some View {
Text(getDirectory())
}
Upvotes: 1