Post

Replies

Boosts

Views

Activity

Reply to HELP! I don't understand Loops.
It is in fact pretty simple. The best is to try in playground The basic loop is the for loop, where you iterate over elements.  1. Index are just sequential index in a range for i in 0...9 { print(i) } You could define this range in a var var zeroToNine = 0...9 for i in zeroToNine { print(i) } Range can come in other flavours, as open range: for i in 0 ..< 10 { print(i) } This is useful when you iterate on the elements in an array let myArray = ["A", "B", "C", "D", "E", "F"] for i in 0..<myArray.count { print(i) } In those examples, you used the index i. But in some cases you don't need: for i in 0 ..< 10 { print("Hello") // i is not used } Then you don't need to declare i for _ in 0 ..< 10 { print("Hello") // i is not used }  2. You can also iterate directly on elements in an Array (in fact on elements of any collection) let myArray = ["A", "B", "C", "D", "E", "F"] for item in myArray { print(item) } This is equivalent to let myArray = ["A", "B", "C", "D", "E", "F"] for i in 0..<myArray.count { print(myArray[i]) }  3. If you don't want to use all items, but just some, use stride for oddNum in stride(from: 1, to: 5, by: 2) {   print(oddNum) } There are other loops very useful when you have to test a condition: do while  4. repeat while, to test condition at the end var nn = 0 repeat { print(nn) nn += 2 //take input from standard IO into variable n } while nn < 10  5. while where you test at start, before executing the code in brackets var nn = 0 while nn < 10 { print(nn) nn += 2 //take input from standard IO into variable n } See the subtle difference between 4 and 5 by testing the following: var nn = 10 repeat { print(nn) nn += 2 } while nn < 10 nn = 10 while nn < 10 { print(nn) nn += 2 } Hope that helps.
Topic: Programming Languages SubTopic: Swift Tags:
Aug ’21
Reply to How to read a local value correctly both in Combine's sink scope and in the scope that Combine's sink was defined?
What you show in the log does not match exactly the code. I would expect: globalNum: 1 completion: ($0) // Is it ? localNum in caller of .sink: 1 received: Optional("") // But here $0 is always "" local value: (localNum) // is it ? localNum in .sink: 1  Where do you get unexpected result ? Here ?           print("local value: \(localNum)")
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’21
Reply to XCode console flooded with "Invalid timestamps for HID response delay" messages on a hello world app
I tested a simple MacOS app, with storyboard, with Xcode 13.0ß4, with MacOS11.5. I do not get those messages, just a few Metal warnings : 2021-08-01 18:12:49.944683+0200 Simple MacOS test[11072:1323345] Metal API Validation Enabled 2021-08-01 18:12:50.073140+0200 Simple MacOS test[11072:1323345] MTLIOAccelDevice bad MetalPluginClassName property (null) 2021-08-01 18:12:50.074332+0200 Simple MacOS test[11072:1323345] +[MTLIOAccelDevice registerDevices]: Zero Metal services found I found references to the problem since 2017 when using Cordova. Is it your case ? h t t p s : / / github.com/immersive-web/webvr-polyfill/issues/201 Otherwise, that could mean that you have timestamps out of sync. Do you have some antivirus installed ? Hope this could help: https://discussions.apple.com/thread/251314646
Aug ’21
Reply to How to compare dates from two view controllers (Core Data and Swift)?
Code is not formatted in comments, making it hard ro read. 1. let model = Calendar(context: context) 2. print(model.dateSaved) 3. if model.dateSaved == dateFromFirstView { 4. print("dates are identical ",model.dateSaved," ",dateFromFirstView) 5. } else { 6. print("dates are not identical ",model.dateSaved," ",dateFromFirstView) 7. } 8. 9. @IBAction func datePickerChanged(_ sender: Any) { 10. let dateFormatter = DateFormatter() 11. dateFormatter.dateFormat = "dd-MM-YYYY" 12. dateFromFirstView = dateFormatter.string(from: datePicker.date) 13. } 14. 15. @IBAction func datePickerChanged(_ sender: Any) { 16. let dateFormatter = DateFormatter() 17. dateFormatter.dateFormat = "dd-MM-YYYY" 18. strDate = dateFormatter.string(from: datePicker.date) 19. print(strDate) 20. } So model is a variable for the Entity, dateSaved is an attribute of the entity and it is a string (as I use a date formatter that converts the date from a date picker into a string). dateFromFirstView is also a string. The thing is that I added a print but the if else statement only gets triggered once so if I change the date on the datePicker the if else statement doesn't get triggered.  Then I store the strDate as dateSaved in Core Data. I want the if else statement to get triggered whenever I select a new date on the date picker but it only works one time. Some questions about this code: datePickerChanged is defined twice. Is it just a copy and paste error ? In which is the code from lines 1 to 7 ? If it is in viewDidload, that's normal to be called only once. What you could do is keep model as a global in your class keep dateFromFirstView as a global in your class create a func to test: func compareDates() { if model.dateSaved == dateFromFirstView { print("dates are identical ", model.dateSaved," ", dateFromFirstView) } else { print("dates are not identical ", model.dateSaved," ", dateFromFirstView) } } replace lines 3 to 7 with compareDates() call it in datePickerChanged @IBAction func datePickerChanged(_ sender: Any) { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "dd-MM-YYYY" dateFromFirstView = dateFormatter.string(from: datePicker.date) compareDates() }
Topic: Programming Languages SubTopic: Swift Tags:
Aug ’21