GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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      

Hacking with Swift is sponsored by try! Swift Tokyo.

SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!

Get your ticket here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.