hippietrail
hippietrail
Explore posts from servers
TSDThe Swift Den
Created by hippietrail on 9/9/2023 in #swift-development
Can progress reporting be added to this style of https transfer?
import Foundation

func fetchData() async throws -> Data {
let url = URL(string: "https://gist.github.com/khaykov/a6105154becce4c0530da38e723c2330/raw/41ab415ac41c93a198f7da5b47d604956157c5c3/gistfile1.txt")!

var request = URLRequest(url: url)

let (data, _) = try await URLSession.shared.data(for: request)
return data
}

@main
struct Main {
static func main() async throws {
do {
let foo = try await fetchData()
let bar = String(data: foo, encoding: .utf8)
dump(bar)
} catch {
print("** \(error.localizedDescription)")
}
}
}
import Foundation

func fetchData() async throws -> Data {
let url = URL(string: "https://gist.github.com/khaykov/a6105154becce4c0530da38e723c2330/raw/41ab415ac41c93a198f7da5b47d604956157c5c3/gistfile1.txt")!

var request = URLRequest(url: url)

let (data, _) = try await URLSession.shared.data(for: request)
return data
}

@main
struct Main {
static func main() async throws {
do {
let foo = try await fetchData()
let bar = String(data: foo, encoding: .utf8)
dump(bar)
} catch {
print("** \(error.localizedDescription)")
}
}
}
I've asked several AI chatbots and none of their attempts have worked. I'm guessing I should be using the old dataTask ways to do this and that trying to do it with async/await isn't really implemented/supported yet. But even though I'm an old programmer I'm very new to the Apple ecosystem.
5 replies
TSDThe Swift Den
Created by hippietrail on 8/30/2023 in #swift-development
Is it possible to replicate Mac's standard Help menu Search item?
No description
3 replies
TSDThe Swift Den
Created by hippietrail on 8/27/2023 in #swift-development
How to position vertical Slider
I'm an old programmer but new to Mac and SwiftUI. I'm trying to get a vertical Slider positioned right up against the right edge of the window. Apparently this requires rotation:
Slider(value: $vx).rotationEffect(.degrees(v1))
Slider(value: $vx).rotationEffect(.degrees(v1))
But this rotation is just an effect. All measurements, bounding boxes, alignment, etc that effect its size and position behave as though it's still horizontal, which makes it incredibly unintuitive and frustrating to reason about, especially when new and unfamiliar with how positioning etc is done even for normal UI elements. The very few Google hits for this have poor answers like just a slider in the middle, or a complete custom replacement that doesn't behave like a native one. (By the way Android has the exact some problem in both XML and Jetpack Compose. AppKit for one is intuitive though.)
25 replies
TSDThe Swift Den
Created by hippietrail on 9/15/2022 in #swift-development
How to pass parameter to custom iterator
I'm making a custom iterator that returns every slice of a given width through a collection. I got it working with a hardcoded width but can't figure out how to make it a parameter. I want the hard-coded 3 to be replaced with that param:
extension Array {

func windows() -> AnySequence<ArraySlice<Element>> {
return AnySequence({ () -> AnyIterator<ArraySlice<Element>> in
var index = self.startIndex

return AnyIterator({
if index > self.count - 3 {
return nil
}

let result = self[index..<index + 3]

self.formIndex(after: &index)

return result
})
})
}

}

let seq = [1,11,7,444]
for val in seq.windows() {
print(val)
}
extension Array {

func windows() -> AnySequence<ArraySlice<Element>> {
return AnySequence({ () -> AnyIterator<ArraySlice<Element>> in
var index = self.startIndex

return AnyIterator({
if index > self.count - 3 {
return nil
}

let result = self[index..<index + 3]

self.formIndex(after: &index)

return result
})
})
}

}

let seq = [1,11,7,444]
for val in seq.windows() {
print(val)
}
2 replies