Post

Replies

Boosts

Views

Activity

Reply to 'height' is inaccessible due to 'internal' protection level error
Try to change with this to see what you get: extension UIImage { convenience init?(bitmap: Bitmap) { let alphaInfo = CGImageAlphaInfo.premultipliedLast let bytesPerPixel = MemoryLayout<Color>.size let bytesPerRow = bitmap.width * bytesPerPixel let theHeight = bitmap.height // To have a local value guard let providerRef = CGDataProvider(data: Data( bytes: bitmap.pixels, count: theHeight * bytesPerRow ) as CFData) else { return nil }
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’21
Reply to 'height' is inaccessible due to 'internal' protection level error
Could you show how Color is defined ? I tested, replacing Color by Int. I get an error on line     self.init(bitmap: cgImage) Cannot convert value of type 'CGImage' to expected argument type 'Bitmap' Which is logic, as cgImage is CGImage and not Bitmap In addition, you call the convenience initialiser within itself… I must be missing something Try to add, in public struct Bitmap ( }      public init() {} See here for details: https://stackoverflow.com/questions/40859139/initializer-is-inaccessable-due-to-internal-protection-level
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’21
Reply to Send notification to iPhone from Apple Watch in Swift
Yes. I did it this way: create an IBAction (that I linked to a tap on a label). Here it is to ask for a sync between iPhone and Watch synced done is displayed on the Watch in:     @IBOutlet weak var autonomyMessage: WKInterfaceLabel!   IBAction: @IBAction func tapOnConnectionToIphone(_ sender: Any) { autonomyMessage.setText("") let session = WCSession.default if session.isReachable { session.sendMessage(["request" : "sync"], replyHandler: { (response) in self.autonomyMessage.setText(NSLocalizedString("Received!", comment: "SessionInterfaceController")) }, errorHandler: { (error) in self.autonomyMessage.setText(NSLocalizedString("Not received!", comment: "SessionInterfaceController")) }) autonomyMessage.setText(NSLocalizedString("Sent!", comment: "SessionInterfaceController")) } else { autonomyMessage.setText(NSLocalizedString("Not sent!", comment: "SessionInterfaceController")) } autonomyMessage.setHidden(false) } On the iPhone side: func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) { if message["request"] as? String == "sync" { // proceed as needed } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21
Reply to How do I display a value in a label (the value keeps changing/updating)
I do not see where you defined the label to write the coordinates. So you should create a Label in the view (in IB) and connect it to its IBOutlet (I call it positionLabel) There is another big problem : userDistance() func is never called. So, I had to guess you want to call it on pin. I refactored your code a little. Finally, func names should start with lowercase and have more explicit names. First, disconnect the button from the IBAction change the name Reconnect Replace @IBAction func Button(_ sender: UIButton) { with (for instance) @IBAction func buttonTapped(_ sender: UIButton) { Here is the modified code. If problem, please tell exactly where 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. 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. 71. if let distance = userDistance(from: pin) { // >>> I call it here 72. positionLabel.text = String(distance) 73. 74. } 75. } 76. } 77. 78. @IBAction func buttonTapped(_ sender: UIButton) { 79. 80. UIApplication.shared.open(URL(string: "https://www.gps-coordinates.net")! as URL, options: [:], completionHandler: nil) 81. 82. } 83. }
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’21
Reply to 'height' is inaccessible due to 'internal' protection level error
You should not post code in comments, they mess it. So I had to rebuild it. Note: I do find it confusing to name the class and the struct inside by the same name… Why do you declare public ? And not simply extension Bitmap {} for instance ? You did not answer my other questions : Cannot convert value of type 'CGImage' to expected argument type 'Bitmap' Which is logic, as cgImage is CGImage and not Bitmap In addition, you call the convenience initialiser within itself… I must be missing something import UIKit class Color: UIViewController { public struct Color { public var r, g, b, a: UInt8 public init(r: UInt8, g: UInt8, b: UInt8, a: UInt8 = 255) { self.r = r self.g = g self.b = b self.a = a } } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } } extension Color { static let clear = Color(r: 0, g: 0, b: 0, a: 0) static let black = Color(r: 0, g: 0, b: 0) static let white = Color(r: 255, g: 255, b: 255) static let gray = Color(r: 192, g: 192, b: 192) static let red = Color(r: 255, g: 0, b: 0) static let green = Color(r: 0, g: 255, b: 0) static let blue = Color(r: 0, g: 0, b: 255) }
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’21
Reply to How do I display a value in a label (the value keeps changing/updating)
the label isn't showing the value It doesn't show when ? Maybe render() func is never called. To check, add line 55 : print("Render") and line 73: print("distance", distance) error message in AppDelegate says,"Thread 1: "-[Project.ViewController buttonTapped:]: unrecognized selector sent to instance 0x133e08da0"" You probably did not do what I told you: First, disconnect the button from the IBAction change the name Reconnect So, now, in IB, select the button and open the connections inspector. Uncheck the link to IBAction. It should look like this: click on the small x to disconnect. Now, control drag from the small circle on the left of IBAction buttonTapped to the button to reconnect. And do an option clean Build folder. Tell what you get after those changes.
Topic: Programming Languages SubTopic: Swift Tags:
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:
Jun ’21