Post

Replies

Boosts

Views

Activity

Easy access to files saved inside documentDirectory
In Swift Playgrounds on macOS, when we create a new file in .documentDirectory given by FileManager, we can always search for the results in e.g. ~/Library/Developer/Xcode/DerivedData/[project-specific]/Build/Products/Debug . I guess this location may change anytime. I know that Xcode is not Matlab, but is there a folder / a shortcut / any place in the IDE that includes the resulting files, or even presents a live preview of created files (.csv) for easy inspection?
1
0
547
Nov ’22
Implementing a modifier that adds an action to perform when the user moves his finger over or away from the view’s frame
Let’s imagine HStack full of views. You put down your finger on the leftmost view and lift it up on the rightmost one. What modifier to use for the views in-betweeen just to be notified when the finger is sliding over them? With mouse it would be onHover, but with finger is there anything? Without a modifier, the result can be achieved with this code: import SwiftUI import PlaygroundSupport // // How to refactor this ugly code // with a modifier similar to onHover(perform:)? // struct ContentView: View{     @State var isHovered = Array(repeating: false, count: 8)     @State private var location = CGPoint.zero     var body: some View{         HStack{             ForEach(0..<8){ i in                 GeometryReader{ g in                     Rectangle()                     .fill(isHovered[i] ? .orange : .gray)                     .onChange(of: location) { newValue in                         isHovered[i] = g.frame( in: .named("keyboardSpace") ).contains(location)                     }                 }             }         }         .gesture(             DragGesture()                 .onChanged { gesture in                     location = gesture.location                 }                 .onEnded{ _ in                     isHovered = Array(repeating: false, count: 8)                 }         )         .frame(width: 500, height: 500)         .coordinateSpace(name: "keyboardSpace")     } } PlaygroundPage.current.setLiveView(ContentView())
0
0
315
Nov ’22
A clean way of checking an assetWriterInput for readiness
The autor of Creating a Movie with an Image and Audio on iOS proposes the way of rechecking for assetwriter readiness. // lines 52-55 while !adaptor.assetWriterInput.isReadyForMoreMediaData { usleep(10) } adaptor.append(buffer, withPresentationTime: startFrameTime) Is this the canonical way of querying AVFoundation's objects, or maybe there's a better way than sleep and try again loop? The only remotely related post I found is How to check if AvAssetWriter has finished writing the last frame, and has four years and no answer.
1
0
495
Nov ’22
Can I modify a View by binding its frame to a given CGRect state value?
For a SwiftUI view, I can SET its size and position with modifiers, or GET its frame by being clever with geometry reader. It is not symmetrical, and violates the Ockham's razor IMO. Do you know a single modifier, from Apple or not, with which caller can set the value of a frame programmatically, BUT ALSO when the view is autopositioned and autosized the binding sets the value to the correct CGRect?
0
0
322
Nov ’22
WWDC example of signal generator outputs files with 0 duration
Using the example from WWDC, with the following command ./SignalGenerator -signal square -duration 3 -output ./uncompressed.wav I can generate an audio file, which is not playable, and its Get Info is: Duration: 00:00 Audio channels: Mono Sample rate: 44,1 kHz Bits per sample: 32 Although for MacOS Preview the duration is zero, this file is playable in VLC and convertible to other formats, so its content is ok. To get more information about the format of an output file I changed the example to print outputFormatSettings: ["AVLinearPCMBitDepthKey": 32, "AVLinearPCMIsBigEndianKey": 0, "AVSampleRateKey": 44100, "AVFormatIDKey": 1819304813, "AVLinearPCMIsFloatKey": 1, "AVNumberOfChannelsKey": 1, "AVLinearPCMIsNonInterleaved": 1] I don’t know how to interpret the “number of AVFormatIDKey": 1819304813. The documentation says: let AVFormatIDKey: String - For information about the possible values for this key, see Audio Format Identifiers Having red this, I still don't know the relation of AVFormatIDKey and listed Audio Format Identifiers. If I knew which file format to expect, it might have helped to guess why the duration of the generated files is always 0? Can you help me with both questions? Thanks.
2
0
1.2k
Nov ’22
Looping over AsyncSequence gives a different result than subscribing with a 'sink'.
Hi, if values are PUBLISHED rapidly, then ALL are present in the Combine sink, but SOME of them are absent from the async loop. Why the difference? For example, in the code below, tapping repeatedly 4 times gives the output: INPUT 24, INPUT 9, INPUT 31, INPUT 45, SINK 24, SINK 9, LOOP 24, SINK 31, SINK 45, LOOP 31. import SwiftUI import Combine import PlaygroundSupport var subject = PassthroughSubject<Int, Never>() struct ContentView: View {     @State var bag = [AnyCancellable]()     @State var a = [String]()     var body: some View {         Text("TAP A FEW TIMES RAPIDLY") .frame(width: 160, height: 160)         .onTapGesture {             Task {                 let anyInt = Int.random(in: 1..<100)                 print("INPUT \(anyInt)")                 try await Task.sleep(nanoseconds: 3_000_000_000)                 subject.send(anyInt)             }         }         .task {             for await anyInt in subject.values {                 print("    LOOP \(anyInt)")             }         }         .onAppear{             subject.sink{ anyInt in                 print("  SINK \(anyInt)")             }.store(in: &bag)         }     } } PlaygroundPage.current.setLiveView(ContentView()) Thank you.
1
0
1.1k
Jul ’22