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")
}
}
}
}
}

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’

IOS data binding

以下資料皆來自[Ref 1],這裡僅作個人筆記整理:

Data Binding is simply the process that establishes a connection between the app UI (View Controller) and the data (Not Model, But View Model) it displays.

There are different ways of data binding:

Technique 1: Observables
Technique 2: Event Bus / Notification Center
Technique 3: FRP Technique (ReactiveCocoa / RxSwift):
Technique 4: Combine

Note:

  • Sample code can be found in [Ref 1]
  • Almost all techniques for data binding use the Publisher-Subscriber pattern.

Ref:

1) Data Binding in MVVM on iOS:
https://fitzafful.medium.com/data-binding-in-mvvm-on-ios-714eb15e3913


2) ReactiveCocoa vs RxSwift:
https://www.raywenderlich.com/1190-reactivecocoa-vs-rxswift

SwiftUI學習

https://developer.apple.com/tutorials/swiftui/handling-user-input

https://stackoverflow.com/questions/59247183/swiftui-state-vs-binding

https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E7%94%A8%E7%8B%80%E6%85%8B%E8%A8%AD%E8%A8%88-swiftui-%E7%95%AB%E9%9D%A2-%E8%AA%8D%E8%AD%98-state-property-binding-n

@StateObject

@State
@EnvironmentObject
@Binding
@Environment(.editMode) var editMode
@Published

ObservableObject

What is the difference in declaring a Binding property in SwiftUI?

https://stackoverflow.com/questions/71757500/what-is-the-difference-in-declaring-a-binding-property-in-swiftui