Post

Replies

Boosts

Views

Activity

Reply to Read in csv file and insert data into Class object
It could be better to have PlayerData as a struct and not a class (no more init() needed). But that's your choice and depends on what you want to do with it. When you read the CSV file, put it into an array of Strings, by reading each line: If data is the String of your complete CSV file, build the array like this: var linesOfFile : [String] let multiLineString = data let newlineChars = CharacterSet.newlines linesOfFile = multiLineString.components(separatedBy: newlineChars).filter{!$0.isEmpty} Now, read each line: var allPlayers : [PlayerData] = [] let separatorMark = "," for line in linesOfFile { let aPlayerData = line.split(separator: separatorMark).map() { String($0) } // separatorMark is "," // You get ["1", "1", "0", "Christian McCaffrey", "CAR", "RB", "13", "TRUE"] // As you know the meaning of each you can simply fill the struct: let num = Int(aPlayerData[0]) ?? 0 let numPosPick = Int(aPlayerData[1]) ?? -1 // May be default should be other than -1 ? let numRookie = Int(aPlayerData[2]) ?? -1 let name = aPlayerData[3] let team = aPlayerData[4] let position = aPlayerData[5] let byeWeek = Int(aPlayerData[6]) ?? 0 let isTopPlayer = aPlayerData[7] == "TRUE" let isRookie = false // I DO NOT SEE IT IN CSV ; maybe it is computed from numRookie ? = numRookie > 0 let player = PlayerData(num: num, numPosPick: numPosPick, numRookie: numRookie, name: name, team: team, position: position, byeWeek: byeWeek, isTopPlayer: isTopPlayer, isRookie: isRookie) allPlayers.append(player) } You could add a description var in your class as: var description : String { let topPlayer = self.isTopPlayer ? "is top player" : "plays"         return "\(self.name) \(topPlayer) in team \(team) at position: \(position) with rank: \(num), numPosPick: \(numPosPick) and numRookie: \(numRookie). His byeWeek is \(byeWeek)" } Calling print(allPlayers[0].description) yields Christian McCaffrey is top player in team CAR at position: RB with rank: 1, numPosPick: 1 and numRookie: 0. His byeWeek is 13
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’21
Reply to Test
Thanks in any case. A last point on this: if you close the thread by marking your own answer as the correct answer, you'll be credited 15 points (you didn't need them 😀) Is it a bug or intentional ? It is bizarre to be able to auto score…
Jun ’21
Reply to Most wanted Xcode features…
Wanted Feature : Allow to insert pictures in Xcode text files. Pain point: When writing code, it would be often useful to include a graphics or a picture in the comments (such as a func header). For instance, when dealing with trigonometric computation, a picture allows to precisely describe what each numeric value is. Presently, one need to keep in a separate file and reference it in the comment. Feasibility: That has been recently done in the Forum. Submitted as FB9182929
Jun ’21
Reply to Problem when using UIButton to switch view controllers in tabbed Application
just find the tabBarController that leads to FireMapViewController self.tabBarController So, if I understand correctly what you look for, try this: @IBAction func fireMapButton(_ sender: UIButton) { // It is often better to give the precise class even though it will have no effect here print("Fire Map Button tapped") let tabBarController = self.tabBarController tabBarController?.selectedIndex = 2 self.dismiss(animated: true, completion: nil) }
Topic: UI Frameworks SubTopic: UIKit Tags:
Jun ’21
Reply to How do I display a value in a label (the value keeps changing/updating)
If you do not explain more clearly the set up, it is nearly impossible to help. the button in this project has nothing to do with the label  But you say you have an error message when button is tapped. Which button is it ? error message in AppDelegate says,"Thread 1: "-[Project.ViewController buttonTapped:]: unrecognized selector sent to instance 0x133e08da0"" When does the location update ? When user moves ? On simulator or on device ? How much distance does it move ? If less than a few meters, that may not be enough to detect. Could you instrument the code with some print statements (lines 32, 70, 73) And tell exactly what you get. 1. import MapKit 2. import UIKit 3. import CoreLocation 4. import UserNotifications 5. 6. class ViewController: UIViewController, CLLocationManagerDelegate { 7. 8. @IBOutlet var mapView: MKMapView! 9. @IBOutlet private weak var positionLabel : UILabel! // <<< add this and connect to the label in IB 10. 11. let manager = CLLocationManager() 12. 13. override func viewDidLoad() { 14. 15. super.viewDidLoad() 16. 17. // Do any additional setup after loading the view. 18. 19. } 20. 21. override func viewDidAppear(_ animated: Bool) { 22. 23. super.viewDidAppear(animated) 24. 25. manager.desiredAccuracy = kCLLocationAccuracyBest // This should better be done in viewDidLoad 26. manager.delegate = self // This should better be done in viewDidLoad 27. manager.requestWhenInUseAuthorization() 28. manager.startUpdatingLocation() 29. } 30. 31. func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 32. print(#function, "didUpdateLocations") // To see if this is called 33. if let location = locations.first{ 34. manager.stopUpdatingLocation() 35. render(location) 36. } 37. 38. func userDistance(from point: MKPointAnnotation) -> Double? { // >>> I took it outside for better readability 39. 40. guard let userLocation = mapView.userLocation.location else { 41. return nil // User location unknown! 42. } 43. 44. let pointLocation = CLLocation( 45. latitude: point.coordinate.latitude, 46. longitude: point.coordinate.longitude 47. ) 48. 49. let value = userLocation.distance(from: pointLocation) // replace var by let, as never changed 50. return value 51. } 52. 53. 54. func render(_ location: CLLocation){ 55. 56. let coordinate = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude) 57. let span = MKCoordinateSpan (latitudeDelta: 0.1, longitudeDelta: 0.1) 58. let region = MKCoordinateRegion(center: coordinate, span: span) 59. mapView.setRegion(region, animated: true) 60. 61. mapView.showsUserLocation = true 62. 63. let pin = MKPointAnnotation() 64. 65. pin.coordinate = coordinate 66. pin.title = "your home" 67. pin.subtitle = "Location" 68. 69. mapView.addAnnotation(pin) 70. print(#function, "userDistance(from: pin) ", userDistance(from: pin) ) 71. if let distance = userDistance(from: pin) { // >>> I call it here 72. positionLabel.text = String(distance) 73. print(#function, "distance", String(distance)) 74. } 75. } 76. }
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’21
Reply to How can I change or create my Developer Forums username?
No, you cannot change it as you have defined it as :  canbalkya
Replies
Boosts
Views
Activity
Jun ’21
Reply to 'height' is inaccessible due to 'internal' protection level error
Update: I rewrote the last line in UIImage+Bitmap.swift to self.init(cgImage: cgImage) and it worked. Appreciate the help. So, you don't call the convenience initialiser itself anymore. Thanks for the feedback, don't forget to close the thread now.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to Unable to install macOS Monterey (macOS 12)
Hope you MB is a spare computer, not your main. It is always very risky to install a beta OS on the main development computer.
Topic: App & System Services SubTopic: Core OS Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to Read in csv file and insert data into Class object
It could be better to have PlayerData as a struct and not a class (no more init() needed). But that's your choice and depends on what you want to do with it. When you read the CSV file, put it into an array of Strings, by reading each line: If data is the String of your complete CSV file, build the array like this: var linesOfFile : [String] let multiLineString = data let newlineChars = CharacterSet.newlines linesOfFile = multiLineString.components(separatedBy: newlineChars).filter{!$0.isEmpty} Now, read each line: var allPlayers : [PlayerData] = [] let separatorMark = "," for line in linesOfFile { let aPlayerData = line.split(separator: separatorMark).map() { String($0) } // separatorMark is "," // You get ["1", "1", "0", "Christian McCaffrey", "CAR", "RB", "13", "TRUE"] // As you know the meaning of each you can simply fill the struct: let num = Int(aPlayerData[0]) ?? 0 let numPosPick = Int(aPlayerData[1]) ?? -1 // May be default should be other than -1 ? let numRookie = Int(aPlayerData[2]) ?? -1 let name = aPlayerData[3] let team = aPlayerData[4] let position = aPlayerData[5] let byeWeek = Int(aPlayerData[6]) ?? 0 let isTopPlayer = aPlayerData[7] == "TRUE" let isRookie = false // I DO NOT SEE IT IN CSV ; maybe it is computed from numRookie ? = numRookie > 0 let player = PlayerData(num: num, numPosPick: numPosPick, numRookie: numRookie, name: name, team: team, position: position, byeWeek: byeWeek, isTopPlayer: isTopPlayer, isRookie: isRookie) allPlayers.append(player) } You could add a description var in your class as: var description : String { let topPlayer = self.isTopPlayer ? "is top player" : "plays"         return "\(self.name) \(topPlayer) in team \(team) at position: \(position) with rank: \(num), numPosPick: \(numPosPick) and numRookie: \(numRookie). His byeWeek is \(byeWeek)" } Calling print(allPlayers[0].description) yields Christian McCaffrey is top player in team CAR at position: RB with rank: 1, numPosPick: 1 and numRookie: 0. His byeWeek is 13
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to Filter in ForEach SwiftUI
Where is the filter on valori ? How do you want to filter on date ? Matching a date, in a range ?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to Send notification to iPhone from Apple Watch in Swift
In UIKit case, iPhone side goes into the viewController that handles the message (in my case, the initial VC)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to Form vs Vstack for SwiftUI for iOS
List is often buggy or at least imprévisible… Form may be a super List. So, Form may (in present versions at least) suited for very simple forms. But if you want to keep some control of what's happening, VStack is probably safer.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to Learn to Code 2
Could you copy here the question ? And tell what you don't know how to do ?
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to Learn to Code 2
You leave other threads without feedback even if you get replies. You should respond before posting a new question.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to What is a scope declaration in swift programming language ?
Didn't you ask the same question here and get an answer ? https://developer.apple.com/forums/thread/679685 Did you add import AVFoundation May have a look here: h t t p s : / / medium. com/@gaspard.rosay/create-a-camera-app-with-swiftui-60876fcb9118
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to Lines numbering in Code Block
When I look at your answer in this thread, I do not see line numbers: I'll wait the return from your bug report.
Replies
Boosts
Views
Activity
Jun ’21
Reply to Test
Thanks in any case. A last point on this: if you close the thread by marking your own answer as the correct answer, you'll be credited 15 points (you didn't need them 😀) Is it a bug or intentional ? It is bizarre to be able to auto score…
Replies
Boosts
Views
Activity
Jun ’21
Reply to Most wanted Xcode features…
Wanted Feature : Allow to insert pictures in Xcode text files. Pain point: When writing code, it would be often useful to include a graphics or a picture in the comments (such as a func header). For instance, when dealing with trigonometric computation, a picture allows to precisely describe what each numeric value is. Presently, one need to keep in a separate file and reference it in the comment. Feasibility: That has been recently done in the Forum. Submitted as FB9182929
Replies
Boosts
Views
Activity
Jun ’21
Reply to Problem when using UIButton to switch view controllers in tabbed Application
just find the tabBarController that leads to FireMapViewController self.tabBarController So, if I understand correctly what you look for, try this: @IBAction func fireMapButton(_ sender: UIButton) { // It is often better to give the precise class even though it will have no effect here print("Fire Map Button tapped") let tabBarController = self.tabBarController tabBarController?.selectedIndex = 2 self.dismiss(animated: true, completion: nil) }
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to How do I display a value in a label (the value keeps changing/updating)
If you do not explain more clearly the set up, it is nearly impossible to help. the button in this project has nothing to do with the label  But you say you have an error message when button is tapped. Which button is it ? error message in AppDelegate says,"Thread 1: "-[Project.ViewController buttonTapped:]: unrecognized selector sent to instance 0x133e08da0"" When does the location update ? When user moves ? On simulator or on device ? How much distance does it move ? If less than a few meters, that may not be enough to detect. Could you instrument the code with some print statements (lines 32, 70, 73) And tell exactly what you get. 1. import MapKit 2. import UIKit 3. import CoreLocation 4. import UserNotifications 5. 6. class ViewController: UIViewController, CLLocationManagerDelegate { 7. 8. @IBOutlet var mapView: MKMapView! 9. @IBOutlet private weak var positionLabel : UILabel! // <<< add this and connect to the label in IB 10. 11. let manager = CLLocationManager() 12. 13. override func viewDidLoad() { 14. 15. super.viewDidLoad() 16. 17. // Do any additional setup after loading the view. 18. 19. } 20. 21. override func viewDidAppear(_ animated: Bool) { 22. 23. super.viewDidAppear(animated) 24. 25. manager.desiredAccuracy = kCLLocationAccuracyBest // This should better be done in viewDidLoad 26. manager.delegate = self // This should better be done in viewDidLoad 27. manager.requestWhenInUseAuthorization() 28. manager.startUpdatingLocation() 29. } 30. 31. func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 32. print(#function, "didUpdateLocations") // To see if this is called 33. if let location = locations.first{ 34. manager.stopUpdatingLocation() 35. render(location) 36. } 37. 38. func userDistance(from point: MKPointAnnotation) -> Double? { // >>> I took it outside for better readability 39. 40. guard let userLocation = mapView.userLocation.location else { 41. return nil // User location unknown! 42. } 43. 44. let pointLocation = CLLocation( 45. latitude: point.coordinate.latitude, 46. longitude: point.coordinate.longitude 47. ) 48. 49. let value = userLocation.distance(from: pointLocation) // replace var by let, as never changed 50. return value 51. } 52. 53. 54. func render(_ location: CLLocation){ 55. 56. let coordinate = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude) 57. let span = MKCoordinateSpan (latitudeDelta: 0.1, longitudeDelta: 0.1) 58. let region = MKCoordinateRegion(center: coordinate, span: span) 59. mapView.setRegion(region, animated: true) 60. 61. mapView.showsUserLocation = true 62. 63. let pin = MKPointAnnotation() 64. 65. pin.coordinate = coordinate 66. pin.title = "your home" 67. pin.subtitle = "Location" 68. 69. mapView.addAnnotation(pin) 70. print(#function, "userDistance(from: pin) ", userDistance(from: pin) ) 71. if let distance = userDistance(from: pin) { // >>> I call it here 72. positionLabel.text = String(distance) 73. print(#function, "distance", String(distance)) 74. } 75. } 76. }
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jun ’21