Post

Replies

Boosts

Views

Activity

How To Make Optional Variables in SwiftUI View
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
2
0
994
Sep ’22
Help Setting Google Maps
Hi all. I have not found a solution to this. Showing Recent Issues GoogleMaps.xcframework' is missing architecture(s) required by this target (arm64), but may still be link-compatible. So many posts saying go to Build Settings and in debug/release exclude arm64 but there is none here. I am using xcode 13.4.1 Anyone got ideas?
2
0
699
Oct ’22
How To Create a Marker With Callout in MapKit SwiftUI
I have not seen a tutorial that shows a callout when a marker is clicked in the map. Can anyone please confirm? Weird that this feature is not included. The callout I need is just a simple title and subtitle. That's all. Tutorials only show how to render using MapMarker, MapPin and custom MapAnnotations but these do nothing since they only show icons.
2
0
532
Oct ’22
Using if condition in Kingfisher.shared.retrieveImage
Hi. This short code for Kingfisher goes like this KingfisherManager.shared.retrieveImage(with: ImageResource(downloadURL: URL(string: "URL HERE")!)) { result in           switch result {             case .success(let value):                             break             default:               break           }         } I am actually interested in .success only but if i remove default it will show an error message like Switch must be exhaustive. How to make this an if statement that goes something like this if result == .success { how to get value in scope here so i can use it. thoughts? }
2
0
866
Oct ’22
Merge An Array Item's Array Property Based On Property
The title might be confusing but here is an example data to give a clear understanding struct City { var name: String? var businesses: [Business]? } So if i have a city array, i wish to merge the businesses array based on the city name since the city may have duplicates e.g. [ { name: "CIty 1" businesses: [ { name: "Business 1" }, { name: "Business 2" }, ] }, { name: "CIty 2" businesses: [ { name: "Business 1" }, { name: "Business 2" }, ] } { name: "CIty 1" businesses: [ { name: "Business 3" }, { name: "Business 4" }, ] } ] In the data example above, there are 2 entries of City1. i am looking for a way to not have to use a dictionary, if it is possible to use the existing array and output it such that there is only 1 entry of City but will contain 4 entries of business. Thoughts?
2
0
411
Oct ’22
How to Resize Placeholder in Kingfisher where loading icon is in the middle
Hi. Please see screenshot. Currently the icon placeholder is small. What I am trying to replicate is to have an area (the one in red) where the place holder icon is in the middle horizontally and vertically. That same red area is where the downloaded image will be drawn. I think the term for this is aspect ratio if i am not mistaken? That the height should be proportionate based on the width. I am using Kingfisher to load the image. This is what I have. KFImage.url(URL(string: ""))             .placeholder {               HStack {                 Image(systemName: "photo")                   .resizable()                   .frame(width: 50, alignment: .center)                                       .background(.red)               }               .background(.yellow)               .scaledToFit()             }             .fade(duration: 0.25)             .onFailure { error in print("Load image error : \(error) = \(earthquakeDetail.imgUrl)")             }             .resizable()             .aspectRatio(contentMode: .fill) Based on the screenshot image, the area for HStack is so thin with its height being small. My goal is to create an area like the one in red border. Thoughts?
2
0
1.6k
Oct ’22
Why Is Bottom Sheet Full Screen? What is the workaround for Dynamic Height?
Hi. Beginner here. I am looking at bottom sheet and it is frustrating that the result is always full screen. Sure, in iPad it gets displayed as a modal dialog. I wonder why they just let it behave like that in the iPhone. The sheet should resize. based on the contents. What is your workaround for this? Should I still use sheet? or do it some other way using something else? I looked at .presentationDetents() but i can only set a fixed height to it. My content can be either 3,4,5,6,7... lines of Text() so my goal is to make the sheet height based on the contents inside it. Thoughts, ideas welcome.
2
0
2.2k
Oct ’22
Why Not Every Part Of View Is Clickable In NavigationLink
My navigation link looks like this struct ContentView: View { var body: some View {     NavigationView {       GeometryReader { geometryProxy in         NavigationLink {             EmptyView() // Any view         }         label: {           TestView(geometryProxy: geometryProxy)               .buttonStyle(.plain)                .padding(.leading, 8)         }         .buttonStyle(.plain)         .padding(.leading, 0)       }     }   } } And my test view looks like this struct TestView: View { var geometryProxy: GeometryProxy       var body: some View {     HStack {       Text("Some text here extra space on right")         .frame(maxWidth: geometryProxy.size.width * 0.85, alignment: .leading)       HStack {         Text("9.4")           .lineLimit(2)           .padding(8)       }       .frame(maxWidth: geometryProxy.size.width * 0.15)       .padding([.top, .bottom], 4)     }   } } Because there is some gap between the text "Some text here extra space on right" and "9.4", when that area gets tapped nothing happens. NavigationLink will only work when any text is tapped. Why is that? What is lacking here that will make any view in the TestView cllickable, including empty space.
2
0
1.1k
Oct ’22
"Invalid top-level type in JSON write"
I have seen posts about this error message but i am making a new one because my case is different. the json is valid. so i have no clue why it gives out this error. Sample json here i removed the forward slash so it will be clean since when i do a print() it always shows as "key": "value" this is the code i use to convert it to data then it gives out that error. i checked the json in online json validator sites and it's good. let string = the json string from the url above let jsonObject = try? JSONSerialization.data(withJSONObject: string, options: []) It is supposed to be a json array. any idea why the error?
2
0
3.1k
Nov ’22
How To Loop Video In SwiftUI
Anyone got sample code for using AVPlayerLooper in SwiftUI? Code i see are not for SwiftUI I only wish to have the option to turn on auto-repeat on and off. I could use AVQueuePlayer as replacement for AVPlayer but it does not have a way to loop. So these are what I am looking for Loop video in SwiftUI If Using AVPlayerLooper, it has a disableLooping() function (possibly to turn off looping). But how can it be turned back on? There is no enableLooping(). Or does it need a new instance? since i am new to iOS, the code there are all linked to no swift ui code so I have no clue (sorry) the equivalent of them in SwiftUI Thoughts?
2
0
4.3k
Mar ’23
Stepper is still buggy?
Anyone experienced this. My goal is to use AppStorage to save the value in the stepper but the behavior is whaked. Buttons get disabled even when theyre not at both ends of the array if i use State, it works a bit better but the buttons only get disabled after the 2nd or 3rd try when the index is either 0 or at the end of the array. @AppStorage("index") private var opacity = 0 let options = [40, 50, 70, 80, 90, 100]         Stepper {           Text("\("Opacity") \(options[opacity].description)%")         } onIncrement: {           opacity = opacity + 1           if opacity >= opacity.count {             opacity = opacity.count - 1           }           print("onincrement="+String(opacity))         } onDecrement: {           opacity = opacity - 1           if opacity < 0 {             opacity = 0           }           print("ondecrement="+String(opacity))         } The sample code looks simple. I am not sure what else I could be missing?
2
0
583
Nov ’22
Is it possible to rebuild overlay only and not the body?
Hi. I am not sure how to do this case scenario I am having. Please see sample code @State private var tileLayers: [GMSURLTileLayer]?    @State private var isShowingLegend = true     var body: some View {     MapView(       tileLayers: tileLayers     )     .task {       setupMenuItems()               if load {         load = false         processRainWatcher()       }     }     .overlay(       isShowingLegend ?         HStack {           RainWatcherLegendView()           Spacer()         }         .transition(.move(edge: isShowingLegend ? .leading : .trailing))       : nil,       alignment: .bottom     ) } This is not a working code. But i wish to ask suggestion. because the overlay shows a legend view. If isShowingLegend.toggle() is triggered, it animates sliding in and out of the screen. Problem mis, everything gets rebuilt. so the tile layer in the MapView() gets re-rendered again. is there a way not to let this happen, but still let the legend animate sliding in and out? or perhaps pass a binding variable to the legend view? and do the animation in that view? so that the parent view does not get rebuilt? thoughts?
2
0
750
Dec ’22
Attempting to store bytes of data in CFPreferences/NSUserDefaults on this platform is invalid
I am not really sure what is the cause but this happens if i overwrite values. if i clear the app from scratch and store them all it works ok. Thoughts? my exact message is Attempting to store >= 4194304 bytes of data in CFPreferences/NSUserDefaults on this platform is invalid Description of keys being set: holocene_update: string value, approximate encoded size: 25 Description of keys already present: holocene: string value, approximate encoded size: 5216090 GMSMapsUserClientZwiebackCookie: string value, approximate encoded size: 175 GMSMapsUserCookie: data value, size: 48 kGMSServerVersionMetadataTrackerSavedServerVersionMetadataKey: data value, size: 30 holocene_update: string value, approximate encoded size: 25 com.google.Maps.GMSCoreLastVersion: string value, approximate encoded size: 6 cyclone_alert_last: string value, approximate encoded size: 5 com.google.Maps.GMSSDKLastVersion: string value, approximate encoded size: 5 GMSMapsUserClientCohort: string value, approximate encoded size: 4 kGMSMapsUserClientLegalCountry: string value, approximate encoded size: 2 ... Total keys: 12 - Average approximate value size: 434699 bytes 2022-12-19 17:49:08.777899+0800 PH Weather And Earthquake Updates[31305:4102377] [User Defaults] CFPrefsPlistSource<0x600002b80980> (Domain: ************, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null), Contents Need Refresh: No): Transitioning into direct mode Is there a way to increase this limit? I need to store data.
2
0
918
Dec ’22
How To Declare Optional Parameter Function
I have a function like this @ViewBuilder   func addInfo(_ text: String, action: (() -> Void)?, addDivider: Bool = true, centerAlignment: Bool = false, isBold: Bool = false) -> some View {     VStack {       Text(text)         .fontWeight(isBold ? .bold : .regular)         .frame(maxWidth: .infinity, alignment: centerAlignment ? .center : .leading)         .padding([.leading, .trailing], 10)         .padding([.top, .bottom], 5)         .contentShape(Rectangle())         .onTapGesture {           if let action {             action()           }         }       if addDivider {         Divider()       }     }   } I do not understand why i get a compile error Expected type after '->' if i call the function like this addInfo("Sample", action: () -> Void {             }) The error points to Void. What is the correct way to do this? Please advise.
2
0
827
Dec ’22