Post

Replies

Boosts

Views

Activity

Reply to some symbols in sf symbol 3 can't be displayed
My Xcode is 12.5.  SF Symbols 3 is a beta version of software intended to work with beta versions of SDKs -- iOS 15 SDK, macOS 12 SDK, and so on. SF Symbols 3 These new symbols are available in apps running the beta versions of iOS 15, iPadOS 15, macOS Monterey, tvOS 15, and watchOS 8. You may need to check the Availability of each symbol if you need to target older versions of OSs.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21
Reply to How to override switch case behaviour like we override operator == with Equatable protocol
Seems Swift prefers pattern matching rather than overridden == operator when you use a value of enum with associated value. You can try something like this: extension AMInputType { public struct Matcher { fileprivate let inputType: AMInputType public init(_ inputType: AMInputType) { self.inputType = inputType } } public static func ~= (lhs: AMInputType, rhs: Matcher) -> Bool { return lhs == rhs.inputType } } func compareEnum(inputType: AMInputType) -> Bool { switch AMInputType.Matcher(inputType) { //<- case .number(.all): print("Switch case number check Successful") return true case .calender(.any): print("Switch case calender check Successful") return true case .list(.any): print("Switch case list check Successful") return true default: print("Switch case check Failed") return false } }
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’21
Reply to The following simple function will cause Xcode 12E262 to have "Abort: trap 6"
Thanks for sharing the info. I can reproduce the same issue with your simpler code example. May I know, why is it so? In older versions of Swift compilers, declaring exactly the same identifier in the same code block caused an error, even when one is declared with guard-let. I'm not sure why this is happening, but compilers should not stop with Abort Trap and you can send a bug report. (Showed Definition conflicts with previous value in Xcode 10.1. It is different than shadowing identifiers declared in outer scopes.) And in Xcode 13 beta, your simpler code compiles without errors. I have not tried with Xcode 12.5.1, have you tried?
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’21
Reply to SwiftUI: Markdown support for string variables
Is this a known bug or do I have to create an entry in Feedback Assistant? I'm not sure this would be considered to be a bug or not. When you pass a String literal for Text.init, Swift uses different initializer than passing a value of String. Structure Text Localized Strings If you initialize a text view with a string literal, the view uses the init(_:tableName:bundle:comment:) initializer, Please try this: struct MarkdownTest: View { var text: String = "**Hello** *World*" var markdownText: AttributedString = try! AttributedString(markdown: "**Hello** *World*") var keyText: LocalizedStringKey = "**Hello** *World*" var body: some View { VStack { Text("**Hello** *World*") Text(text) Text("**Hello** *World*" as String) Text(markdownText) Text(keyText) } } }
Topic: App & System Services SubTopic: General Tags:
Jun ’21
Reply to App crash
In your Main.storyboard, you have a scene called Favorite Reminder View Controller. There, you have two labels, one is connected to dateLabel of FavoriteReminderViewController. And the other is connected to both reminderLabel and titleLabel of FavoriteReminderViewController. But your FavoriteReminderViewController does not have a label named reminderLabel, which is causing the error: '[<RemindersApp.FavoriteReminderViewController 0x15150c2a0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key reminderLabel.' You may need to remove the wrong connection by pushing × shown in the Connection Inspector.
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’21
Reply to What is the syntax for "if not" in Swift?
But this would then go when all I really want is the not part if let twinkling = getHighGem() as? MyGem { } else { ..... } Sorry, I wanted the NOT (!) equivalent to : if let twinkling = getHighGem() as? MyGem I cannot be sure if I would be interpreting what you mean correctly, but you want to find some sort of better alternative for this? if let twinkling = getHighGem() as? MyGem { /* Do nothing */ } else { ..... } As this type of code: if someCondition { /* Do nothing */ } else { ..... } can be re-written using not ! operator: if !someCondition { ..... } If you want to do something like above, you need to think what if-else would do on true-case and false-case: The if-let first checks the Optional value of the right hand side if it is nil or not: And if it is not nil, it does a little more Unwrap the Optional value and retrieve the non-Optional value Create a new binding to the unwrapped value as a local constant Execute the true-case code block And if the checked value is nil Execute the false-case code block So, if you just want a false-case execution, you just need a nil checking: if (getHighGem() as? MyGem) == nil { ..... } In this case, it can be rewritten as: if !(getHighGem() is MyGem) { ..... } But, as you can see in the answer of Graphics and Games Engineer, using guard-let-else would be a better alternative in various cases. If you had shown enough context, you would have been shown which is better.
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’21
Reply to Type 'DefaultNavigationViewStyle' has no member 'automatic'
How can I set up my navigationViewStyle to be StackNavigationStyle and the default elsewhere ? The modifier navigationViewStyle requires a value of one concrete type conforming to NavigationViewStyle. So, even when DefaultNavigationViewStyle.automatic is valid there, you cannot use the ternary operator which may return two different types -- StackNavigationViewStyle or DefaultNavigationViewStyle. One possible solution would be using a custom modifier: struct ContentView: View { //... var body: some View { ZStack { NavigationView { List { //... } }.navigationTitle("Options") } .modifier(MyNavigationViewStyle()) } } struct MyNavigationViewStyle: ViewModifier { func body(content: Content) -> some View { if UIDevice.current.model == "iPad" { content.navigationViewStyle(StackNavigationViewStyle()) } else { content.navigationViewStyle(DefaultNavigationViewStyle()) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21
Reply to Nil
I guess, you are using asynchronous methods incorrectly. Anyway, avoid using forced unwrapping. By the way, don't you think you should respond when you get comments and answers?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21
Reply to How can I identify and group data in a JSON file?
I've googled around about top level arrays and nested JSON info but can't figure out what my next step is. Seems your Googling ability is not good enough. You can find bunch of articles searching with swift searching one element in an array. You should show your code enough to test instantly, especially when you show some code in a method, you should show at least the whole method. Partially shown code might often lead to an unexpected and unusable answers. I needed to guess what was the header of the method and some other things. And I assumed something as follows: @IBOutlet weak var targetAreaField: UITextField! func apiCall(callCompletionHandler: @escaping([ReceivedAPIData])->Void) { guard let apiUrl = URL(string: "https://cadatacatalog.state.gov/dataset/4a387c35-29cb-4902-b91d-3da0dc02e4b2/resource/299b3b67-3c09-46a3-9eb7-9d0086581bcb/download/countrytravelinfo.json") else { print("Error: cannot create URL.") return } let session = URLSession(configuration: .default) let task = session.dataTask(with: apiUrl) { [self] data, response, error in if let error = error { print(error) return } guard let safeData = data else { print("Couldn't get data back from url."); return } let decoder = JSONDecoder() let safeDecodeData: [ReceivedAPIData] do { safeDecodeData = try decoder.decode([ReceivedAPIData].self, from: safeData) } catch { fatalError("Couldn't decode data.") } self.delegate?.didUpdateAPIResults(returnedResult: safeDecodeData) //let jsonStringData = data?.prettyPrintedJSONString ?? "**data cannot be prity-printed**" //print("Pretty JSON from server ----- \(jsonStringData)") //... callCompletionHandler(safeDecodeData) } task.resume() } (I have corrected some bad practices in your code, but there is still one unrecommended thing: You should better call completion handler in case of errors. But that is another issue.) When I searched with swift searching one element in an array, this Apple' doc showed in the top page: first(where:) | Apple Developer Documentation. You can use it like this: guard let targetArea = targetAreaField.text, !targetArea.isEmpty else { return } apiCall { result in //print(result.count) guard let targetResult = result.first(where: {$0.geopoliticalarea == targetArea}) else { print("NO data for \(targetArea)") return } print(targetResult) //... }
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’21
Reply to valid replacement for kUTTypeJPEG which is deprecated
Seems the deprecation message is meant for Objective-C code. (You can send a bug report to Apple using Feedback Assistant.) In Swift, Uniform Type Identifiers are wrapped into static members of UTType. System Declared Uniform Type Identifiers Can you try UTType.jpeg.identifier as CFString instead? (You may need import UniformTypeIdentifiers if your source code does not have it yet.)
Topic: Media Technologies SubTopic: Audio Tags:
Jun ’21
Reply to some symbols in sf symbol 3 can't be displayed
My Xcode is 12.5.  SF Symbols 3 is a beta version of software intended to work with beta versions of SDKs -- iOS 15 SDK, macOS 12 SDK, and so on. SF Symbols 3 These new symbols are available in apps running the beta versions of iOS 15, iPadOS 15, macOS Monterey, tvOS 15, and watchOS 8. You may need to check the Availability of each symbol if you need to target older versions of OSs.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to How to override switch case behaviour like we override operator == with Equatable protocol
Seems Swift prefers pattern matching rather than overridden == operator when you use a value of enum with associated value. You can try something like this: extension AMInputType { public struct Matcher { fileprivate let inputType: AMInputType public init(_ inputType: AMInputType) { self.inputType = inputType } } public static func ~= (lhs: AMInputType, rhs: Matcher) -> Bool { return lhs == rhs.inputType } } func compareEnum(inputType: AMInputType) -> Bool { switch AMInputType.Matcher(inputType) { //<- case .number(.all): print("Switch case number check Successful") return true case .calender(.any): print("Switch case calender check Successful") return true case .list(.any): print("Switch case list check Successful") return true default: print("Switch case check Failed") return false } }
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to How can you send a notification using an if statement.
You marked this thread as SOLVED.
Replies
Boosts
Views
Activity
Jun ’21
Reply to The following simple function will cause Xcode 12E262 to have "Abort: trap 6"
Thanks for sharing the info. I can reproduce the same issue with your simpler code example. May I know, why is it so? In older versions of Swift compilers, declaring exactly the same identifier in the same code block caused an error, even when one is declared with guard-let. I'm not sure why this is happening, but compilers should not stop with Abort Trap and you can send a bug report. (Showed Definition conflicts with previous value in Xcode 10.1. It is different than shadowing identifiers declared in outer scopes.) And in Xcode 13 beta, your simpler code compiles without errors. I have not tried with Xcode 12.5.1, have you tried?
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to SwiftUI: Markdown support for string variables
Is this a known bug or do I have to create an entry in Feedback Assistant? I'm not sure this would be considered to be a bug or not. When you pass a String literal for Text.init, Swift uses different initializer than passing a value of String. Structure Text Localized Strings If you initialize a text view with a string literal, the view uses the init(_:tableName:bundle:comment:) initializer, Please try this: struct MarkdownTest: View { var text: String = "**Hello** *World*" var markdownText: AttributedString = try! AttributedString(markdown: "**Hello** *World*") var keyText: LocalizedStringKey = "**Hello** *World*" var body: some View { VStack { Text("**Hello** *World*") Text(text) Text("**Hello** *World*" as String) Text(markdownText) Text(keyText) } } }
Topic: App & System Services SubTopic: General Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to What is the syntax for "if not" in Swift?
if-let is a syntax including non-nil test, so writing twinkling != nil after if let twinkling = ... is not needed. Is this the proper way to test for a nil return? YES.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to Use java with Xcode
is Xcode for java as good as Eclipse? Far from as good. Java currently is not a supported language on Apple's platforms. Better find some better tool if you are planning to do something with Java.
Replies
Boosts
Views
Activity
Jun ’21
Reply to App crash
In your Main.storyboard, you have a scene called Favorite Reminder View Controller. There, you have two labels, one is connected to dateLabel of FavoriteReminderViewController. And the other is connected to both reminderLabel and titleLabel of FavoriteReminderViewController. But your FavoriteReminderViewController does not have a label named reminderLabel, which is causing the error: '[<RemindersApp.FavoriteReminderViewController 0x15150c2a0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key reminderLabel.' You may need to remove the wrong connection by pushing × shown in the Connection Inspector.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to What is the syntax for "if not" in Swift?
But this would then go when all I really want is the not part if let twinkling = getHighGem() as? MyGem { } else { ..... } Sorry, I wanted the NOT (!) equivalent to : if let twinkling = getHighGem() as? MyGem I cannot be sure if I would be interpreting what you mean correctly, but you want to find some sort of better alternative for this? if let twinkling = getHighGem() as? MyGem { /* Do nothing */ } else { ..... } As this type of code: if someCondition { /* Do nothing */ } else { ..... } can be re-written using not ! operator: if !someCondition { ..... } If you want to do something like above, you need to think what if-else would do on true-case and false-case: The if-let first checks the Optional value of the right hand side if it is nil or not: And if it is not nil, it does a little more Unwrap the Optional value and retrieve the non-Optional value Create a new binding to the unwrapped value as a local constant Execute the true-case code block And if the checked value is nil Execute the false-case code block So, if you just want a false-case execution, you just need a nil checking: if (getHighGem() as? MyGem) == nil { ..... } In this case, it can be rewritten as: if !(getHighGem() is MyGem) { ..... } But, as you can see in the answer of Graphics and Games Engineer, using guard-let-else would be a better alternative in various cases. If you had shown enough context, you would have been shown which is better.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to Type 'DefaultNavigationViewStyle' has no member 'automatic'
How can I set up my navigationViewStyle to be StackNavigationStyle and the default elsewhere ? The modifier navigationViewStyle requires a value of one concrete type conforming to NavigationViewStyle. So, even when DefaultNavigationViewStyle.automatic is valid there, you cannot use the ternary operator which may return two different types -- StackNavigationViewStyle or DefaultNavigationViewStyle. One possible solution would be using a custom modifier: struct ContentView: View { //... var body: some View { ZStack { NavigationView { List { //... } }.navigationTitle("Options") } .modifier(MyNavigationViewStyle()) } } struct MyNavigationViewStyle: ViewModifier { func body(content: Content) -> some View { if UIDevice.current.model == "iPad" { content.navigationViewStyle(StackNavigationViewStyle()) } else { content.navigationViewStyle(DefaultNavigationViewStyle()) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to I am getting the Errors Type 'CoinModel' does not conform to protocol 'Decodable', and Type 'CoinModel' does not conform to protocol 'Encodable''
As far as I tried with Xcode 12.5.1, this message was shown on the line case roi: CodingKey case 'roi' does not match any stored properties CoinModel.CodingKeys has a case named roi, but there is no stored property roi in CoinModel. What is it for?
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to Nil
I guess, you are using asynchronous methods incorrectly. Anyway, avoid using forced unwrapping. By the way, don't you think you should respond when you get comments and answers?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to Create Instance of class
Can you show enough code to reproduce the issue?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to How can I identify and group data in a JSON file?
I've googled around about top level arrays and nested JSON info but can't figure out what my next step is. Seems your Googling ability is not good enough. You can find bunch of articles searching with swift searching one element in an array. You should show your code enough to test instantly, especially when you show some code in a method, you should show at least the whole method. Partially shown code might often lead to an unexpected and unusable answers. I needed to guess what was the header of the method and some other things. And I assumed something as follows: @IBOutlet weak var targetAreaField: UITextField! func apiCall(callCompletionHandler: @escaping([ReceivedAPIData])->Void) { guard let apiUrl = URL(string: "https://cadatacatalog.state.gov/dataset/4a387c35-29cb-4902-b91d-3da0dc02e4b2/resource/299b3b67-3c09-46a3-9eb7-9d0086581bcb/download/countrytravelinfo.json") else { print("Error: cannot create URL.") return } let session = URLSession(configuration: .default) let task = session.dataTask(with: apiUrl) { [self] data, response, error in if let error = error { print(error) return } guard let safeData = data else { print("Couldn't get data back from url."); return } let decoder = JSONDecoder() let safeDecodeData: [ReceivedAPIData] do { safeDecodeData = try decoder.decode([ReceivedAPIData].self, from: safeData) } catch { fatalError("Couldn't decode data.") } self.delegate?.didUpdateAPIResults(returnedResult: safeDecodeData) //let jsonStringData = data?.prettyPrintedJSONString ?? "**data cannot be prity-printed**" //print("Pretty JSON from server ----- \(jsonStringData)") //... callCompletionHandler(safeDecodeData) } task.resume() } (I have corrected some bad practices in your code, but there is still one unrecommended thing: You should better call completion handler in case of errors. But that is another issue.) When I searched with swift searching one element in an array, this Apple' doc showed in the top page: first(where:) | Apple Developer Documentation. You can use it like this: guard let targetArea = targetAreaField.text, !targetArea.isEmpty else { return } apiCall { result in //print(result.count) guard let targetResult = result.first(where: {$0.geopoliticalarea == targetArea}) else { print("NO data for \(targetArea)") return } print(targetResult) //... }
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to valid replacement for kUTTypeJPEG which is deprecated
Seems the deprecation message is meant for Objective-C code. (You can send a bug report to Apple using Feedback Assistant.) In Swift, Uniform Type Identifiers are wrapped into static members of UTType. System Declared Uniform Type Identifiers Can you try UTType.jpeg.identifier as CFString instead? (You may need import UniformTypeIdentifiers if your source code does not have it yet.)
Topic: Media Technologies SubTopic: Audio Tags:
Replies
Boosts
Views
Activity
Jun ’21