Post

Replies

Boosts

Views

Activity

Reply to How to limit my numbers in Swift
lives represents health points attack function is the code that my characters can loses health points heal function is the code that my characters can gains health points You can write something like this: &#9;&#9;func heal(anotherCharacter: Character){ &#9;&#9;&#9;&#9;anotherCharacter.lives += 20 &#9;&#9;&#9;&#9;if anotherCharacter.lives > 100 { &#9;&#9;&#9;&#9;&#9;&#9;anotherCharacter.lives = 100 &#9;&#9;&#9;&#9;} &#9;&#9;} &#9;&#9;func attack(otherCharacter: Character){ &#9;&#9;&#9;&#9;otherCharacter.lives -= power &#9;&#9;&#9;&#9;if otherCharacter.lives < 0 { &#9;&#9;&#9;&#9;&#9;&#9;otherCharacter.lives = 0 &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;//If `power` can never be negative, the following check is not needed &#9;&#9;&#9;&#9;if otherCharacter.lives > 100 { &#9;&#9;&#9;&#9;&#9;&#9;otherCharacter.lives = 100 &#9;&#9;&#9;&#9;} &#9;&#9;} Or like this, if you prefer using min, max as specified in your other thread: &#9;&#9;func heal(anotherCharacter: Character){ &#9;&#9;&#9;&#9;anotherCharacter.lives = min(100, anotherCharacter.lives + 20) &#9;&#9;} &#9;&#9;func attack(otherCharacter: Character){ &#9;&#9;&#9;&#9;otherCharacter.lives = min(100, max(0, otherCharacter.lives + power)) &#9;&#9;}
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to How to limit my numbers in Swift
What I have to do now ?  Have in mind what would be the right usage of this site. Have you read this article? Apple Developer Forums - https://developer.apple.com/support/forums/ How this site works and how you should use this site are described there. Please read it carefully if not yet. Undo is not supported in this site, so you should better consider taking enough time when doing things on this site. Especially, please think for future readers. Marking SOLVED on the right solution would benefit for the future readers suffering the same issue.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to Xcode coming to iPad Pro
all kind of people are talking about iOS apps coming to Mac OS and Mac apps coming to iPad M1 Macs have a feature to run iOS apps, but M1 iPads do not have features to run macOS apps. Generally M1 is just a name for marketing and is very similar to A-something. because the m1 chip can run it easily Your because is based on a false assumption. Running macOS apps is not so easy whether M1 is used or not. You can find many threads discussing Xcode on iPad, for example Xcode for iPad Pro - https://developer.apple.com/forums/thread/17585. I do expect Xcode on iPad, but do not know Apple might be planning it or not. You can send a feature request using Apple's Feedback Assistant.
Apr ’21
Reply to Failed to produce diagnostic for expression; please file a bug report
Your code shown is sort of broken, so cannot say something sure for all ranges of your code. (Better care about code formatting if you want to let readers help solving your issue.) But you need to fix ScanView_Previews at least: struct ScanView_Previews: PreviewProvider { static let float2 : Float = 0.2 static var previews: some View { ScanView(los: .constant(float2), los2: .constant(float2)) } } If you have other issues with your code, you may need to show more info. Please show all the relevant code (OrientationInfo, LidarViewModel, Session, ScanLidarButtons and ScanLidarButtonsMobile, there may be more), well formatted. (Use the Code block icon `` properly.)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to Read image from asset catalog
is "UIImage(named:)" the only way to retrieve image data from image set in assets catalog?  As far as I know, YES. UIImage is a bit expensive for sizing and to resizing.  What are you trying to do? Does it make a significant difference between UIImage and Data of the image?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to Read image from asset catalog
I drag a image.jpg into the assets.xcassets. Then you have added an Image Set of name image to the Asset catalogue, not Data Set. You may need to use UIImage.init(named:). Or else if you want to use NSDataAsset, you need to add a Data Set.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to How do I stop 'The compiler is unable to type-check this expression in reasonable time'
now I get more errors Generally, when you get The compiler is unable to type-check this expression, there may be multiple type-related errors, that Swift compiler could not detect. So, while breaking up, you may find more and more errors. Your definition of body is too large. You should better check errors every time you add lines into body. The same original error You need to split up codes until the same error is not generated Cannot find 'launch' in scope (line 5) Cannot find 'launch' in scope (line 13) You use launch in your definition of bodyGroup, that should be declared inside the scope of bodyGroup. But it does not exist. You may need to make it a function instead of computed property:     func bodyGroup(_ launch: Launch) -> some View { &#9;&#9;&#9;&#9;//... &#9;&#9;} And use it as:                                             bodyGroup(launch) You may need to repeat this process.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to UITextView selectRange value when the text include emoji
UITextView.selectedRange return value as 4.  The returned value should be an NSRange, not an integer. My question is how to convert this selectedRange convert to character based? Depends on your usage. What of character based? Assume you want a String specified by selectedRange, you may need to write something like this: &#9;&#9;&#9;&#9;let nsRange = textView.selectedRange &#9;&#9;&#9;&#9;if let range = Range(nsRange, in: textView.text) { &#9;&#9;&#9;&#9;&#9;&#9;let selectedText = String(textView.text[range]) &#9;&#9;&#9;&#9;&#9;&#9;print(selectedText) &#9;&#9;&#9;&#9;&#9;&#9;//... &#9;&#9;&#9;&#9;}
Topic: UI Frameworks SubTopic: UIKit Tags:
Apr ’21
Reply to Generic JSON response from API request
If you want to make your loadData generic and access the property results of decodedResponse, You need to tell that type T has a property named results. For example: struct Response: Codable { &#9;&#9;var results: [Result] } struct Result: Codable { &#9;&#9;var trackId: Int &#9;&#9;var trackName: String &#9;&#9;var collectionName: String } protocol HoldingResults { &#9;&#9;associatedtype R &#9;&#9;var results: [R] {get} } extension Response: HoldingResults {} As you see, the protocol HoldingResults is used to tell Swift that the type conforming to it has a property named results. You can define your loadData using the protocol: &#9;&#9;func loadData<T: Decodable>(model: T.Type, completion: @escaping ([T.R])->Void) &#9;&#9;where T: HoldingResults &#9;&#9;{ &#9;&#9;&#9;&#9;guard let url = URL(string: "https://itunes.apple.com/search?term=taylor+swift&entity=song") else { &#9;&#9;&#9;&#9;&#9;&#9;print("Invalid URL") &#9;&#9;&#9;&#9;&#9;&#9;return &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;let request = URLRequest(url: url) &#9;&#9;&#9;&#9;URLSession.shared.dataTask(with: request) { data, response, error in &#9;&#9;&#9;&#9;&#9;&#9;if let data = data { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;do { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;let decodedResponse = try JSONDecoder().decode(model, from: data) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;DispatchQueue.main.async { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print(decodedResponse) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;completion(decodedResponse.results) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;return &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} catch { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print(error) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;print("Fetch failed: \(error?.localizedDescription ?? "Unknown error")") &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;.resume() &#9;&#9;} And use it as: &#9;&#9;&#9;&#9;loadData(model: Response.self) { &#9;&#9;&#9;&#9;&#9;&#9;self.results = $0 &#9;&#9;&#9;&#9;} (I replaced if-let-try? to do-try-catch, that's my preference and not required.)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to How do I stop 'The compiler is unable to type-check this expression in reasonable time'
it got rid of the original error Big advance! now I get the error Cannot convert value of type '(inout [Rocket]) -> ()' to expected argument type '([Rocket]) -> ()' on line 45 Having multiple things with the same name may not be good for programming, especially when the code being complex enough. I guess you may need to distinguish the property rockets and closure argument rockets: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;rocketApiCall().getUsers{ (rockets) in self.rockets = rockets} //<- Please do not miss `self.`
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to Xcode for OS X El Capitan
Is there a version of xcode that works with OS 10.11.6 (El Capitan) Yes, there is. But it is too old. According to the Xcode wiki - https://en.wikipedia.org/wiki/Xcode, the latest version for OS X 10.11.6 is Xcode 8.2.1. It was released nearly 4 years ago, in the era of Swift 3.0. You cannot build App Store apps, you cannot run your app on recent devices through Xcode. And Swift3 is too old to learn, the Swift language has changed drastically since then. If you do not mind such things above, visit the More Downloads page - https://developer.apple.com/download/more/.
Apr ’21
Reply to Cannot find 'application' in scope
hmm now the app just crashes when i press the call button i think i still got something in the code? I guess it's a problem of your storyboard setting. From this part of the error message: [Diabell_App.ViewController Call:]: unrecognized selector sent to instance iOS is trying to call a method named Call(_:), not btnCallClick(_:). I guess you first connected the action to method Call(_:) and then renamed it to btnCallClick(_:) only on the swift file. Please remove the action connection on the storyboard and re-connect it to the right method. In some cases, you may need to remove the button on the storyboard before re-connecting the action of it.
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21
Reply to Cannot find 'application' in scope
I cannot find any declaration of application in your code. You need to explicitly declare it. With another fixes: @IBAction func btnCallClick(_ sender: UIButton) { let application = UIApplication.shared //- if let phoneURL = URL(string: "tel://+46706106310") { if application.canOpenURL(phoneURL) { //- `canOpenURL`, not `canOpenUrl` application.open(phoneURL, options: [:], completionHandler: nil) //- `options:` needed } else { //... } } }
Topic: Programming Languages SubTopic: Swift Tags:
Apr ’21