Post

Replies

Boosts

Views

Activity

Reply to TabBar
Now I need to switch TabBar items, using the buttons on the View. How can I do this ? You may need to access the selection of TabView: struct ContentView: View { @State var tabSelection: Int = 1 var body: some View { TabView(selection: $tabSelection) { TabView1View(tabSelection: $tabSelection) .tabItem { Text("TabItem1") } .tag(0) TabView2View(tabSelection: $tabSelection) .tabItem { Text("TabItem2") } .tag(1) } } } struct TabView1View: View { @Binding var tabSelection: Int var body: some View { Button(action: { //call tabItem 2 self.tabSelection = 1 }) { Text("TabItem1") } } } struct TabView2View: View { @Binding var tabSelection: Int var body: some View { Button(action: { //call tabItem 1 self.tabSelection = 0 }) { Text("TabItem2") } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to Value of type "PlaygroundKeyValueStore" has no member "keyValueStore"
Based on other descriptions in the doc, the code sample is a bit outdated. It should be something like this: // Store a value. PlaygroundKeyValueStore.current["animal"] = .string("Llama") // Retreive that same value. var theAnimal: String? = nil if let keyValue = PlaygroundKeyValueStore.current["animal"], case .string(let animalType) = keyValue { theAnimal = animalType } You can send a bug report of this documentation bug using Apple's Feedback Assistant.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to UserDefaults with Struct - TableView not displaying data from saved from UserDefaults?
Please show full code. Fragments of code do not make sense. In which class person, tableView(_:numberOfRowsInSection:) and tableView(_:cellForRowAt:) are defined? How are you filling data into the property person? (Asking about the property person, not about the local variable person.) One more, please use the Code block feature (icon ``) when you want to show some codes.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to What replaces there Tabbed template in Xcode 12?
How do you close a thread? There's no closing features in the dev forums. Better visit the support page - https://developer.apple.com/support/forums/ of the dev forums and learn what would be the right usage. If you think any of the replies (including yours) helped solving your issue, marking it as SOLVED would have similar effect as closing. Please be careful when marking a reply as SOLVED, once marked you cannot unmark it.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to Getting picker selected value with api data
This is my complete code: Far from complete. Simply saying a complete code is a self contained code which can be built and run without any guesses. Sorry, didn't think that you'd need that as well If you could guess what is needed for readers properly, more readers would take time to solve your issue and you would get better responses sooner. You declare ID of MyModel as String, so the type of selection needs to match to it: struct MyView: View { @State var mydata = [MyModel]() @State private var ID: String = "" //- var body: some View { NavigationView { Form { Section { Picker("Pick an item", selection: $ID) { ForEach(mydata, id: \.ID) { mymodel in Text(mymodel.Name) //`name` or `Name` } }.onAppear(perform: loadData) Text("Selected: \(ID)") } } } } //... } Assuming your loadData() works properly. But generally, you should better not dispose error info silently: func loadData() { guard let url = URL(string: "https://mysite.co.uk/api/getmydatalist") else { print("Invalid URL") return } let request = URLRequest(url: url) URLSession.shared.dataTask(with: request) { data, response, error in if let error = error { print(error) return } guard let data = data else { print("data is nil") return } do { let response = try JSONDecoder().decode([MyModel].self, from: data) DispatchQueue.main.async { self.mydata = response } } catch { print(error) } }.resume() }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to How long can we use Xcode 11 for app updates?
It is unclear what the quoted paragraph means: does it mean new applications must be built with Xcode 12, or does it mean that any app update to existing applications must use Xcode 12? It is quite clear. When the term apps submitted to the App Store is used, it means all the apps submitted to the App Store, whether they are new applications or updates to existing applications.
Apr ’21
Reply to How long can we use Xcode 11 for app updates?
So maybe I will not find out until we do a *real* release? I haven't checked how Apple's system works for newly submitted apps, you have an option to try it by yourself with your own risk. But anyway, if you submit your app built with Xcode 11, it is clearly against the Apple's announcement. (You can check old news and find that Apple used new application or updates when needed to distinguish them.) Please share your experience when you submit your update.
Apr ’21
Reply to insert Json file into core Data
The json is build as followed:  {1 : { "title" : "Mini pork pies with piccalilli", "category" : "meat"}, 2 : {"title" : "apple crumble", "category" : "dessert"}} First of all, {1 : { "title" : "Mini pork pies with piccalilli", "category" : "meat"}, 2 : {"title" : "apple crumble", "category" : "dessert"}} is not a valid JSON. If you want to work with Apple's frameworks, you first need to make a valid JSON. One possible form: { "1" : { "title" : "Mini pork pies with piccalilli", "category" : "meat" }, "2" : { "title" : "apple crumble", "category" : "dessert" } } (In valid JSON, the keys need to be JSON string, no number keys are allowed.) Another would be: [ { "title" : "Mini pork pies with piccalilli", "category" : "meat" }, { "title" : "apple crumble", "category" : "dessert" } ] If you were using the latter form, you would directly get an Array from the JSON. So I assume the first one. But in any way, if your JSON actually looks like {1 : { "title" : "Mini pork pies with piccalilli", ..., please create a valid JSON first. Assuming you got the JSON file as my first example, you can write some code to convert it to an Array like this: let jsonText = """ { "1" : { "title" : "Mini pork pies with piccalilli", "category" : "meat" }, "2" : { "title" : "apple crumble", "category" : "dessert" } } """ func jsonTextToArray(_ jsonText: String) - [[String: Any]]? { guard let data = jsonText.data(using: .utf8) else { print("invalid data") return nil } do { guard let json = try JSONSerialization.jsonObject(with: data) as? [String: [String: Any]] else { print("bad format") return nil } return json.sorted {Int($0.key) ?? 0 Int($1.key) ?? 0} .map {$0.value} } catch { print(error) return nil } } if let array = jsonTextToArray(jsonText) { print(array) } else { print("bad jsonText") } //-[["title": Mini pork pies with piccalilli, "category": meat], ["category": dessert, "title": apple crumble]]
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to unrecognized selector sent to instance 0x144b071
Here is the error: I'm not sure what's happening, but the error message is hidden and we cannot see it. I needed to explore the HTML source for it: [CalorieCounter.FirstViewController yourAge:]: unrecognized selector sent to instance 0x144b071 This is very odd, as the selector yourAge: contains a colon. It seems as if iOS is invoking some action method (yourAge(_:) in Swift) linked to some UI component (maybe a UITextField). You once connected Action from the UITextField mistakenly? I guess you removed that mistakenly created action method from code, but you have not removed the connection in the Interface Builder. Open the Connections inspector, then find and remove the action connection showing yourAge: (grouped in Sent Evens).
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to Using Swift playground UIKit, Constraints are not working
An interesting issue. It seems as if iOS (or Playground?) is removing some constraints on the properties after loadView(). Is there any solution to fix this problem? Setting up subviews in viewDidLoad() would fix the issue: class ViewController2: UIViewController { var titleLabel: UILabel! var subLabel: UILabel! override func loadView() { let view = UIView(frame: viewRect) view.backgroundColor = UIColor.white self.view = view } override func viewDidLoad() { super.viewDidLoad() titleLabel = UILabel() titleLabel.translatesAutoresizingMaskIntoConstraints = false titleLabel.text = "title" titleLabel.backgroundColor = .gray view.addSubview(titleLabel) NSLayoutConstraint.activate([ titleLabel.widthAnchor.constraint(equalToConstant: 200), titleLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 100), titleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor) ]) subLabel = UILabel() subLabel.translatesAutoresizingMaskIntoConstraints = false subLabel.text = "title" subLabel.backgroundColor = .red view.addSubview(subLabel) NSLayoutConstraint.activate([ subLabel.widthAnchor.constraint(equalToConstant: 100), subLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 100), subLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor) ]) } } But I do not understand what's happening behind and am not sure if this can be the solution for you.
Topic: UI Frameworks SubTopic: UIKit Tags:
Apr ’21