Post

Replies

Boosts

Views

Activity

Reply to ViewDidLoad in SwiftUI
I think both task and onAppear are executed before the view is displayed, so you can use either. A quick internet search suggests this page has some good examples: https://byby.dev/swiftui-task-vs-onappear
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’24
Reply to Online data to swift UI
There's a bunch of ways to do this. Let me explain a few ideas I've got... If your website is using some sort of Content Management System (CMS), then it likely already provides a JSON file that contains this information. You could download that file, interrogate the various parts of it and determine where the important information is held for what your app needs, then load the thumbnail images, headlines and preview text from there. If it doesn't use a CMS, someone somewhere (you) is always going to have to create this file of metadata. If I were you I'd have it as part of the website/hosting, and your app should download that file. I wouldn't put it in the app itself because you'd have to update the app - which is labour-intensive, and pointless. (We don't have to download a new copy of Apple's News app every time there's a new story.) What you would do with the JSON file downloaded from the website is take the relevant bits from it, then add the image, headline, preview text to an array of articles in the app, and display those in your view. I'd say sort it by article date descending, and limit how many you put in the view. When a user taps one of the articles, you would then download the full article from the website. It wouldn't make any sense to download the full article as part of the JSON file as the user might not view it. Be sure to implement error handling if you can't reach the website to download the data. And don't download data you already have, i.e. only download article data for articles after the most recent date in your list of articles. For example, if you have two articles already downloaded in the app, and they're dated last Monday and last Tuesday, if the JSON file now contains an article for today, you would only bother downloading today's article. If you're going to limit the number of articles shown your view, then your array can be a FIFO stack, i.e. first in first out. If you want to retain 20 articles then you would remove the oldest article from the array and add a new one into it. Because you're sorting the data in the view anyway there is no need to handling rotating the data in the array, i.e. no need to delete item 0 and move item 1 into slot 0, 2 into slot 1 etc. Hope this helps.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’24
Reply to Attempt to present * on * from * while a presentation is in progress
A few things here: You don't need to write this: let months = [1,2,3,4,5,6,7,8,9,10,11,12] You can use: let months = [1...12] But, you only use this once, so instead of this: ForEach (months, id: \.self) { month in you can use this: ForEach(1...12, id: \.self) { month in Secondly, when you write switch month { case 01: ... You've declared month as an Int, and you don't need to use leading zeroes, so you should write: switch month { case 1: ... BUT... There's a much simpler way of doing this. Look into DateFormatter() with a format of MMM. Something like: func getMonthText(_ month: Int) -> String { let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: "en_US") dateFormatter.dateFormat = "MM" let firstOfMonth = dateFormatter.date(from: "\(month)")! dateFormatter.dateFormat = "MMM" return dateFormatter.string(from: firstOfMonth) } The issue you're having with presenting is because you've put the sheet inside the ForEach loop. You're only showing the sheet once when one thing is tapped, so you can just add it anywhere in the View. This works: import SwiftUI struct ContentView: View { @State private var selectedMonth = 1 @State private var selectedYear = "2024" @State private var isShowing = false var body: some View { List { ForEach(1...12, id: \.self) { month in HStack { ViewList(month: month, year: selectedYear) .onTapGesture { isShowing = true selectedMonth = month } } } } .listRowSpacing(2) .listStyle(.grouped) // MOVED THIS TO HERE .sheet(isPresented: $isShowing){ PopupView(month: selectedMonth, year: selectedYear) .presentationDetents([.large]) } } } struct ViewList: View { var month: Int var year: String var body: some View { VStack(alignment: .leading) { Text("\(getMonthText(month)) / \(year)") .font(.headline) } } } struct PopupView: View { @Environment(\.dismiss) var dismiss var month: Int var year: String @State private var selectedMonthText = "Jan" @State private var imageText = "plus" @State private var items = ["Cat", "Dog", "Bird", "Snake"] var body: some View { Button("Dismiss") { dismiss() } Text("\(selectedMonthText) / \(year)") List { ForEach(items, id: \.self) {item in HStack { Text(item) Text("Fed on: ") Text(selectedMonthText) } } } .listRowSpacing(0) .listStyle(.inset) .onAppear { selectedMonthText = getMonthText(month) } } } func getMonthText(_ month: Int) -> String { let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: "en_US") dateFormatter.dateFormat = "MM" let firstOfMonth = dateFormatter.date(from: "\(month)")! dateFormatter.dateFormat = "MMM" return dateFormatter.string(from: firstOfMonth) } #Preview { ContentView() }
Topic: Community SubTopic: Apple Developers Tags:
Jul ’24
Reply to Decoding JSON with a reserved property name
Have you tried using backticks? var `Type`: String
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jul ’24
Reply to IOS Beta 18
It's a beta. Who forced you to install it? Just uninstall it. Plug it into a computer and put it in DFU mode. You can search the internet for instructions. I hope you have a backup from iOS 17.x.
Replies
Boosts
Views
Activity
Jul ’24
Reply to ViewDidLoad in SwiftUI
I think both task and onAppear are executed before the view is displayed, so you can use either. A quick internet search suggests this page has some good examples: https://byby.dev/swiftui-task-vs-onappear
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jul ’24
Reply to iOS 18 and iPhone 12 battery
Beta releases generally include logs of debugging and various processes running in the background that can use up battery. I wouldn't install a beta on my primary device because I'd hit issues exactly like these.
Replies
Boosts
Views
Activity
Jul ’24
Reply to iPhone randomly shutting off
Have you raised a Feedback report? If not, please do so at https://www.apple.com/feedback/ then post the FB number here.
Replies
Boosts
Views
Activity
Jul ’24
Reply to Failed to produce diagnostic for expression; please submit a bug report
What is StandardHomeMovie? What is Movie? What is exampleMovie2? What is CustomTabSwitcher? What is SmallVerticalButton? What is PlayButton? Without this code we can't really help you. I could create a simple struct to cover each of those, but who's to say the issue isn't in one of those?
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jul ’24
Reply to Online data to swift UI
There's a bunch of ways to do this. Let me explain a few ideas I've got... If your website is using some sort of Content Management System (CMS), then it likely already provides a JSON file that contains this information. You could download that file, interrogate the various parts of it and determine where the important information is held for what your app needs, then load the thumbnail images, headlines and preview text from there. If it doesn't use a CMS, someone somewhere (you) is always going to have to create this file of metadata. If I were you I'd have it as part of the website/hosting, and your app should download that file. I wouldn't put it in the app itself because you'd have to update the app - which is labour-intensive, and pointless. (We don't have to download a new copy of Apple's News app every time there's a new story.) What you would do with the JSON file downloaded from the website is take the relevant bits from it, then add the image, headline, preview text to an array of articles in the app, and display those in your view. I'd say sort it by article date descending, and limit how many you put in the view. When a user taps one of the articles, you would then download the full article from the website. It wouldn't make any sense to download the full article as part of the JSON file as the user might not view it. Be sure to implement error handling if you can't reach the website to download the data. And don't download data you already have, i.e. only download article data for articles after the most recent date in your list of articles. For example, if you have two articles already downloaded in the app, and they're dated last Monday and last Tuesday, if the JSON file now contains an article for today, you would only bother downloading today's article. If you're going to limit the number of articles shown your view, then your array can be a FIFO stack, i.e. first in first out. If you want to retain 20 articles then you would remove the oldest article from the array and add a new one into it. Because you're sorting the data in the view anyway there is no need to handling rotating the data in the array, i.e. no need to delete item 0 and move item 1 into slot 0, 2 into slot 1 etc. Hope this helps.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jul ’24
Reply to iOS 18
It's quite a subjective question. Have you heard any reason why you shouldn't update? You could always take a backup of your current device, then do the update. If you don't like it, restore from your backup.
Replies
Boosts
Views
Activity
Jul ’24
Reply to Showing All Issues Undefined symbol: _kGADErrorDomain
Surely this is something you should contact Google about?
Replies
Boosts
Views
Activity
Jul ’24
Reply to Can I block certain iPhone types from using my app?
No, you just have to rearrange your UI to cope with smaller devices. One of my apps has a maximum of 6 icons in the navigation bar. For devices with smaller widths I move one or two of those icons to somewhere else. You should look into re-jigging your UI to handle these devices.
Replies
Boosts
Views
Activity
Jul ’24
Reply to Need some help! Failed to produce diagnostic for expression error
Can't really do much with this. You haven't provided AuthenticationManager. Have you tried commenting out certain bits to narrow down where the error is?
Topic: UI Frameworks SubTopic: SwiftUI
Replies
Boosts
Views
Activity
Jul ’24
Reply to Attempt to present * on * from * while a presentation is in progress
A few things here: You don't need to write this: let months = [1,2,3,4,5,6,7,8,9,10,11,12] You can use: let months = [1...12] But, you only use this once, so instead of this: ForEach (months, id: \.self) { month in you can use this: ForEach(1...12, id: \.self) { month in Secondly, when you write switch month { case 01: ... You've declared month as an Int, and you don't need to use leading zeroes, so you should write: switch month { case 1: ... BUT... There's a much simpler way of doing this. Look into DateFormatter() with a format of MMM. Something like: func getMonthText(_ month: Int) -> String { let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: "en_US") dateFormatter.dateFormat = "MM" let firstOfMonth = dateFormatter.date(from: "\(month)")! dateFormatter.dateFormat = "MMM" return dateFormatter.string(from: firstOfMonth) } The issue you're having with presenting is because you've put the sheet inside the ForEach loop. You're only showing the sheet once when one thing is tapped, so you can just add it anywhere in the View. This works: import SwiftUI struct ContentView: View { @State private var selectedMonth = 1 @State private var selectedYear = "2024" @State private var isShowing = false var body: some View { List { ForEach(1...12, id: \.self) { month in HStack { ViewList(month: month, year: selectedYear) .onTapGesture { isShowing = true selectedMonth = month } } } } .listRowSpacing(2) .listStyle(.grouped) // MOVED THIS TO HERE .sheet(isPresented: $isShowing){ PopupView(month: selectedMonth, year: selectedYear) .presentationDetents([.large]) } } } struct ViewList: View { var month: Int var year: String var body: some View { VStack(alignment: .leading) { Text("\(getMonthText(month)) / \(year)") .font(.headline) } } } struct PopupView: View { @Environment(\.dismiss) var dismiss var month: Int var year: String @State private var selectedMonthText = "Jan" @State private var imageText = "plus" @State private var items = ["Cat", "Dog", "Bird", "Snake"] var body: some View { Button("Dismiss") { dismiss() } Text("\(selectedMonthText) / \(year)") List { ForEach(items, id: \.self) {item in HStack { Text(item) Text("Fed on: ") Text(selectedMonthText) } } } .listRowSpacing(0) .listStyle(.inset) .onAppear { selectedMonthText = getMonthText(month) } } } func getMonthText(_ month: Int) -> String { let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: "en_US") dateFormatter.dateFormat = "MM" let firstOfMonth = dateFormatter.date(from: "\(month)")! dateFormatter.dateFormat = "MMM" return dateFormatter.string(from: firstOfMonth) } #Preview { ContentView() }
Topic: Community SubTopic: Apple Developers Tags:
Replies
Boosts
Views
Activity
Jul ’24
Reply to App Icon not loading
Have you raised a Feedback report? If not, please do so at https://www.apple.com/feedback/ then post the FB number here.
Topic: UI Frameworks SubTopic: General
Replies
Boosts
Views
Activity
Jul ’24
Reply to Map behaves differently compared to MKMapView
I'm marking this as a duplicate post because you already raised it here: https://developer.apple.com/forums/thread/758894 under a different username.
Replies
Boosts
Views
Activity
Jul ’24
Reply to Xcode 16 Beta, macOS 15 Beta - Lists look different?!
There's probably a new default list spacing value. Have you tried to set the spacing manually? If you do, it will always be the value you set, rather than using a default.
Topic: UI Frameworks SubTopic: SwiftUI
Replies
Boosts
Views
Activity
Jul ’24