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

Day 77 Challenge help

Forums > 100 Days of SwiftUI

Hi. Im trying to complete day 77 challenge and maybe you could give some hints? I only have this code for now which .alert dosnt work. What do you think i could do next? This challenge is a bit overwhelming for me. :)

import SwiftData
import PhotosUI
import SwiftUI

struct ContentView: View {

    @State private var selectedItem: PhotosPickerItem?
    @State private var imageName: String = ""
    @State private var isNamingPhoto: Bool = false
    @State private var importedImage: Image?

    var body: some View {
        NavigationStack {
            VStack {

                        PhotosPicker(selection: $selectedItem) {

                            ContentUnavailableView("No picture", systemImage: "photo.badge.plus", description: Text("Tap to import a photo"))

                        .alert(isPresented: $isNamingPhoto, content: {
                            Alert(title: Text("Name the photo"), message: Text("Enter the name for the imported photo"), primaryButton: .default(Text("Save"), action: {
                                // Handle saving the image name here
                                print("Image Name: \(imageName)")
                            }), secondaryButton: .cancel())

                    .navigationTitle("Name that image").font(.subheadline)


 #Preview {


You are using an old API for .alert. Try this one

.alert("Name the photo", isPresented: $isNamingPhoto) {
    TextField("Enter Image Name", text: $imageName) // <- To enter name of photo
    Button("Done") {
        // Handle saving the image name here
        print("Image Name: \(imageName)")
    .disabled(imageName.isEmpty) // <- disable the Done button until something in text field

    Button("Cancel", role: .cancel) { }
} message: {
    Text("Enter the name for the imported photo")

I am assume that you have a method to change the PhotosPickerItem to the importedImage when that no longer nil change the isNamingPhoto to true which will show the alert.

Remember as Paul say

Note: Don’t worry if you don’t complete challenges in the day they were assigned – in future days you’ll find you have some time to spare here and there, so challenges are something you can return back to in the future.


Thanks for the .alert update :) its started to show an alert!

I have a method to load image but im not sure its final :) What do you think about it?

func loadImage() {
        Task {
            guard let imageData = try await selectedItem?.loadTransferable(type: Data.self) else { return }
            guard let importedImage = UIImage(data: imageData) else { return }

            isNamingPhoto = true



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.