Post

Replies

Boosts

Views

Activity

Reply to Project Won't Build with .h File
Don't you hate it when this happens? Yes, there is a newer version of Xcode (14.3.1), but I doubt that the problem is that you are not using the latest version, given that your project didn't change. I suggest you take a deep breath, boot into Recovery mode and run Disk First Aid. Perhaps something is messed up in your file system.
Jun ’23
Reply to Swift language, how to convert an array of int type to data type
plain old data in Swift arrays are contiguous in memory. If the type you are storing were more complex, you may need to use ContiguousArray instead of Array. There are lots of withUnsafeXXX functions, both free and member functions; it took me quite a while to find the right ones. This code generates your array of Int values, makes a Data from the array's data, then makes a new Array from the Data's data. There doesn't seem to be a way to make an Array or even a ContiguousArray directly from a buffer pointer. import Foundation let sizeofElement = MemoryLayout<Int>.size // create array (example is twelve Ints from 1 to 12 var array = Array(1...12) // create a Data from the bytes in the original array var data = Data() array.withUnsafeBufferPointer { data = Data(bytes: $0.baseAddress!, count: $0.count * MemoryLayout<Int>.size) } // construct a new array by walking through the bytes of data, one Float at a time // start with an empty array, which we can manipulate in the closure below var newArray: [Int] = [] data.withUnsafeBytes { rawBufferPtr in rawBufferPtr.withMemoryRebound(to: Int.self) { buffer in // access it as contiguous Int values var iter = buffer.makeIterator() while let aNumber = iter.next() { newArray.append(aNumber) } } } print(array) print(newArray)
Topic: Programming Languages SubTopic: Swift Tags:
May ’23
Reply to How to operate a image file
a URL can point at a local file (the scheme is 'file', so such URLs begin "file://"). For example, if you use NSOpenPanel on macOS to enable the user to select the file(s) to open, you'll get an array of URLs from the panel. See https://developer.apple.com/documentation/appkit/nsopenpanel. NSImage supports jpeg format input.
Topic: Programming Languages SubTopic: Swift Tags:
May ’23
Reply to How to operate a image file
the search terms you are looking for are NSImage (macOS) or UIImage (iOS/iPadOS). Images have methods for loading from and writing to a URL. Images can be rendered into a CGContext. CGContexts provide access to their pixel buffers (and various higher level operations which manipulate those buffers).
Topic: Programming Languages SubTopic: Swift Tags:
May ’23
Reply to communicating between agent and app
Well, as with most engineering questions, the answer begins with "it depends". It depends what the data is, and what a user is going to do with it. Generally, you shouldn't hit the file system if you don't need to. What you're building (an agent with a separate UI process) is quite advanced for someone new to programming. You asked, "how would I update the UI to reflect the changes"? If you're new enough to programming to have to ask this question, you should probably start with a simple, single-process application. Once you know how to make a UI respond to changes in your data model, you can work on pushing those changes from another process.
Topic: Programming Languages SubTopic: Swift Tags:
May ’23
Reply to Calculating the Real-World Distance Between middleTip and wrist
Vision points are normalized. Vision has no idea how far away your image subject is. You might be able to use information from the TrueDepth camera (if present, and in use) to derive subject distance, or you can infer this information if you know the size of the subject and the angle of the camera's viewport (it might be digitally zoomed and scaled). Bear in mind that the hand may not be parallel to the plane of the sensor - if you hold your hand at 45 degrees to the sensor it is going to look 29% shorter.
Topic: Spatial Computing SubTopic: ARKit Tags:
May ’23
Reply to SwiftUI - ScrollView with LazyVGrid and onTap, scrolling randomly by itself
ah. thanks for the video, I wasn't scrolling far enough but I couldn't tell where I was. I modified your code so it is easier to see what cell you're at. I couldn't reproduce it with an array of only 30, or even 100 Ints. But with 150 (as below), I can press on C14 and the scroll position will jump so that I'm looking at E14. Your code doesn't modify the scroll position, so this looks like a bug to me. struct ContentView: View { let strings: [String] = ["A", "B", "C", "D", "E"] let ints: [Int] = Array(1...150) @State var selectedCell: String? var body: some View { ZStack { ScrollView { VStack { ForEach(strings, id: \.self) { string in VStack { HStack { Text(string) Spacer() } LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())]) { ForEach(ints, id: \.self) { int in VStack { Text(string + String(int)) Spacer() } .frame(minWidth: 0, maxWidth: .infinity) .frame(height: 200) .background( RoundedRectangle(cornerRadius: 5) .fill(int % 2 == 0 ? .orange : .green) ) .onTapGesture { self.selectedCell = string + String(int) } } } } } } .padding() } if let selectedCell { VStack { Spacer() Text(selectedCell) Spacer() Button { self.selectedCell = nil } label: { Image(systemName: "x.circle") .resizable() .scaledToFit() .frame(width: 30) } } .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity) } } } } the good news is that it seems you can work around it by making the second VStack persist, regardless of whether you are displaying a selection or not. Modify the second VStack like this (remove the if let selectedCell condition) VStack { Spacer() Text(selectedCell ?? "you don't see me") .foregroundColor(selectedCell == nil ? .clear : .black) Spacer() Button { self.selectedCell = nil } label: { Image(systemName: "x.circle") .resizable() .scaledToFit() .frame(width: 30) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’23
Reply to Project Won't Build with .h File
Don't you hate it when this happens? Yes, there is a newer version of Xcode (14.3.1), but I doubt that the problem is that you are not using the latest version, given that your project didn't change. I suggest you take a deep breath, boot into Recovery mode and run Disk First Aid. Perhaps something is messed up in your file system.
Replies
Boosts
Views
Activity
Jun ’23
Reply to Why AVPlayer returns video duration (seconds) as Double?
an AVPlayerItem has a duration property, of type CMTime. CMTime does not have a .toString property, it looks like a member of your team wrote an extension on CMTime to provide such a property. Your confusion probably arises because that extension chose to only display whole seconds. Your video really has a duration of 37,568 ticks of a 1/1000s clock.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
May ’23
Reply to Swift language, how to convert an array of int type to data type
plain old data in Swift arrays are contiguous in memory. If the type you are storing were more complex, you may need to use ContiguousArray instead of Array. There are lots of withUnsafeXXX functions, both free and member functions; it took me quite a while to find the right ones. This code generates your array of Int values, makes a Data from the array's data, then makes a new Array from the Data's data. There doesn't seem to be a way to make an Array or even a ContiguousArray directly from a buffer pointer. import Foundation let sizeofElement = MemoryLayout<Int>.size // create array (example is twelve Ints from 1 to 12 var array = Array(1...12) // create a Data from the bytes in the original array var data = Data() array.withUnsafeBufferPointer { data = Data(bytes: $0.baseAddress!, count: $0.count * MemoryLayout<Int>.size) } // construct a new array by walking through the bytes of data, one Float at a time // start with an empty array, which we can manipulate in the closure below var newArray: [Int] = [] data.withUnsafeBytes { rawBufferPtr in rawBufferPtr.withMemoryRebound(to: Int.self) { buffer in // access it as contiguous Int values var iter = buffer.makeIterator() while let aNumber = iter.next() { newArray.append(aNumber) } } } print(array) print(newArray)
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
May ’23
Reply to How to operate a image file
a URL can point at a local file (the scheme is 'file', so such URLs begin "file://"). For example, if you use NSOpenPanel on macOS to enable the user to select the file(s) to open, you'll get an array of URLs from the panel. See https://developer.apple.com/documentation/appkit/nsopenpanel. NSImage supports jpeg format input.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
May ’23
Reply to How to operate a image file
the search terms you are looking for are NSImage (macOS) or UIImage (iOS/iPadOS). Images have methods for loading from and writing to a URL. Images can be rendered into a CGContext. CGContexts provide access to their pixel buffers (and various higher level operations which manipulate those buffers).
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
May ’23
Reply to communicating between agent and app
Well, as with most engineering questions, the answer begins with "it depends". It depends what the data is, and what a user is going to do with it. Generally, you shouldn't hit the file system if you don't need to. What you're building (an agent with a separate UI process) is quite advanced for someone new to programming. You asked, "how would I update the UI to reflect the changes"? If you're new enough to programming to have to ask this question, you should probably start with a simple, single-process application. Once you know how to make a UI respond to changes in your data model, you can work on pushing those changes from another process.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
May ’23
Reply to Calculating the Real-World Distance Between middleTip and wrist
Vision points are normalized. Vision has no idea how far away your image subject is. You might be able to use information from the TrueDepth camera (if present, and in use) to derive subject distance, or you can infer this information if you know the size of the subject and the angle of the camera's viewport (it might be digitally zoomed and scaled). Bear in mind that the hand may not be parallel to the plane of the sensor - if you hold your hand at 45 degrees to the sensor it is going to look 29% shorter.
Topic: Spatial Computing SubTopic: ARKit Tags:
Replies
Boosts
Views
Activity
May ’23
Reply to SwiftUI - ScrollView with LazyVGrid and onTap, scrolling randomly by itself
ah. thanks for the video, I wasn't scrolling far enough but I couldn't tell where I was. I modified your code so it is easier to see what cell you're at. I couldn't reproduce it with an array of only 30, or even 100 Ints. But with 150 (as below), I can press on C14 and the scroll position will jump so that I'm looking at E14. Your code doesn't modify the scroll position, so this looks like a bug to me. struct ContentView: View { let strings: [String] = ["A", "B", "C", "D", "E"] let ints: [Int] = Array(1...150) @State var selectedCell: String? var body: some View { ZStack { ScrollView { VStack { ForEach(strings, id: \.self) { string in VStack { HStack { Text(string) Spacer() } LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())]) { ForEach(ints, id: \.self) { int in VStack { Text(string + String(int)) Spacer() } .frame(minWidth: 0, maxWidth: .infinity) .frame(height: 200) .background( RoundedRectangle(cornerRadius: 5) .fill(int % 2 == 0 ? .orange : .green) ) .onTapGesture { self.selectedCell = string + String(int) } } } } } } .padding() } if let selectedCell { VStack { Spacer() Text(selectedCell) Spacer() Button { self.selectedCell = nil } label: { Image(systemName: "x.circle") .resizable() .scaledToFit() .frame(width: 30) } } .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity) } } } } the good news is that it seems you can work around it by making the second VStack persist, regardless of whether you are displaying a selection or not. Modify the second VStack like this (remove the if let selectedCell condition) VStack { Spacer() Text(selectedCell ?? "you don't see me") .foregroundColor(selectedCell == nil ? .clear : .black) Spacer() Button { self.selectedCell = nil } label: { Image(systemName: "x.circle") .resizable() .scaledToFit() .frame(width: 30) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’23