Learn Courses My Dashboard

Issue in a Func of PhotoWidget

Hello Chris,

Thanks for your quick reply.

I am trying to make a func in a Photowidget of indent configuration that can select photo from user library as a timeline entry.

But, it is showing error for doing that.

Here, I am attaching the screenshot for that code.

Please can you guys help me for to solve that issue.

Here is the code.

struct ReInventingMemoriesView : View {
    
    @State var Photo: UIImage?
    let configuration: ReInventingMemoriesIntent
    var iCloudPhotosEnabled:Bool
    var entry: Provider.Entry
    let intent = ReInventingMemoriesIntent()
    let kind: String = "ReInventingMemories"
    
    var body: some View {
        
        func SelectPhoto()  -> {
            
            if (Photo != nil || iCloudPhotosEnabled = .true) {
                ForEach(Photo.PHPhotoLibrary, id:\.self) { Photo  in
                    
             Photo =   INSearchForPhotosIntentIdentifier.random(count(1))
                
                    return Photo
                    }
                    
            }
            
                .clipShape(RoundedRectangle(cornerRadius: 20))
                    .scaleEffect(WidgetSize.scaleFactor)
                    .stroke(lineWidth: 2)
                    .shadow(radius: 5)
                    .overlay {
                        if true {
                            Image(systemName: "My Memories")
                        }
                    }
            }
            
        }
        
    }

Thanks,
Jayesh Patel.

Don’t put the function inside the body, put it outside of it and you can call your function in the body

Hello Mikaela,

Thanks for your answer.
I tried doing that way but, its still showing the errors as follows.
Thanks
Jayesh

struct ReInventingMemoriesView : View {
    
    @State var Photo: UIImage?
    let configuration: ReInventingMemoriesIntent
    var iCloudPhotosEnabled:Bool
    var entry: Provider.Entry
    let intent = ReInventingMemoriesIntent()
    let kind: String = "ReInventingMemories"
    
    
    func SelectPhoto()   {
         
        if (Photo != nil || iCloudPhotosEnabled == .true) {
            
          ForEach(Photo.PHPhotoLibrary, id:\.self) { Photo in
                
           INSearchForPhotosIntentIdentifier.random(count(1))
             
        }
                
        }
    }
    
    var body: some View {
        
        SelectPhoto()
             .clipShape(RoundedRectangle(cornerRadius: 20))
             .scaleEffect(WidgetSize.scaleFactor)
             .stroke(lineWidth: 2)
             .shadow(radius: 5)
             .overlay {
                if true {
                            Image(systemName: "My Memories")
                        }
                    }
            }
            
        
}

When you paste your code in, please also include the import statements above the struct so that we know what Frameworks you are using in your code.

For the purposes of the exercise you should also include any external definitions that are relevant to the code.

Hello Chris,
Thanks for your answer.
Here are the import frameworks.

import WidgetKit

import SwiftUI

import Intents

Here are the some external definitions.

struct Provider: IntentTimelineProvider

**struct** ReInventingMemories: Widget {

**let** kind: String = "ReInventingMemories"
var body: some WidgetConfiguration {
        IntentConfiguration(kind: kind, intent: ReInventingMemoriesIntent.self, provider: Provider()) { entry in
            ReInventingMemoriesView(Photo: UIImage() ,configuration: ReInventingMemoriesIntent(), iCloudPhotosEnabled: true , entry: entry)
        }
class PHPhotoLibrary: NSObject {
    
  static let shared = PHPhotoLibrary()
    
    var id:Int = 0
    var Photo:UIImage?
    var intent = ReInventingMemoriesIntent()

}

@Jayesh2022

There are definitions that you have not provided. ie:

  • ReInventingMemoriesIntent

Your struct Provider: IntentTimelineProvider appears to be incomplete and the details you provided above implies that the struct ReInventingMemories: Widget { is declared within that Provider.

Can you provide the complete code for each of those structs.

If you have sourced some of this code from online can you share your source.

Hello Chris,
Thanks for your answer.

Actually that Provider is separate struct than others as follows.

struct Provider: IntentTimelineProvider {
    func placeholder(in context: Context) -> SimpleEntry {
        SimpleEntry(date: Date() , configuration: ReInventingMemoriesIntent(), Photo: UIImage())
    }

@Jayesh2022

Where is ReInventingMemoriesIntent defined?

That’s the intent definition file where I defined custom intent Photo.

Sorry mate, but that is not defined anywhere as can be seen by these error messages.

Let me know if there is something missing.

I am trying to help but this is a bit of a struggle at the moment.

In this file other structs are the getsnapshot func and the timeline entry.

And the struct ReInventingMemories include the last three line of code with systemfamily info.

struct ReInventingMemories: Widget {
    let kind: String = "ReInventingMemories"
    
    var body: some WidgetConfiguration {
        IntentConfiguration(kind: kind, intent: ReInventingMemoriesIntent.self, provider: Provider()) { entry in
            ReInventingMemoriesView(Photo: UIImage() ,configuration: ReInventingMemoriesIntent(), iCloudPhotosEnabled: true , entry: entry)
        }
        .configurationDisplayName("My Widget")
        .description("This is an example widget.")
        .supportedFamilies([.systemMedium])
    }
}
struct SimpleEntry: TimelineEntry {
    let date: Date
    let configuration: ReInventingMemoriesIntent
    var Photo: UIImage?
    
    
}

It might be way easier if you create an archive of your project by selecting the top level folder in Finder and then right click on that Folder and select Compress “FolderName”.

Then post that to DropBox and create a share link. Copy that link and paste it in a reply. It will be much easier to debug if I have your entire project.

If you do not want that project to be public then provide the link in a private message.

Hello Chris,

How can send a Private message?

I will send you one that you can reply to.

Ok for sure.