Post

Replies

Boosts

Views

Activity

Reply to Is there any way to get static var's in a type using Mirror?
Well, it's partially true. Playground: print("class:") let m1 = Mirror(reflecting: Some.self) for child in m1.children { print(child.label ?? "", child.value) } print("instance:") let m2 = Mirror(reflecting: Some()) for child in m2.children { print(child.label ?? "", child.value) } print("\n-=|E.O.F|=-") class Some { static let inst = Some() private var n = 1234 var f = 1.234 } Output: class: instance: n 1234 f 1.234 Mirror can't mirror the static variable.
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’23
Reply to How read image file metadata?
After some digging, it seems the command line tool mdls does similar job: mdls IMG_0245.jpeg _kMDItemDisplayNameWithExtensions = "IMG_0245.jpeg" kMDItemAcquisitionMake = "Apple" ... kMDItemContentType = "public.jpeg" ... But I prefer achieving these information by coding. Any ideas?
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’23
Reply to Does Swift support number suffixes?
Actually I don't have 'a lot' examples. I just started real app development using Swift a couple of months ago. For this specific scenario, I really like to have what I have in C/C++. Advantages: It can save a few key strokes and makes me as a developer feel better; and even more, makes Swift feels more swift. auto n = 12345ull; vs var n: UInt64 = 12345 Real world example (unit test): func testByteSize() { let tests = [ (s: "1234", v: 1234), (s: "1kb", v: 1024), (s: "1234k", v: 1234 * 1024), (s: "3mb", v: 3 * 1024 * 1024), (s: "2GB", v: 2 * 1024 * 1024 * 1024), (s: "7tb", v: 7 * 1024 * 1024 * 1024 * 1024), ] for test in tests { let n = test.s.parseByteSize() XCTAssertNotNil(n) XCTAssertEqual(UInt64(test.v), n!) } } If Swift allows me to code a number as 1234ull, I won't have to do the UInt64(test.v) cast. Moreover, I really like the tuple's v field be UInt64 instead of default Int.
Topic: Programming Languages SubTopic: Swift Tags:
Nov ’23
Reply to How parse byte size strings into actual number?
It seems I can use the builtin Scanner class: public extension String { func parseByteSize() -> UInt64? { var n: UInt64 = 0 let scanner = Scanner(string: self) if scanner.scanUnsignedLongLong(&n) { if !scanner.isAtEnd { let suffix = self[self.index(self.startIndex, offsetBy: scanner.scanLocation)...] switch suffix.uppercased() { case "KB": n *= 1024 case "MB": n *= 1024 * 1024 case "GB": n *= 1024 * 1024 * 1024 case "TB": n *= 1024 * 1024 * 1024 * 1024 default: return nil } } return n } return nil } }
Topic: Programming Languages SubTopic: Swift Tags:
Nov ’23
Reply to Need advices on NSMutableArray in Swift
It's an agony working with array/dictionary in Swift! NSMutableArray does not support generics so I have to stick to Swift builtin array. I finally have to employ a not-so-perfect-and-ugly workaround: /// Act as a reference container for value types. open class ValueBox<ValueType> { public var value: ValueType public init(_ value: ValueType) { self.value = value } } // Arrays in a dictionary typealias TranslationUnitBox = ValueBox<[TranslationUnit]> var translations = [String: TranslationUnitBox>() // Later in some other code if let box = translations["en"] { box.value[index].translatedText = "..." }
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’23
Reply to Weird error with HTTPS connection
I found the answer myself. It's a misunderstanding of ATS (Apple Transport Security) feature. I thought only non-HTTPS protocol needs the following ATS in info.plist: <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> But it turned out that connecting to HTTPS also needs the above setting.
Topic: Programming Languages SubTopic: Swift Tags:
Oct ’23
Reply to Environment variable not working in another user account
It's a stupid question. I found out why. I set this up several years ago and I forgot the exact steps. Actually I have to define a custom path in Xcode Preferences->Locations->Custom Paths.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Dec ’23
Reply to Is there any way to get static var's in a type using Mirror?
Well, it's partially true. Playground: print("class:") let m1 = Mirror(reflecting: Some.self) for child in m1.children { print(child.label ?? "", child.value) } print("instance:") let m2 = Mirror(reflecting: Some()) for child in m2.children { print(child.label ?? "", child.value) } print("\n-=|E.O.F|=-") class Some { static let inst = Some() private var n = 1234 var f = 1.234 } Output: class: instance: n 1234 f 1.234 Mirror can't mirror the static variable.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Dec ’23
Reply to How read image file metadata?
After some digging, it seems the command line tool mdls does similar job: mdls IMG_0245.jpeg _kMDItemDisplayNameWithExtensions = "IMG_0245.jpeg" kMDItemAcquisitionMake = "Apple" ... kMDItemContentType = "public.jpeg" ... But I prefer achieving these information by coding. Any ideas?
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Dec ’23
Reply to Common audio/image file extensions on macOS
For other peoples reference, I have found the answer in this thread, thanks to Quinn's reply.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Dec ’23
Reply to What should I do with UTI (NSImage.imageTypes)?
Yes! That's just what I need. if let ut = UTType.types(tag: "jpeg", tagClass: .filenameExtension, conformingTo: nil).first { print(ut.tags[.filenameExtension]) }
Topic: App & System Services SubTopic: Core OS Tags:
Replies
Boosts
Views
Activity
Dec ’23
Reply to Does Swift support number suffixes?
Actually I don't have 'a lot' examples. I just started real app development using Swift a couple of months ago. For this specific scenario, I really like to have what I have in C/C++. Advantages: It can save a few key strokes and makes me as a developer feel better; and even more, makes Swift feels more swift. auto n = 12345ull; vs var n: UInt64 = 12345 Real world example (unit test): func testByteSize() { let tests = [ (s: "1234", v: 1234), (s: "1kb", v: 1024), (s: "1234k", v: 1234 * 1024), (s: "3mb", v: 3 * 1024 * 1024), (s: "2GB", v: 2 * 1024 * 1024 * 1024), (s: "7tb", v: 7 * 1024 * 1024 * 1024 * 1024), ] for test in tests { let n = test.s.parseByteSize() XCTAssertNotNil(n) XCTAssertEqual(UInt64(test.v), n!) } } If Swift allows me to code a number as 1234ull, I won't have to do the UInt64(test.v) cast. Moreover, I really like the tuple's v field be UInt64 instead of default Int.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Nov ’23
Reply to How parse byte size strings into actual number?
It seems I can use the builtin Scanner class: public extension String { func parseByteSize() -> UInt64? { var n: UInt64 = 0 let scanner = Scanner(string: self) if scanner.scanUnsignedLongLong(&n) { if !scanner.isAtEnd { let suffix = self[self.index(self.startIndex, offsetBy: scanner.scanLocation)...] switch suffix.uppercased() { case "KB": n *= 1024 case "MB": n *= 1024 * 1024 case "GB": n *= 1024 * 1024 * 1024 case "TB": n *= 1024 * 1024 * 1024 * 1024 default: return nil } } return n } return nil } }
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Nov ’23
Reply to How convert data encoded in GBK/GB2312 to string?
After some digging on the net, I myself found a solution: NSStringEncoding gbkEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); NSString* newStr = [[NSString alloc] initWithData:data encoding:gbkEncoding];
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Nov ’23
Reply to PropertyListDecoder and .strings file
Does anyone know where I can find the specification of strings file format? Currently by pure observing, I believe the value part of key-value pair is very similar or same with C/JavaScript string syntax. Say, \n and " are escaped, but not sure if there are other specials.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Oct ’23
Reply to FileMerge won't run on macOS 12.7
It's strange that I can run FileMerge via command line opendiff file1 file2.
Replies
Boosts
Views
Activity
Oct ’23
Reply to Need help on generics where clause
Is there any way to specify ValueType is a value type in the where clause? Apparently you don't need a box if the value is already a reference type.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Oct ’23
Reply to Need advices on NSMutableArray in Swift
It's an agony working with array/dictionary in Swift! NSMutableArray does not support generics so I have to stick to Swift builtin array. I finally have to employ a not-so-perfect-and-ugly workaround: /// Act as a reference container for value types. open class ValueBox<ValueType> { public var value: ValueType public init(_ value: ValueType) { self.value = value } } // Arrays in a dictionary typealias TranslationUnitBox = ValueBox<[TranslationUnit]> var translations = [String: TranslationUnitBox>() // Later in some other code if let box = translations["en"] { box.value[index].translatedText = "..." }
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Oct ’23
Reply to Weird error with HTTPS connection
I found the answer myself. It's a misunderstanding of ATS (Apple Transport Security) feature. I thought only non-HTTPS protocol needs the following ATS in info.plist: <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> But it turned out that connecting to HTTPS also needs the above setting.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Oct ’23
Reply to Is there any builtin Text Find Bar?
Okay, I just found answer myself - NSSearchField
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Sep ’23
Reply to How throw error in willSet
I found a solution myself. I need to force unwrap the newValue like below: willSet { // this guarantees newValue is not nil let _ = newValue! }
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Aug ’23