Post

Replies

Boosts

Views

Activity

Reply to How to integrate data from a web service into an array
That's because dataTask has not completed when you return. Look here to see how to wait until it has completed: https://stackoverflow.com/questions/38952420/swift-wait-until-datataskwithrequest-has-finished-to-call-the-return Write the sendRequest function, func sendRequest (request: NSURLRequest,completion:(NSData?)->()){ NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in if error != nil { return completion(data) } else { return completion(nil) } }.resume() } And use it inside var quotes. The completion will return output
Mar ’25
Reply to Most wanted Xcode features…
Wanted Feature : Get a warning when compiling if an IBOutlet is not connected. Pain point: It may happen to forget a connection and not see it (notably because in some cases, Xcode is mislead to show a connection that does not exist in this project). That's crash at runtime. Feasibility: When using storyboard, Xcode notes when an IBOutlet is connected or not to the storyboard object. So it must be easy to have it detected at compile/build phase.
Mar ’25
Reply to iOS Screenshot prevention Whatsapp
I tested on a WhatsApp conversation and could get a screen capture. What page is this ? If that could help: https://www.reddit.com/r/ios/comments/14yyvzo/can_i_disable_screenshot_detection/?rdt=61137 Note: have you tested to take a photo of the screen with another iPhone ? Does it work ? If so, even if you prevent direct screen capture, there are simple workaround…
Topic: UI Frameworks SubTopic: UIKit Tags:
Mar ’25
Reply to UITextView crash on iOS 18.4 beta
I tested in Swift something similar, without crash on simulator 18.4 let string = "ffi" var style = [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: .regular), NSAttributedString.Key.paragraphStyle: NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle] let attributeString = NSMutableAttributedString(string: string, attributes: style) testField.attributedText = attributeString
Topic: UI Frameworks SubTopic: UIKit Tags:
Mar ’25
Reply to ForEach loop in a Map doesn't work
Welcome to the forum MapMarker is deprecated and required extra parmateres in Map: Deprecated: Use Marker along with Map initializers that take a MapContentBuilder instead. Here is the example from Xcode doc: struct IdentifiablePlace: Identifiable { let id: UUID let location: CLLocationCoordinate2D init(id: UUID = UUID(), lat: Double, long: Double) { self.id = id self.location = CLLocationCoordinate2D( latitude: lat, longitude: long) } } struct PinAnnotationMapView: View { let place: IdentifiablePlace = IdentifiablePlace(lat: 0, long: 0) @State var region: MKCoordinateRegion = MKCoordinateRegion() var body: some View { Map(coordinateRegion: $region, annotationItems: [place]) { place in MapMarker(coordinate: place.location) } } } I tested your code replacing MapMarker by Marker, it works (with a String as first parameter. Map { ForEach(markers) { marker in Marker("test", coordinate: marker.coordinate) // MapMarker } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’25
Reply to How to integrate data from a web service into an array
To further test, I mimicked your JSON, considering it would look like this (each element with an id and a name). They are in fact Dictionaries: [{"id":"First","name":"One"}, {"id":"Second","name":"Two"}, {"id":"Third","name":"Three"}] If so, the test code (I commented out the url part to replace by hardwired data) typealias MyDict = [String: String] // A dictionary is defined as [key:Value] struct Item : Codable { // Need Codable to be used by Decoder var id: String // Identical to the key:value of the JSON (you can later change the name using CodingKeys … var name: String } // ----------- this part for hardwired test -------------- // This to create data by hand ; in your case, data will come from php request let one = Item(id: "First", name:"One") let two = Item(id: "Second", name:"Two") let three = Item(id: "Third", name:"Three") let itemData = [one, two, three] // Here, 3 elements in JSON let data = try JSONEncoder().encode(itemData) // create the data that you'll in fact retrieve from url print("JSON Data", String(data: testJSONData, encoding: .utf8)!) // To check what is in data // ------------------------------------------------------- var quotes: [MyDict] { // back to array of Dict var output: [MyDict] = [] // ----------- 3 lines commented for test with hardwired data -------------- // if let url = URL(string:"https://www.TEST.com/test_connection.php") { // URLSession.shared.dataTask(with: url) { (data, response, error) in // if let data { // data were built with encode, but that would work with url if you uncomment those 3 lines if let json = try? JSONDecoder().decode([MyDict].self, from: data) { output = json } // } // } // } return output } print("quotes", quotes) You get this (once again, dictionaries are not ordered): JSON Data [{"id":"First","name":"One"},{"id":"Second","name":"Two"},{"id":"Third","name":"Three"}] quotes [["name": "One", "id": "First"], ["name": "Two", "id": "Second"], ["name": "Three", "id": "Third"]] if json in server is like this: [{"id":"First","name":"One"}, {"id":"Second","name":"Two"}, {"id":"Third","name":"Three"}] Your own code should then be: struct Item : Codable { // Need Codable to be used by Decoder var id: String // Identical to the key:value of the JSON (you can later change the name using CodingKeys … var name: String } var quotes: [Item] { var output: [Item] = [] if let url = URL(string:"https://www.TEST.com/test_connection.php") { URLSession.shared.dataTask(with: url) { (data, response, error) in if let data { if let json = try? JSONDecoder().decode([Item].self, from: data) { output = json } } } } return output }
Mar ’25
Reply to ForEach loop in a Map doesn't work
@DuBu error messages are, IMHO, a real weakness of SwiftUI. Messages are at best not helpful, at worst misleading.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Mar ’25
Reply to Capturing self instead of using self. in switch case in DispatchQueue causes compiler error
Thanks Scott. I filed a bug report: Mar 13, 2025 at 3:24 PM – FB16853197 I'll leave the thread open for a while, in case some compiler engineer notices it.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Mar ’25
Reply to How to integrate data from a web service into an array
That's because dataTask has not completed when you return. Look here to see how to wait until it has completed: https://stackoverflow.com/questions/38952420/swift-wait-until-datataskwithrequest-has-finished-to-call-the-return Write the sendRequest function, func sendRequest (request: NSURLRequest,completion:(NSData?)->()){ NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in if error != nil { return completion(data) } else { return completion(nil) } }.resume() } And use it inside var quotes. The completion will return output
Replies
Boosts
Views
Activity
Mar ’25
Reply to Most wanted Xcode features…
Wanted Feature : Get a warning when compiling if an IBOutlet is not connected. Pain point: It may happen to forget a connection and not see it (notably because in some cases, Xcode is mislead to show a connection that does not exist in this project). That's crash at runtime. Feasibility: When using storyboard, Xcode notes when an IBOutlet is connected or not to the storyboard object. So it must be easy to have it detected at compile/build phase.
Replies
Boosts
Views
Activity
Mar ’25
Reply to my first post about swift language
Welcome to the forum. I hope your next posts will be more interesting and useful 😉
Replies
Boosts
Views
Activity
Mar ’25
Reply to Just want to check if
Welcome to the forum. You can access, but of course to a limited scope if you have no account. Why this question ?
Replies
Boosts
Views
Activity
Mar ’25
Reply to iOS Screenshot prevention Whatsapp
I tested on a WhatsApp conversation and could get a screen capture. What page is this ? If that could help: https://www.reddit.com/r/ios/comments/14yyvzo/can_i_disable_screenshot_detection/?rdt=61137 Note: have you tested to take a photo of the screen with another iPhone ? Does it work ? If so, even if you prevent direct screen capture, there are simple workaround…
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Mar ’25
Reply to App is marked as a blood alcohol content calculator
"Promillewert" Do you effectively reference to blood alcohol content ? If so, the problem is the app itself, as explained by reviewer. Just changing the wording by speaking of estimation will most likely not be enough. It remains a blood alcohol value. And this has to be provided by approved hardware (there are likely legal liability behind this).
Replies
Boosts
Views
Activity
Mar ’25
Reply to Immediate crash of Apple Watch simulator when typing a key
The bug cannot be reproduced anymore (Xcode 16.2, Watch simulator Series 9 45mm 10.0 or Series 6 45 mm OS 10.4). It seems solved ; may be thanks to MacOS update to Sequoia 15.3.1 (24D70). I closed the bug report as well.
Replies
Boosts
Views
Activity
Mar ’25
Reply to Change app language on Apple Watch simulator
Thanks a lot. That's so simple, once you know.
Replies
Boosts
Views
Activity
Mar ’25
Reply to App Rejected Due to U.S. Sanctions – Seeking Guidance
Where are you from ? Netherlands ? Is it about encryption ?
Replies
Boosts
Views
Activity
Mar ’25
Reply to No response for an appeal for one week
This forum is not the right place for such a request. And no interest to repost 2 or 3 times. That will not change anything but clutter the forum. Did you try to contact support ? https://developer.apple.com/contact/topic/select
Replies
Boosts
Views
Activity
Mar ’25
Reply to UITextView crash on iOS 18.4 beta
I tested in Swift something similar, without crash on simulator 18.4 let string = "ffi" var style = [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: .regular), NSAttributedString.Key.paragraphStyle: NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle] let attributeString = NSMutableAttributedString(string: string, attributes: style) testField.attributedText = attributeString
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Mar ’25
Reply to ForEach loop in a Map doesn't work
Welcome to the forum MapMarker is deprecated and required extra parmateres in Map: Deprecated: Use Marker along with Map initializers that take a MapContentBuilder instead. Here is the example from Xcode doc: struct IdentifiablePlace: Identifiable { let id: UUID let location: CLLocationCoordinate2D init(id: UUID = UUID(), lat: Double, long: Double) { self.id = id self.location = CLLocationCoordinate2D( latitude: lat, longitude: long) } } struct PinAnnotationMapView: View { let place: IdentifiablePlace = IdentifiablePlace(lat: 0, long: 0) @State var region: MKCoordinateRegion = MKCoordinateRegion() var body: some View { Map(coordinateRegion: $region, annotationItems: [place]) { place in MapMarker(coordinate: place.location) } } } I tested your code replacing MapMarker by Marker, it works (with a String as first parameter. Map { ForEach(markers) { marker in Marker("test", coordinate: marker.coordinate) // MapMarker } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Mar ’25
Reply to How to integrate data from a web service into an array
To further test, I mimicked your JSON, considering it would look like this (each element with an id and a name). They are in fact Dictionaries: [{"id":"First","name":"One"}, {"id":"Second","name":"Two"}, {"id":"Third","name":"Three"}] If so, the test code (I commented out the url part to replace by hardwired data) typealias MyDict = [String: String] // A dictionary is defined as [key:Value] struct Item : Codable { // Need Codable to be used by Decoder var id: String // Identical to the key:value of the JSON (you can later change the name using CodingKeys … var name: String } // ----------- this part for hardwired test -------------- // This to create data by hand ; in your case, data will come from php request let one = Item(id: "First", name:"One") let two = Item(id: "Second", name:"Two") let three = Item(id: "Third", name:"Three") let itemData = [one, two, three] // Here, 3 elements in JSON let data = try JSONEncoder().encode(itemData) // create the data that you'll in fact retrieve from url print("JSON Data", String(data: testJSONData, encoding: .utf8)!) // To check what is in data // ------------------------------------------------------- var quotes: [MyDict] { // back to array of Dict var output: [MyDict] = [] // ----------- 3 lines commented for test with hardwired data -------------- // if let url = URL(string:"https://www.TEST.com/test_connection.php") { // URLSession.shared.dataTask(with: url) { (data, response, error) in // if let data { // data were built with encode, but that would work with url if you uncomment those 3 lines if let json = try? JSONDecoder().decode([MyDict].self, from: data) { output = json } // } // } // } return output } print("quotes", quotes) You get this (once again, dictionaries are not ordered): JSON Data [{"id":"First","name":"One"},{"id":"Second","name":"Two"},{"id":"Third","name":"Three"}] quotes [["name": "One", "id": "First"], ["name": "Two", "id": "Second"], ["name": "Three", "id": "Third"]] if json in server is like this: [{"id":"First","name":"One"}, {"id":"Second","name":"Two"}, {"id":"Third","name":"Three"}] Your own code should then be: struct Item : Codable { // Need Codable to be used by Decoder var id: String // Identical to the key:value of the JSON (you can later change the name using CodingKeys … var name: String } var quotes: [Item] { var output: [Item] = [] if let url = URL(string:"https://www.TEST.com/test_connection.php") { URLSession.shared.dataTask(with: url) { (data, response, error) in if let data { if let json = try? JSONDecoder().decode([Item].self, from: data) { output = json } } } } return output }
Replies
Boosts
Views
Activity
Mar ’25