All I have come across are NSURLSession. Is this possible for URLSession? How?
I noticed my requests show up instantly so I figured these reponses are cached instead of fetching new ones.
Caching should not be the default. Weird why it was set this way.
Thoughts?
This is my code. It is basic
let url = URL(string: urlLink)!
let (data, _) = try await URLSession.shared.data(from: url)
print(String(decoding: data, as: UTF8.self))
Post
Replies
Boosts
Views
Activity
So String interpolation is possible in Swift. However, the samples i have seen are either expressions or variables placed inside the "".
Is it possible to replace with values if you have {0} and {1} in the string? I do not know what this is called in Swift if this is possible.
I thought it is still interpolation but all I see are posts that do not have {0} ...
Or is this feature not available in Swift?
Need your insights.
Also, since there is no way in the Date struct/class? How can you get the timezone of the date object?
Hi, this is my function
I cannot figure out why i always get jan 1, 2000 [time...]
this is my code
if let thisTime = Int64(time) {
let date = Date(timeIntervalSince1970: TimeInterval(thisTime) / 1000)
let dateWithTimezone = convertStringToDateWithTimezone(date)
print(formatDate(dateWithTimezone, "hh:mm a")!)
}
The date variable is correct but once I use convertStringToDateWithTimezone the result is always jan 1, 2000 [time...] Any idea what could be wrong?
func convertStringToDateWithTimezone(_ d: Date) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.current
return dateFormatter.date(from: (dateFormatter.string(from: d)))!
}
func formatDate(_ date: Date?, _ pattern: String) -> String? {
if let date = date {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en")
dateFormatter.dateFormat = pattern
return dateFormatter.string(from: date)
}
return nil
}
I am converting Java code to Swift. This is the Java code
try {
if (filter == null || Float.parseFloat(ew.getMagnitude()) >= Float.parseFloat(filter))
liist.add(ew);
} catch (NumberFormatException e) {
}
In Swift, currently this is what I have
do {
if let toFilter = filter, Float(ew.magnitude ?? "0") >= Float(toFilter ?? "0") {
list.append(ew)
}
}
else {
list.append(ew)
}
} catch {
}
Currently
it even gives out an error that I have to add a ! after the Float() because "Force-unwrap using '!' to abort execution if the optional value contains 'nil'"
But i do not want that, that is why I placed th do/catch there
Please enlighten. Thank you
Hi. I am new to SwiftUI and i am not sure what the right term for this is. Hoping someone can guide me
Take this generic view for example (this one is incorrect since if i pass a view as parameter, how can it use the fetched data).
struct FetchUrlContentView<Content: View>: View {
var url: String?
@State var loading = true
@State var error = false
@State var view: Content
var body: some View {
HStack {
if !NetworkTool.hasInternet() {
NoInternetView()
}
else if loading {
LoadingProgressView()
}
else if error {
SomethingWrongView()
}
else {
view
}
}
.task {
if let urlLink = self.url {
fetchContentFromUrl(url: urlLink, { data in
// Supposed to use data to the view here but it won't be generic.
// any ideas?
self.loading = false
})
}
else {
self.loading = false
self.error = true
}
}
}
What i am looking for is something of a way to keep on reusing FetchUrlContentView such that after data is fetched, i could call it like this
FetchUrlContentView(url: "https://www.test.com/", loading: true) { data in
MyView(data)
}
where MyView contains something like Text(data.description)
Hi, i had successfully fetched data from a URL but i cannot figure out why its sub link from the same domain, does not fetch any data. it always shows nil, but it contains byte like 39962 bytes.
If it is nil, shouldnt that be something like 0 bytes
Sample url is this.
https://earthquake.phivolcs.dost.gov.ph/2022_Earthquake_Information/September/2022_0922_1059_B2.html
This is how I fetch the data from url
URLSession.shared.dataTask(with: URL(urlString)) { (data, response, error) in
guard let data = data else {
completion(nil)
return
}
if error != nil {
completion(nil)
}
else {
print(data)
completion(String(data: data, encoding: .utf8))
}
}.resume()
Thoughts? What could be wrong?
If it is https://earthquake.phivolcs.dost.gov.ph, i can fetch its data without issues.
Please see sample code
struct Test: View {
var array: [String] = []
init() { }
var body: some View {
Text(String(description: array.count)
}
}
struct Test_Previews: PreviewProvider {
static var previews: some View {
Test(array: ["1", "2", "3"])
}
}
This one is a string array, I wish to pass some values only in preview because in the main view, i will be fetching data from a url. While setting this variable array to an empty array is the solution, what if my variable is a Struct? or Class? How will i make this optional and only supply a new Struct/Class instance in the preview only
This is my struct view for the overflow menu
struct OverflowMenu: View {
var body: some View {
Menu {
NavigationLink(destination: SettingView()) {
Label{
Text("Settings")
}
icon: {
Awesome.Solid.cog.image
.foregroundColor(UIColor(hex: "#ffbd16"))
}
}
} label: {
Image(systemName: "ellipsis.circle")
}
}
}
Nothing happens when the button is clicked. The SettingsView is not shown in a new window. And the OverflowMenuView() is inside the NavigationView. Am i missing something else?
var body: some View {
NavigationView {
.....
HStack {
Spacer()
OverflowMenu()
}
}
}