cannot find 'minutes' in the scope

im getting this error after this code, what module should Iimport here? or does it causes any other problem?


//  ContentView.swift

//  Widget_and_LiveActivities

//

//  Created by Makwin Santhosh K on 20/11/22.

//



import SwiftUI

import ClockKit





struct ContentView: View {

    var body: some View {

        var future = Calendar.current.date(byAdding: .minute, value: (Int(minutes) ?? 0), to: Date())! // here is where im getting (cannot find 'minutes' in the scope)

        future = Calendar.current.date(byAdding: .second, value: (Int(seconds) ?? 0), to: future)! // also where is where im getting (cannot find 'seconds' in the scope)

        let date = Date.now...future

        let updatedDeliveryStatus = PizzaDeliveryAttributes.PizzaDeliveryStatus(driverName: "Anne Johnson", deliveryTimer: date)

        let alertConfiguration = AlertConfiguration(title: "Delivery Update", body: "Your pizza order will arrive in 25 minutes.", sound: .default)



        await deliveryActivity?.update(using: updatedDeliveryStatus, alertConfiguration: alertConfiguration)

        

        

        let initialContentState = PizzaDeliveryAttributes.ContentState(driverName: "Bill James", deliveryTimer:date)

        let activityAttributes = PizzaDeliveryAttributes(numberOfPizzas: 3, totalAmount: "$42.00", orderNumber: "12345")



        do {

            deliveryActivity = try Activity.request(attributes: activityAttributes, contentState: initialContentState)

            print("Requested a pizza delivery Live Activity \(String(describing: deliveryActivity?.id)).")

        } catch (let error) {

            print("Error requesting pizza delivery Live Activity \(error.localizedDescription).")

        }

    }

}



struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView()

    }

}
 
// this is the example code from 
[https://developer.apple.com/documentation/activitykit/displaying-live-data-with-live-activities)


  
Answered by Claude31 in 737366022

Sorry, but I still do not catch it.

I tested the following which works:

struct ContentView: View {
    let start = Date() 
    let future = Calendar.current.date(byAdding: .minute, value: 10, to: Date())!
    var deliveryTimer: ClosedRange<Date> {
        start...future
    }

    var body: some View {
            Text(timerInterval: deliveryTimer, countsDown: true)
    }
}

In the following

var future = Calendar.current.date(byAdding: .minute, value: (Int(minutes) ), to: Date())!

you use minutes. Where is the String minutes defined ?

if you change to

var future = Calendar.current.date(byAdding: .minute, value: 10, to: Date())!

That does work

That's normal as minutes and seconds are not defined anywhere.

What are the values you expect in them ? Where do you get those values ?

You should have something like:

struct ContentView: View {
    var minutes = "" // Or a computed var to get the value from somewhere
    var seconds = ""

    var body: some View {

Oh i must check the code

and I have clearly checked the code and here is my doubt

I have set deliveryTimer to be ClosedRange data format so to get ClosedRange input

so if I give Minutes and Seconds as variable it is showing error in Timer

I have added the code below which the data transfers from Content view to this view seeing this might give you a idea


import WidgetKit

import SwiftUI

import Foundation







struct dynamicIsland : Widget {

    

    var body: some WidgetConfiguration{

        ActivityConfiguration(for: PizzaDeliveryAttributes.self) { context in

            

            LockScreenLiveActivityView(context: context)

        } dynamicIsland: { context in

            DynamicIsland{

                

                DynamicIslandExpandedRegion(.leading){

                    Label("\(context.attributes.numberOfPizzas) Pizzas", systemImage: "bag")

                        .foregroundColor(.indigo)

                        .font(.title2)

                }

                DynamicIslandExpandedRegion(.trailing) {

                    Label {

                        Text(timerInterval: context.state.deliveryTimer, countsDown: true)

                            .multilineTextAlignment(.trailing)

                            .frame(width: 50)

                            .monospacedDigit()

                    } icon: {

                        Image(systemName: "timer")

                            .foregroundColor(.indigo)

                    }

                    .font(.title2)

                }

                

                DynamicIslandExpandedRegion(.center) {

                    Text("\(context.state.driverName) is on their way!")

                        .lineLimit(1)

                        .font(.caption)

                }

                

                DynamicIslandExpandedRegion(.bottom) {

                    Button {

                        // Deep link into your app.

                    } label: {

                        Label("Call driver", systemImage: "phone")

                    }

                    .foregroundColor(.indigo)

                }

            } compactLeading: {

                // Create the compact leading presentation.

                // ...

            } compactTrailing: {

                // Create the compact trailing presentation.

                // ...

            } minimal: {

                // Create the minimal presentation.

                // ...

            }

            .contentMargins(.trailing, 8, for: .expanded)

            .keylineTint(.yellow)

        }

    }

}

        

        

        struct LockScreenLiveActivityView: View {

            let context: ActivityViewContext<PizzaDeliveryAttributes>

            

            var body: some View {

                VStack {

                    Spacer()

                    Text("\(context.state.driverName) is on their way with your pizza!")

                    Spacer()

                    HStack {

                        Spacer()

                        Label {

                            Text("\(context.attributes.numberOfPizzas) Pizzas")

                        } icon: {

                            Image(systemName: "bag")

                                .foregroundColor(.indigo)

                        }

                        .font(.title2)

                        Spacer()

                        Label {

                            Text(timerInterval: context.state.deliveryTimer, countsDown: true) // im getting error here if I give var minutes = 10 something like this, it gives error (I think for ClosedRange<date> the input that we should give is in different format)

                                .multilineTextAlignment(.center)

                                .frame(width: 50)

                                .monospacedDigit()

                        } icon: {

                            Image(systemName: "timer")

                                .foregroundColor(.indigo)

                        }

                        .font(.title2)

                        Spacer()

                    }

                    Spacer()

                }

                .activitySystemActionForegroundColor(.indigo)

                .activityBackgroundTint(.cyan)

            }

        }

   

Please format your code properly, without all those empty lines, it is so hard to read.

Does your code compile ? I get errors with Labels:

                        Label {
                            Text("\(context.attributes.numberOfPizzas) Pizzas")
                        } icon: {
                            Image(systemName: "bag")
                                .foregroundColor(.indigo)
                        }
                        .font(.title2)

Incorrect argument label in call (have '_:icon:', expected 'text:showIcon:')

Trailing closure passed to parameter of type 'String' that does not accept a closure


import WidgetKit

import SwiftUI

import Foundation







struct dynamicIsland : Widget {

    

    var body: some WidgetConfiguration{

        ActivityConfiguration(for: PizzaDeliveryAttributes.self) { context in

            

            LockScreenLiveActivityView(context: context)

        } dynamicIsland: { context in

            DynamicIsland{

                

                DynamicIslandExpandedRegion(.leading){

                    Label("\(context.attributes.numberOfPizzas) Pizzas", systemImage: "bag")

                        .foregroundColor(.indigo)

                        .font(.title2)

                }

                DynamicIslandExpandedRegion(.trailing) {

                    Label {

                        Text(timerInterval: context.state.deliveryTimer , countsDown: true)

                            .multilineTextAlignment(.trailing)

                            .frame(width: 50)

                            .monospacedDigit()

                    } icon: {

                        Image(systemName: "timer")

                            .foregroundColor(.indigo)

                    }

                    .font(.title2)

                }

                

                DynamicIslandExpandedRegion(.center) {

                    Text("\(context.state.driverName) is on their way!")

                        .lineLimit(1)

                        .font(.caption)

                }

                

                DynamicIslandExpandedRegion(.bottom) {

                    Button {

                        // Deep link into your app.

                    } label: {

                        Label("Call driver", systemImage: "phone")

                    }

                    .foregroundColor(.indigo)

                }

            } compactLeading: {

                // Create the compact leading presentation.

                // ...

            } compactTrailing: {

                // Create the compact trailing presentation.

                // ...

            } minimal: {

                // Create the minimal presentation.

                // ...

            }

            .contentMargins(.trailing, 8, for: .expanded)

            .keylineTint(.yellow)

        }

    }

}

        

        

        struct LockScreenLiveActivityView: View {

            let context: ActivityViewContext<PizzaDeliveryAttributes>

            

            var body: some View {

                VStack {

                    Spacer()

                    Text("\(context.state.driverName) is on their way with your pizza!")

                    Spacer()

                    HStack {

                        Spacer()

                        Label {

                            Text("\(context.attributes.numberOfPizzas) Pizzas")

                        } icon: {

                            Image(systemName: "bag")

                                .foregroundColor(.indigo)

                        }

                        .font(.title2)

                        Spacer()

                        Label {

                            Text(timerInterval: context.state.deliveryTimer, countsDown: true)

                                .multilineTextAlignment(.center)

                                .frame(width: 50)

                                .monospacedDigit()

                        } icon: {

                            Image(systemName: "timer")

                                .foregroundColor(.indigo)

                        }

                        .font(.title2)

                        Spacer()

                    }

                    Spacer()

                }

                .activitySystemActionForegroundColor(.indigo)

                .activityBackgroundTint(.cyan)

            }

        }

   

struct PizzaDeliveryAttributes: ActivityAttributes {

    public typealias PizzaDeliveryStatus = ContentState



    public struct ContentState: Codable, Hashable {

        var driverName: String

        var deliveryTimer: ClosedRange<Date>

    }

    //var id = UUID()

    var numberOfPizzas: Int

    var totalAmount: String

    var orderNumber: String

}

now check the code above also I have this in my contentview


//  ContentView.swift

//  Widget_and_LiveActivities

//

//  Created by Makwin Santhosh K on 20/11/22.

//



import SwiftUI

import WidgetKit

import ActivityKit



struct ContentView: View {

 

    var body: some View {

        ZStack {

            VStack{

                Button("Start Activity"){

                    AddLiveACtivity()

                }

            }

        }

        .navigationTitle("Live Activities")

    }

}



func AddLiveACtivity() {

  

    var future = Calendar.current.date(byAdding: .minute, value: (Int(minutes) ), to: Date())!

    future = Calendar.current.date(byAdding: .second, value: (Int(seconds) ), to: future)!

    

    let date = Date.now...future

    let activityAttributes = PizzaDeliveryAttributes(numberOfPizzas: 10, totalAmount: "$100", orderNumber: "101")

    let initialContentState = PizzaDeliveryAttributes.ContentState(driverName: "Makwin", deliveryTimer: date )

    

    

    do {

        let deliveryActivity = try Activity<PizzaDeliveryAttributes>.request(attributes: activityAttributes, contentState: initialContentState,pushType: nil)

        print("Requested a pizza delivery Live Activity \(String(describing: deliveryActivity.id)).")

    } catch (let error) {

        print("Error requesting pizza delivery Live Activity \(error.localizedDescription).")

    }

}



struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView()

    }

}



  

inhave just added the code which caused the error label

    public typealias PizzaDeliveryStatus = ContentState
    public struct ContentState: Codable, Hashable {
        var driverName: String
        var deliveryTimer: ClosedRange<Date>
    }
    //var id = UUID()
    var numberOfPizzas: Int
    var totalAmount: String
    var orderNumber: String
}

i think i have a doubt in syntax only

i have used

Text(timerInterval: context.state.deliveryTimer, countsDown: true)

Where deliveryTimer is in ClosedRange Format like

var deliveryTimer: ClosedRange<Date>

Now if you go through this below code

   var future = Calendar.current.date(byAdding: .minute, value: (Int(minutes) ), to: Date())!
    future = Calendar.current.date(byAdding: .second, value: (Int(seconds) ), to: future)!    
    let date = Date.now...future
    let activityAttributes = PizzaDeliveryAttributes(numberOfPizzas: 10, totalAmount: "$100", orderNumber: "101")
    let initialContentState = PizzaDeliveryAttributes.ContentState(driverName: "Makwin", deliveryTimer: date )
    do {
        let deliveryActivity = try Activity<PizzaDeliveryAttributes>.request(attributes: activityAttributes, contentState: initialContentState,pushType: nil)
        print("Requested a pizza delivery Live Activity \(String(describing: deliveryActivity.id)).")
    } catch (let error) {
        print("Error requesting pizza delivery Live Activity \(error.localizedDescription).")
    }
}

now you may get some clearance of my doubt if i give value for minutes and seconds which is not defined in the code as Int or String it will not accept

do you know how to give input format for this Minutes and Seconds which is not defined in my code

var future = Calendar.current.date(byAdding: .minute, value: (Int(minutes) ), to: Date())!

future = Calendar.current.date(byAdding: .second, value: (Int(seconds) ), to: future)!
Accepted Answer

Sorry, but I still do not catch it.

I tested the following which works:

struct ContentView: View {
    let start = Date() 
    let future = Calendar.current.date(byAdding: .minute, value: 10, to: Date())!
    var deliveryTimer: ClosedRange<Date> {
        start...future
    }

    var body: some View {
            Text(timerInterval: deliveryTimer, countsDown: true)
    }
}

In the following

var future = Calendar.current.date(byAdding: .minute, value: (Int(minutes) ), to: Date())!

you use minutes. Where is the String minutes defined ?

if you change to

var future = Calendar.current.date(byAdding: .minute, value: 10, to: Date())!

That does work

cannot find 'minutes' in the scope
 
 
Q