Post

Replies

Boosts

Views

Activity

How extract files in Assets.car?
I got a few answers from SO, but the tools are too old and they don't even run my macOS (12.6). Is there any official way to extract an Assets.car file? The reason I ask this question is that I want to re-use the icons for strings/storyboard files in Xcode packaged Assets.car (if it's legal).
0
0
1k
Sep ’23
How throw error in willSet
I have the following code: public var endpoint: String! { willSet { if newValue == nil { throw ErrorCode.NullValue("endpoint") } } } But compiler gives me error: Error is not handled because the enclosing function is not declared 'throws'
3
0
791
Aug ’23
Does Swift support stored property with computed value?
This may sound strange, but I encounter real world need on this. private var queryItems_: [URLQueryItem]? private var queryItems: [URLQueryItem]? { get { if queryItems_ == nil { if !queries.isEmpty { queryItems_ = queries.map { (key: String, value: String?) in return URLQueryItem(name: key, value: value) } } } return queryItems_ } } /// Query strings public private(set) lazy var queries = [String: String?]() { didSet { queryItems_ = nil } } The queryItems will be (re)created on get if queries property was changed. What I wish is that I could use queryItems as a simple var property but let me do my logic in its getter. Is this supported already?
1
0
345
Aug ’23
String interpolation produces a debug description for an optional value; did you mean to make this explicit?
This makes my head dizzy! Help me out of this peril. How to avoid this with my own class objects? let obj: LanguageItem? = LanguageItem(language: "en") print("object: \(obj)") struct LanguageItem: Codable { var language: String var name: String? } extension LanguageItem: CustomStringConvertible { var description: String { "Lang:\(language) name:\(name ?? "(none)")" } } The print statement still prints "Optional(Lang:en name:(none))". How to get rid the Optional prefix?
2
0
561
Aug ’23
Does Swift has a string builder class?
In other languages, I usually have a StringBuilder class that provides the functionality to concatenate strings in an efficient way. // pseudo code let sb = StringBuilder() sb.append("text") sb.appendFormat("name=%@", name) I am aware of @resultBuilder, but does Swift provide a builtin construct?
2
0
2.3k
Aug ’23
JSONEncoder with dictionary of objects?
I have a need to wrap several kinds of objects into a dictionary say [String: Any?], but how do I tell that the Any? object is all Encodable? let params: [String: Any?] = ["num": 123, "text": "abc", "obj": encodableobject] JSONEncoder().encode(params) // compiler error because Any? is not Encodable
1
0
726
Aug ’23
Not able to reference classes/structs in Swift playground sources
I have to admit that this is strange for me. Though I have been using playgrounds for years, but I only write small pieces of code to test simple ideas, and I never used another source file. Today I want to add a new struct in Sources folder. To my surprise, I am not able to reference the struct in the main playground file. Sources/testlets.swift: struct Dummy { var name: String } MyPlayground: var box = Dummy(name: "abc") // error: /.../MyPlayground.playground:22:11 Cannot find 'Dummy' in scope
2
0
930
Aug ’23
How make SomeClass<T> Decodable?
I am trying to encode/decode JSON data, and I have the following code: struct SomeOrdinaryClass: Decodable { // ... } struct SomeBox<T>: Decodable { var data: T? } But Xcode gives me the following error: myfile.swift:16:8 Type 'SomeBox' does not conform to protocol 'Decodable' Is there anyway to overcome this?
1
0
390
Aug ’23
Is there any builtin Text Find Bar?
I have a NSTableView which shows a list of translated text items. I want to provide a Find Bar for user to filter through the items, like the one I have in Xcode. Of course I don't want so many features; I only want to let user enter some text.
Replies
2
Boosts
0
Views
621
Activity
Sep ’23
How extract files in Assets.car?
I got a few answers from SO, but the tools are too old and they don't even run my macOS (12.6). Is there any official way to extract an Assets.car file? The reason I ask this question is that I want to re-use the icons for strings/storyboard files in Xcode packaged Assets.car (if it's legal).
Replies
0
Boosts
0
Views
1k
Activity
Sep ’23
How throw error in willSet
I have the following code: public var endpoint: String! { willSet { if newValue == nil { throw ErrorCode.NullValue("endpoint") } } } But compiler gives me error: Error is not handled because the enclosing function is not declared 'throws'
Replies
3
Boosts
0
Views
791
Activity
Aug ’23
Does Swift provide conventional standard error classes/enums?
I could not find any standard error classes/enums in docs. For example, I have the following error situations: Invalid null parameter Parameter value out of range Property value (of T!) not set
Replies
2
Boosts
0
Views
527
Activity
Aug ’23
Does Swift support stored property with computed value?
This may sound strange, but I encounter real world need on this. private var queryItems_: [URLQueryItem]? private var queryItems: [URLQueryItem]? { get { if queryItems_ == nil { if !queries.isEmpty { queryItems_ = queries.map { (key: String, value: String?) in return URLQueryItem(name: key, value: value) } } } return queryItems_ } } /// Query strings public private(set) lazy var queries = [String: String?]() { didSet { queryItems_ = nil } } The queryItems will be (re)created on get if queries property was changed. What I wish is that I could use queryItems as a simple var property but let me do my logic in its getter. Is this supported already?
Replies
1
Boosts
0
Views
345
Activity
Aug ’23
Linker errors in Swift app with a framework bundle
I have a simple test project which has a framework bundle (as a target). See attached screenshots. When I import MyFramework and use classes from the framework, the app compiles fine but got linker errors. It seems Xcode does not automatically link the bundle into the app.
Replies
2
Boosts
0
Views
574
Activity
Aug ’23
String interpolation produces a debug description for an optional value; did you mean to make this explicit?
This makes my head dizzy! Help me out of this peril. How to avoid this with my own class objects? let obj: LanguageItem? = LanguageItem(language: "en") print("object: \(obj)") struct LanguageItem: Codable { var language: String var name: String? } extension LanguageItem: CustomStringConvertible { var description: String { "Lang:\(language) name:\(name ?? "(none)")" } } The print statement still prints "Optional(Lang:en name:(none))". How to get rid the Optional prefix?
Replies
2
Boosts
0
Views
561
Activity
Aug ’23
Not able to inherit from NSObject in unit test
The following code compiles fine in a normal Swift app, but not in the accompanying unit test bundle. I don't have any idea why. import Foundation struct Dummy: NSObject { // Error: Inheritance from non-protocol type 'NSObject' var name: String? }
Replies
1
Boosts
0
Views
636
Activity
Aug ’23
Argument type 'LanguageItem?' does not conform to expected type 'CVarArg'
I have the following code: let obj: LanguageItem? = LanguageItem(language: "zh-CN") // Argument type 'LanguageItem?' does not conform to expected type 'CVarArg' print(String(format:"obj: %@", obj)) struct LanguageItem: Codable { var language: String var name: String? } How can I make my class work with String(format:)?
Replies
2
Boosts
0
Views
502
Activity
Aug ’23
Does Swift has a string builder class?
In other languages, I usually have a StringBuilder class that provides the functionality to concatenate strings in an efficient way. // pseudo code let sb = StringBuilder() sb.append("text") sb.appendFormat("name=%@", name) I am aware of @resultBuilder, but does Swift provide a builtin construct?
Replies
2
Boosts
0
Views
2.3k
Activity
Aug ’23
JSONEncoder with dictionary of objects?
I have a need to wrap several kinds of objects into a dictionary say [String: Any?], but how do I tell that the Any? object is all Encodable? let params: [String: Any?] = ["num": 123, "text": "abc", "obj": encodableobject] JSONEncoder().encode(params) // compiler error because Any? is not Encodable
Replies
1
Boosts
0
Views
726
Activity
Aug ’23
Not able to reference classes/structs in Swift playground sources
I have to admit that this is strange for me. Though I have been using playgrounds for years, but I only write small pieces of code to test simple ideas, and I never used another source file. Today I want to add a new struct in Sources folder. To my surprise, I am not able to reference the struct in the main playground file. Sources/testlets.swift: struct Dummy { var name: String } MyPlayground: var box = Dummy(name: "abc") // error: /.../MyPlayground.playground:22:11 Cannot find 'Dummy' in scope
Replies
2
Boosts
0
Views
930
Activity
Aug ’23
How make SomeClass<T> Decodable?
I am trying to encode/decode JSON data, and I have the following code: struct SomeOrdinaryClass: Decodable { // ... } struct SomeBox<T>: Decodable { var data: T? } But Xcode gives me the following error: myfile.swift:16:8 Type 'SomeBox' does not conform to protocol 'Decodable' Is there anyway to overcome this?
Replies
1
Boosts
0
Views
390
Activity
Aug ’23
Swift equivalent of C getenv function?
A simple question, but I am not able to find the answer myself. Note - I tried ProcessInfo.environment but it does not include system env vars.
Replies
1
Boosts
0
Views
449
Activity
Aug ’23
Is there any way to add new localizations using a command line tool?
I am reading "Managing Strings Files Yourself". Now the question arises in my mind. Is it possible to add new localizations using ibtool (or alike)? The reason I have this question is that I have a plan to build an app to automate strings localization so that I will be freed from tedious and repetitive work on translation.
Replies
0
Boosts
0
Views
426
Activity
Jul ’23