Write stream to file by Swift

Original version in objective-c:

https://gist.github.com/algal/54467df650c875827f2b

// Ref:
// https://gist.github.com/algal/54467df650c875827f2b
// https://developer.apple.com/documentation/dispatch/dispatchio/2892309-init
class FileOutputStream: OutputStream {
private var filepath: URL!
private var channel: DispatchIO!
// Initialized an output stream that writes to `filepath`.
// – requires: `filepath` must be a file path URL, for a file that can be created.
init?(filepath: URL) {
super.init(url: filepath, append: true)
self.filepath = filepath
if let cpath = filepath.path.cString(using: .utf8) {
let outputflag: Int32 = O_CREAT | O_WRONLY
let mode: mode_t = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
self.channel = DispatchIO(type: .stream,
path: cpath,
oflag: outputflag,
mode: mode,
queue: .global(qos: .background)) { errorCode in
if errorCode != 0 {
print("FileOutputStream: error creating io channel")
}
}
} else {
self.channel = nil
return nil
}
}
// Initializes output stream to write to `filename` in the user's Documents directory.
convenience init?(filename: String) {
if let pathURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent(filename) {
self.init(filepath: pathURL)
} else {
return nil
}
}
func write(string: String) {
if let dataString = string.data(using: .utf8) {
let dispatchData = dataString.withUnsafeBytes { DispatchData(bytes: $0)}
channel.write(offset: 0, data: dispatchData, queue: .global(qos: .background)) { done, data, errorCode in
if errorCode != 0 {
print("FileOutputStream: error creating io channel")
}
}
}
}
}

關於版本號碼命名規則(rule of version number)

以下資料皆來自Ref,這裡僅做個人筆記整理

https://medium.com/fiverr-engineering/major-minor-patch-a5298e2e1798

Major.Minor.Patch

A program’s version does not represent the state of the software but makes a statement about its API for the consumer.
Semantic versioning does not reflect the size of the update, but the changes in the software’s public API.

  • Patch:
    Patch updates are 「interchangeable」, meaning consumers can upgrade or downgrade freely.

Content: Internal fix
Example: Bug fix, Performance improvement, environment or internal tweaks
Policy: Consumers should update their software without hesitation

  • Minor
    Minor updates are 「backwards compatible」, meaning consumers can upgrade freely.

Content: Interface change with full backward compatibility
Example: New feature, Endpoint declared deprecated
Policy: Update your software to get some new features. Nothing will break

  • Major
    Major updates are 「non-compatible」, meaning consumers can not upgrade without changing their software where applicable.

Content: Interface change breaking backward compatibility
Example: Change API endpoint name or signature, Remove an endpoint
Policy: Test your system extensively after updating.
Migration documents may be in order

IOS – creating a framework

以下資料皆來自Ref,這裏僅作個人筆記整理

Ref: https://www.raywenderlich.com/17753301-creating-a-framework-for-ios

pack ios archive of framework

xcodebuild archive \
-scheme TestFramework \
-configuration Release \
-destination ‘generic/platform=iOS’ \
-archivePath ‘./build/TestFramework.framework-iphoneos.xcarchive’ \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES

pack ios simulator archive of framework

xcodebuild archive \
-scheme TestFramework \
-configuration Release \
-destination ‘generic/platform=iOS Simulator’ \
-archivePath ‘./build/TestFramework.framework-iphonesimulator.xcarchive’ \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES

pack macOS Catalyst archive of framework

xcodebuild archive \
-scheme TestFramework \
-configuration Release \
-destination ‘platform=macOS,arch=x86_64,variant=Mac Catalyst’ \
-archivePath ‘./build/TestFramework.framework-catalyst.xcarchive’ \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES

Generating the XCFramework

xcodebuild -create-xcframework \
-framework ‘./build/TestFramework.framework-iphonesimulator.xcarchive/Products/Library/Frameworks/TestFramework.framework’ \
-framework ‘./build/TestFramework.framework-iphoneos.xcarchive/Products/Library/Frameworks/TestFramework.framework’ \
-output ‘./build/TestFramework.xcframework’