Moved fints4k iOS extension methods to common

This commit is contained in:
dankito 2020-07-23 15:27:07 +02:00
parent 85572d7fb2
commit 388f725c32
2 changed files with 20 additions and 22 deletions

View File

@ -1,7 +1,7 @@
package net.dankito.utils.multiplatform
import platform.Foundation.NSArray
import platform.Foundation.NSDictionary
import kotlinx.cinterop.*
import platform.Foundation.*
fun <T> NSArray.toList(): List<T> {
@ -26,3 +26,21 @@ fun NSDictionary.getString(key: String, defaultValue: String): String {
fun NSDictionary.getStringOrEmpty(key: String): String {
return this.getString(key, "")
}
fun String.toNSData(): NSData {
return this.encodeToByteArray().toNSData()
}
fun ByteArray.toNSData(): NSData = NSMutableData().apply {
if (isEmpty()) return@apply
this@toNSData.usePinned {
appendBytes(it.addressOf(0), size.convert())
}
}
fun NSData.toByteArray(): ByteArray {
val data: CPointer<ByteVar> = bytes!!.reinterpret()
return ByteArray(length.toInt()) { index -> data[index] }
}

View File

@ -1,20 +0,0 @@
import kotlinx.cinterop.*
import platform.Foundation.*
fun String.toNsData(): NSData {
return this.encodeToByteArray().toNSData()
}
fun ByteArray.toNSData(): NSData = NSMutableData().apply {
if (isEmpty()) return@apply
this@toNSData.usePinned {
appendBytes(it.addressOf(0), size.convert())
}
}
fun NSData.toByteArray(): ByteArray {
val data: CPointer<ByteVar> = bytes!!.reinterpret()
return ByteArray(length.toInt()) { index -> data[index] }
}