Post

Replies

Boosts

Views

Activity

Reply to Picker with ForEach
import SwiftUI struct ProviderCalendarView: View { @StateObject var viewModel = YearViewModel() @State private var selectedYear = 3 var body: some View { VStack { HStack { Picker(selection: $selectedYear) { ForEach(viewModel.years, id: \.self) { year in Text("\(year)") } } label: { } } } } } class YearViewModel: ObservableObject { @Published var years: [Int] = [] init() { createYears() } func createYears() { let now = Date() for i in 0..<4 { years.append(2023 + i) } } } I don't use Range to create an array in this case. Yet, I still get the same compiler guy.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’23
Reply to Removing More?
I think I've fixed it. import SwiftUI struct ContentView: View { @State var selectedTab = 0 @State var addTapped = false @State var refresh = false @State var people = [ Person(name: "Alice", systemImage: "person.circle.fill"), Person(name: "Jane", systemImage: "person.circle.fill"), Person(name: "Dave", systemImage: "person.circle.fill"), Person(name: "Susan", systemImage: "person.circle.fill"), Person(name: "Robert", systemImage: "person.circle.fill"), Person(name: "Daniel", systemImage: "person.circle.fill") ] var body: some View { VStack(alignment: .leading, spacing: 0) { ScrollView(.horizontal) { HStack(spacing: 20) { ForEach(0..<people.count, id: \.self) { num in VStack { ... } .foregroundColor(selectedTab == num ? Color.blue : Color.gray) .onTapGesture { self.selectedTab = num } } } }.padding(.horizontal, 10) Spacer() .frame(height: 2) Rectangle().fill(.gray) .frame(height: 1) TabView(selection: $selectedTab) { ForEach(0..<people.count, id: \.self) { num in let person = people[num] Text(person.name) .tag(person.id) } } } .tabViewStyle(.page(indexDisplayMode: .never)) // <<<<<<<<<<<<<<<<<<<<< .onAppear { UITabBar.appearance().isHidden = true } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’23
Reply to Sorting CoreData Records by Creation Date
Thank you for your reply. Yes and no. I've figured out that I can do it by having an additional Date attribute. So I've started with a new Xcode project by having a new Date attribute named createdAt. And code goes as follows. import SwiftUI struct ContentView: View { @Environment(\.managedObjectContext) var managedObject @FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Student.createdAt, ascending: true)]) var students: FetchedResults<Student> var body: some View { VStack { List(students) { student in Text(student.name ?? "") } Button { let firstNames = ... let lastNames = ... if let selectedFirstName = firstNames.randomElement(), let selectedLastName = lastNames.randomElement() { let newStudent = Student(context: managedObject) newStudent.id = UUID() newStudent.name = "\(selectedFirstName) \(selectedLastName)" newStudent.createdAt = Date() // <<<<<<<<<<<<<<<<<<<<<< try? managedObject.save() } } label: { Text("Add") } } } } , which takes care of the sort issue. As for the older project shown above, I wonder if Core Data has a simple mechanism in reordering the records when the entity does not include an additional Date attribute? That was my initial question.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’23
Reply to List Single Selection
Thanks. I'm not sure how that helps, though. In the meantime, Paul from Hacking with Swift shows how the List guy works. I've tested the exact lines of code he has, and the selection symbol doesn't appear, either. That's odd. The article is not even one year old. By the way, I'm using Xcode 14.3. https://www.hackingwithswift.com/quick-start/swiftui/how-to-allow-row-selection-in-a-list
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’23
Reply to Making a Call to a Distant Struct
I think another approach is use of an ObservableObject class with onReceive(_:perform:) .
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’23
Reply to Picker with ForEach
The following works. Picker(selection: $selectedYear) { ForEach(viewModel.years.indices, id: \.self) { Text("\(viewModel.years[$0])") } } label: { }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Aug ’23
Reply to Picker with ForEach
import SwiftUI struct ProviderCalendarView: View { @StateObject var viewModel = YearViewModel() @State private var selectedYear = 3 var body: some View { VStack { HStack { Picker(selection: $selectedYear) { ForEach(viewModel.years, id: \.self) { year in Text("\(year)") } } label: { } } } } } class YearViewModel: ObservableObject { @Published var years: [Int] = [] init() { createYears() } func createYears() { let now = Date() for i in 0..<4 { years.append(2023 + i) } } } I don't use Range to create an array in this case. Yet, I still get the same compiler guy.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Aug ’23
Reply to Exporting a Document with FileDocument, Not Packaged
Silly me... func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper { let fileContent = try JSONEncoder().encode(self.document) return FileWrapper(regularFileWithContents: fileContent) }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Aug ’23
Reply to Exporting a Document with FileDocument, Not Packaged
Hmm... Maybe let fileWrapper = FileWrapper(directoryWithFileWrappers: [filename: jsonFileWrapper]) is wrong?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Aug ’23
Reply to Removing More?
I think I've fixed it. import SwiftUI struct ContentView: View { @State var selectedTab = 0 @State var addTapped = false @State var refresh = false @State var people = [ Person(name: "Alice", systemImage: "person.circle.fill"), Person(name: "Jane", systemImage: "person.circle.fill"), Person(name: "Dave", systemImage: "person.circle.fill"), Person(name: "Susan", systemImage: "person.circle.fill"), Person(name: "Robert", systemImage: "person.circle.fill"), Person(name: "Daniel", systemImage: "person.circle.fill") ] var body: some View { VStack(alignment: .leading, spacing: 0) { ScrollView(.horizontal) { HStack(spacing: 20) { ForEach(0..<people.count, id: \.self) { num in VStack { ... } .foregroundColor(selectedTab == num ? Color.blue : Color.gray) .onTapGesture { self.selectedTab = num } } } }.padding(.horizontal, 10) Spacer() .frame(height: 2) Rectangle().fill(.gray) .frame(height: 1) TabView(selection: $selectedTab) { ForEach(0..<people.count, id: \.self) { num in let person = people[num] Text(person.name) .tag(person.id) } } } .tabViewStyle(.page(indexDisplayMode: .never)) // <<<<<<<<<<<<<<<<<<<<< .onAppear { UITabBar.appearance().isHidden = true } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Aug ’23
Reply to Removing More?
I think my topic is related to https://developer.apple.com/forums/thread/697197 .
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Aug ’23
Reply to Sorting CoreData Records by Creation Date
Thank you for your reply. Yes and no. I've figured out that I can do it by having an additional Date attribute. So I've started with a new Xcode project by having a new Date attribute named createdAt. And code goes as follows. import SwiftUI struct ContentView: View { @Environment(\.managedObjectContext) var managedObject @FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Student.createdAt, ascending: true)]) var students: FetchedResults<Student> var body: some View { VStack { List(students) { student in Text(student.name ?? "") } Button { let firstNames = ... let lastNames = ... if let selectedFirstName = firstNames.randomElement(), let selectedLastName = lastNames.randomElement() { let newStudent = Student(context: managedObject) newStudent.id = UUID() newStudent.name = "\(selectedFirstName) \(selectedLastName)" newStudent.createdAt = Date() // <<<<<<<<<<<<<<<<<<<<<< try? managedObject.save() } } label: { Text("Add") } } } } , which takes care of the sort issue. As for the older project shown above, I wonder if Core Data has a simple mechanism in reordering the records when the entity does not include an additional Date attribute? That was my initial question.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jul ’23
Reply to NSItemProvider & DropDelegate - Shrinking Preview Picture
No, I haven't. I don't even remember having posted this question although I know what this case is about.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jul ’23
Reply to List Single Selection
Thanks. I'm not sure how that helps, though. In the meantime, Paul from Hacking with Swift shows how the List guy works. I've tested the exact lines of code he has, and the selection symbol doesn't appear, either. That's odd. The article is not even one year old. By the way, I'm using Xcode 14.3. https://www.hackingwithswift.com/quick-start/swiftui/how-to-allow-row-selection-in-a-list
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jul ’23
Reply to List Single Selection
Hmm... I'm sorry. I realize that I should have done that. I cannot edit the topic any more.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jul ’23
Reply to Drawing a Pie without Path?
It's cheesy, but I guess one way of doing it is to use Circle().stroke.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jul ’23
Reply to Reading a PSSliderSpecifier value in Settings Bundle
I got it.
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Jan ’23
Reply to Saving Camera-Captured Picture to Photo with AVCaptureSession
I guess it's the matter of setting a preset to AVCaptureSession.
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Dec ’22
Reply to Saving Camera-Captured Picture to Photo with AVCaptureSession
If I set the preset to captureSession.sessionPreset = .hd1280x720 , I get a different image.
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Dec ’22