UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 55 Problems with default picker value

Forums > 100 Days of SwiftUI

Hi, when creating a new book in the Bookworm project, if i don't touch the genre picker and let it go with the default value (Fantasy), when i go to check the detail view, it doesn't appear (neither image or text). But if i click the picker, choose another value and then click "Fantasy" it appears.

My DetailView code:

import CoreData
import SwiftUI

struct DetailView: View {

    let book: Book

    var body: some View {
        ScrollView {
            ZStack(alignment: .bottomTrailing) {
                Image(book.genre ?? "Fantasy")
                    .resizable()
                    .scaledToFit()

                Text(book.genre?.uppercased() ?? "FANTASY")
                    .font (.caption)
                    .fontWeight(.black)
                    .padding(8)
                    .foregroundColor(.white)
                    .background(.black.opacity(0.75))
                    .clipShape(Capsule())
                    .offset(x: -5, y: -5)
            }

            Text(book.author ?? "Unknown author")
                .font(.title)
                .foregroundColor(.secondary)

            Text(book.review ?? "No review")
                .padding()

            RatingView(rating: .constant(Int(book.rating)))
                .font(.largeTitle)
        }
        .navigationTitle(book.title ?? "Unknown Book")
        .navigationBarTitleDisplayMode(.inline)
    }
}

struct DetailView_Previews: PreviewProvider {

    static let moc = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)

    static var previews: some View {

        let book = Book(context: moc)
        book.title = "Test book"
        book.author = "Test author"
        book.genre = "Fantasy"
        book.rating = 4
        book.review = "This was a great book; I really enjoyed it."

        return NavigationView {
            DetailView(book: book)
        }
    }
}

2      

Hi. This is ok. Because later in the course you will add validation and your "Save" button won't be active until all fields are filled. If you look at your AddBookView you can see that you genre is not nil when you create it but an empty string i.e. "". So when you create your book empty string is assinged to your book genre NOT nil. And when you go to DetailView as there are no genre with empty string and no image name with empty string it cannot find anything so it doesn't show those pieces. But once you add validation to your save button later in the project you won't be able to save it unless all data is filled.

2      

To make it more clear take a look here

Image(book.genre ?? "Fantasy")
    .resizable()
    .scaledToFit()

Text(book.genre?.uppercased() ?? "FANTASY")
    .font (.caption)
    .fontWeight(.black)
    .padding(8)
    .foregroundColor(.white)
    .background(.black.opacity(0.75))
    .clipShape(Capsule())
    .offset(x: -5, y: -5)

Here it says if book.genre is nil give me default "Fantasy" image and if my book.genre is nil give me "FANTASY".

But in AddBookView in the project we declare it as

@State private var genre = ""

and unless we choose it from the picker or set genre to contain default value but not an empty string it will still be the empty string. And will be saved as empty string to core data.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.