Feature/central manager init options#51
Merged
Conversation
Nativeの数値、boolをcsharp側で生成するために作成 swiftのint, boolは値型だが、NSNumberはクラス。
add Foundation NSNumber
## NSStringクラスを追加した理由 汎用的な文字列取得実装のため。 MicrosoftのドキュメントでStringBuilderは非推奨と書かれていたためにStringBuilderを使い続けることに抵抗があった。 https://learn.microsoft.com/ja-jp/dotnet/standard/native-interop/best-practices そして、NSDictionaryに文字列を登録するため ## NSString.HandleToStringについて 流れに関してはXamarinの実装を参考にしている。 https://github.com/xamarin/xamarin-macios/blob/main/src/CoreFoundation/CFString.cs#L190-L224 ここではFromHandleという関数名だが、NSString.FromHandleという名前であればNSStringを返した方が自然だと思うためHandleToStringという名前にした。 引数のreleaseHandleに関してはSafeNSStringHandleをusingで破棄する方が書き心地よいと感じるため不要と判断した。 ## NSStringを使用している理由 swiftのStringは値型のためUnmanaged.passRetainedの引数にすることができない。 したがって、csharp側にポインタを渡すためにはCFStringまたはNSString等のクラスを用いる必要があった。 そして、CFStringよりもNSStringのほうが扱いやすかったためNSStringを使用している。 NSStringを参照型のクラスとして使用することはswiftの公式ドキュメントにも書かれている。 > They provide reference semantics instead of value semantics, which is a useful tool to have in the toolbox. https://github.com/apple/swift-corelibs-foundation ## NSStringのcstringからのコピーをcsharp側で行っている理由 char配列を渡してそこにswiftで文字列を入れたところ文字化けしたから。 StringBuilderの場合は文字化けしないが前述の理由により避けた。 byte配列を渡せば文字化けしないかもしれないが、ArrayPoolが遅いかもしれず、最適な方法がわからなかったため 中身のコード見た感じ問題なさそうで一番楽なMarshal.PtrToStringUTF8を使用した。 https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs,80 ## LengthOfBytesUtf8について 使ってないけど、テストで活用できるため残している。
良い。NSStringを解放したらこれも解放される(内部だから)。 > This C string is a pointer to a structure inside the string object, which may have a lifetime shorter than the string object and will certainly not have a longer lifetime. https://developer.apple.com/documentation/foundation/nsstring/1411189-utf8string ## utf8StringはNSStringの中身だがこれをコピーしなくてよいのか? NSStringはヒープなため別の場所にコピーするメリットはlifetimeをNSStringと分離することだと思う。 同じでいいためしなくて良いと判断 ## cstringの文字列の長さの測り方 ここにstrlenを使った例がある https://developer.apple.com/documentation/swift/string/utf8cstring
add Foundation NSString
## NSMutableDictionaryが必要な理由 swiftのDictionaryはクラスじゃないから。 https://developer.apple.com/documentation/swift/dictionary Unmanaged.passRetainの引数にするためにはクラスである必要がある。 ## NSMutableDictionaryからswift Dictionaryへの変換 基本的には as でできる。 ただしbool値に関してはNSNumberのため変換後に入るのが数値。 0,1 ではなく false, trueの値が必要な場合には変換用の関数を自前で作る必要がある。 今回はなかった。
add Foundation NSMutableDictionary
## クラス名 https://learn.microsoft.com/en-us/dotnet/api/corebluetooth.cbcentralinitoptions?view=xamarin-ios-sdk-12 xamarinのをそのまま踏襲 ## キー名について 「kCBInitOptionShowPowerAlert」という値はswift側のprintで出力したもの。 https://developer.apple.com/documentation/corebluetooth/cbcentralmanageroptionshowpoweralertkey appleのドキュメントでは定数の中身については書かれていないため、この定数を取得するNativeMethodsを定義するのが無難ではあると思う。 xamarinではおそらく定数を取ってきている。 https://github.com/xamarin/xamarin-macios/blob/a0eec0c265eeb3ad5b3e01e08793e326d890a647/src/corebluetooth.cs#L33 が、定数取ってくるためにNativeMethods書いたりするのが手間だったため一番楽で処理速度も当然最速なcsharp側に直書きという方法に一旦している。 ## ToNativeDictionaryについて xamarinではDictionaryContainerを継承して、オプションをセットすると同時にNativeのDictionaryを更新しているようだった。 https://learn.microsoft.com/en-us/dotnet/api/foundation.dictionarycontainer?view=xamarin-ios-sdk-12 でもこれだと options が持ってるNSMutableDictionaryのDisposeが手間。 xamarinだとやっていないっぽかった。 そのため使う時にだけ作って使い捨てるという実装にしている。
## なぜIntPtrなのか SafeHandleはクラスだが、nullを渡したらエラーがでた。 SafeHandleをnullとして渡すドキュメントが見つからなかったためIntPtrを引数にとるようにした。 また、別の箇所でもSafeHandleにnullはいれないようにしている
add CBCentralInitOptions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PRs
add Foundation NSNumber #50
add Foundation NSString #52
add Foundation NSMutableDictionary #53
add CBCentralInitOptions #54