Post

Replies

Boosts

Views

Activity

Reply to Need help converting JSON objects from double & floats to strings
As already noted, when you need to convert Float/Double to String for assignment, you need to do it in the right hand side. So, this would be the minimal code: func connectLabels(loc: String, temp: Double, lat: Float, long: Float) { locationLabel.text = loc tempInFLabel.text = String(temp) latLabel.text = String(lat) longLabel.text = String(long) } But if you want to make your app sort of practical in the future, you should better use NumberFormatter for user readable values. Also, some users want temperatures shown in the unit they accustomed to, we use Celsius usually. So, some practical code would be something like this: static let tempFormatter: MeasurementFormatter = { let mf = MeasurementFormatter() //You may want to modify these as you like... mf.unitStyle = .medium mf.numberFormatter.maximumFractionDigits = 1 return mf }() static let latLongFormatter: NumberFormatter = { let nf = NumberFormatter() //You may want to modify these as you like... nf.usesSignificantDigits = true nf.minimumSignificantDigits = 5 nf.maximumSignificantDigits = 5 return nf }() func connectLabels(loc: String, temp: Double, lat: Float, long: Float) { locationLabel.text = loc let tempMeasurement = Measurement(value: temp, unit: UnitTemperature.fahrenheit) tempLabel.text = MyWeatherTableViewCell.tempFormatter.string(from: tempMeasurement) latLabel.text = MyWeatherTableViewCell.latLongFormatter.string(from: lat as NSNumber) longLabel.text = MyWeatherTableViewCell.latLongFormatter.string(from: long as NSNumber) }
Topic: Programming Languages SubTopic: Swift Tags:
May ’21
Reply to How can I auto-populate a Text Field once a button is selected?
You declare a local constant within offlineListing(_:), but it is not used and the value is disposed at the end of the method. (You would have seen a waring Initialization of immutable value 'productURLField' was never used; consider replacing with assignment to '_' or removing it, which should not be ignored.) Even if the local constant and the IBOutlet have exactly the same identifier, they are two different things and have nothing to do with each other. You may want to do something like this: @IBAction func offlineListing(_ sender: Any) { productURLField.text = "N/A" }
Topic: UI Frameworks SubTopic: UIKit Tags:
May ’21
Reply to Toolbar buttons become unusable after closing the sheet.
Can you try something like this? struct ContentView: View { @State private var showingSheet = false var body: some View { NavigationView { Text("1st View") .toolbar { ToolbarItemGroup(placement: .navigationBarTrailing) { Button("Button1") { showingSheet = true } Button("Button2") { showingSheet = true } } } .sheet(isPresented: $showingSheet) { Sheet() } } } } Or this: struct ContentView2: View { @State private var showingSheet = false var body: some View { NavigationView { Text("1st View") .toolbar { ToolbarItem(placement: .navigationBarTrailing) { Button("Button1") { showingSheet = true } } ToolbarItem(placement: .navigationBarTrailing) { Button("Button2") { showingSheet = true } } } .sheet(isPresented: $showingSheet) { Sheet() } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’21
Reply to Get data from view after it gets added on screen using ForEach
Thanks for showing the refactored code. It compiled fine. I needed to add enableButtonBool = true, but it was a great help to guess what you want to do. (I still do not understand some parts of the code, but it may be better to show what I guessed.) I may be mistaking something, but you may want to do something like this: import SwiftUI struct ChecklistModel: Codable, Hashable { var description, remarks, status: String? } struct CheckListCard: View { @Binding var checkListData : ChecklistModel @State var statusList: [String] = [] @Binding var enableButtonBool: Bool var body: some View { let currentStatus = Binding<String>( get: {checkListData.status ?? ""}, set: {checkListData.status = $0}) let currentRemark = Binding<String>( get: {checkListData.remarks ?? ""}, set: {checkListData.remarks = $0}) VStack{ Text("Description").padding() .onAppear(){ addData() } if let description = checkListData.description { Text(description) .padding() } Text("Status").padding() if checkListData.status != nil { Picker("Status", selection: currentStatus) { ForEach(statusList, id: \.self){ status in Text(status) } }.pickerStyle(SegmentedPickerStyle()) .padding() .disabled(!enableButtonBool) } Text("Remarks") if checkListData.remarks != nil { TextField("Remarks", text: currentRemark) .disabled(!enableButtonBool) .padding() .background(Color(.white)) .cornerRadius(8) .accentColor(.gray) } else { TextField("Remarks", text: currentRemark) .disabled(!enableButtonBool) .padding() .background(Color(.white)) .cornerRadius(8) } } .padding() .background(Color("light_gray")) .foregroundColor(.black) .cornerRadius(8) .shadow(radius: 10) .padding() } func addData() { statusList.append("Yes") statusList.append("No") statusList.append("NA") if let status = checkListData.status { statusList.append(status) //<- Why this is needed? } } } struct CheckListSheet: View { @State var taskId: Int @State var checklistResponse: [ChecklistModel] = [ChecklistModel()] @State var enableButtonBool: Bool = false @State var statusList: [String] = [] var body: some View { Text("Checklist Items") .padding() .font(.title) ScrollView { ForEach(checklistResponse.indices, id:\.self) { index in CheckListCard(checkListData: $checklistResponse[index], enableButtonBool: $enableButtonBool) } if enableButtonBool { Button("Update") { updateCheckList() } } } .onAppear(){ getChecklists() } } func updateCheckList() { print("this is the checklistresponse \(checklistResponse)") } func getChecklists() { checklistResponse = [ ChecklistModel(description: "ist item", remarks: nil, status: "Yes"), ChecklistModel(description: "2nd item", remarks: "Fine", status: "no") ] enableButtonBool = true } }
Topic: Programming Languages SubTopic: Swift Tags:
May ’21
Reply to Removal of deprecated Carbon APIs
What I want to know is, in which macOS version will it be removed? As far as I know, Apple would not announce removal plans of deprecated APIs. There were some exceptions, such as UIWebView which was very widely used in iOS apps. Any idea on this? Unless the API is so widely used as above, Apple might abruptly remove it in some beta which would be released a few weeks before released version. If a few weeks (it might be less!) is too short for you, you should better start migrating your app immediately.
Topic: App & System Services SubTopic: Core OS Tags:
May ’21
Reply to Adding MKCircle to the annotations on the map - Problem with setting CLLocationCoordinate2D
I guess all the properties and methods you have shown belongs to a class -- some UIViewController. You should better include the header of the class. And the definition of szczyty is not consistent with the struct definition Szczyt. I assume the struct definition is right and the initial value of szczyty needs to be modified. As shown in the error message, you cannot use an expression containing instance properties or methods as an initial value of an instance property, in Swift. One possible way is to set the value of circles in viewDidLoad(): private(set) var circles: [MKCircle]! override func viewDidLoad() { super.viewDidLoad() circles = szczyty.map { MKCircle(center: $0.coordinate, radius: 100) } //... }
Topic: Programming Languages SubTopic: Swift Tags:
May ’21
Reply to Need help w/escaping closures for API call
Can you be more specific? What is the current issue with your code? Does it cause some build-time errors and cannot run? Or it runs but causes some runtime errors? Or else, it runs without errors but shows unexpected results? And you should better include all the relevant types when showing your code. What are APIResults and MappedResults? Please show the definitions of them. One more, it is not recommended to use an initializer exactly the same as existing type names, as in your Line 47. It should be lowercased mappedResults.
Topic: Programming Languages SubTopic: Swift Tags:
May ’21