Post

Replies

Boosts

Views

Activity

Comment on Use CoreDate with swift
PS I meant to get rid of the cost property in BucketItem, because I essentially replaced it with a computed value totalCost. In this simple app, the cost of a buckelist item is the sum of the associated cost items, therefore the original cost property on BucketItem is redundant - and potentially a consistency problem.
Sep ’21
Comment on Date in Swift printed is wrong
Sorry for the delay: there seems to have been a problem with the forum software and I was getting an error when attempting to respond. You only format the dates at the point of displaying them to the user: for all other purposes, you must use the original dates for comparisons and computations - except where you want specific information about the local date-time e.g. whether it's morning or not, in which case you use the Calendar functions with the date (not the formatted string).
Sep ’21
Comment on help me! (data sync between watch and phone)
My understanding is that you need to store Goal, with its title, count1, count2 and creation time, and that, as the app gets used, more and more Goals are stored. If you only ever store ONE goal on the watch, before it is sent to the phone, then you might be able to use UserDefaults to reload title, count1, count2 and creation time each time the watch app relaunches - before data are sent to the phone. However, "The defaults system allows an app to customize its behavior to match a user’s preferences. For example, you can allow users to specify their preferred units of measurement or media playback speed." - Apple Documentation. What you're suggesting are not preferences, but cumulative data. I wouldn't use UserDefaults this way. When I have a small amount of data that only gets added to by appending, I store it as a text file with comma separated values. For anything else I use SQLite and/or CloudKit. You will be using CoreData on the phone, so (as I've stated) you can use the same code on the watch to store your data there. CoreData, SQLite and CloudKit are all complex (to begin with) but data persistence is an essential skill that must be learned. :). Regards, Michaela
Sep ’21
Comment on Date in Swift printed is wrong
let currentHour = Calendar.current.component(.hour, from: Date()) if currentHour < 12 {print("now is morning")} This gets the hour, as an Integer, from the current local time, i.e. the Calendar function converts the UTC time to the user's current timezone and summertime (or not) settings. I suggest that you experiment with Date formatting and with Calendar in a Playground.
Sep ’21
Comment on Date in Swift printed is wrong
No, comparison of Date types is done on the UTC date-time. If the date-time of your user's timezone is important in decision making, e.g. how many times did the user do X before 10am each day, then you'd need to use the Calendar functions https://developer.apple.com/documentation/foundation/calendar
Sep ’21
Comment on help me! (data sync between watch and phone)
Your data are more complex than I gathered from your original post, i.e. title:number. You're now describing a struct with various properties, which include an array of counts. If this is correct, then there is a way of sending that via transferUserInfo but UserDefaults would not be an appropriate way of storing the data on the watch. Is only one 'title', 'count1', 'count2', 'creation date' struct sent at a time, or an array of them? Is there a variable number of counts per title on a given creation date? The answers to these questions determine how best to store data on the watch (if really needed) and how to transmit to the phone. I can't do much more to help until tomorrow afternoon Eastern Australia time.
Sep ’21
Comment on help me! (data sync between watch and phone)
I’d prefer to post working code on here: I understand your requirement and the code will be short enough to fit in a reply, but without the Coredata part. What version of Xcode, iOS & WatchOS do you use and do you use SwiftUI? If you use SwiftUI, do you generate apps with or without an AppDelegate? These days I prefer “pure” SwiftUI apps, I.e. without the app delegate. I can probably make a start tomorrow morning (12 hours time) if you want, and you get those answers to me. The watch connectivity sample code you refer to (above) contains far more than you need, so it’s not a good starting point.
Sep ’21