当前位置: 首页 > wzjs >正文

做网站先做前台还是后台建站模板平台

做网站先做前台还是后台,建站模板平台,江阴做网站的企业,免费模板网站都有什么区别Kotlin Multiplatform--01:项目结构基础 引言Common CodeTargetsExpected 和 actual1.使用函数2.使用接口 引言 以下为使用 Android Studio 创建的默认 Kotlin Multiplatform 的项目结构,本章将对项目结构进行简单介绍,让读者对 Kotlin Multi…

Kotlin Multiplatform--01:项目结构基础

    • 引言
    • Common Code
    • Targets
    • Expected 和 actual
        • 1.使用函数
        • 2.使用接口

引言

        以下为使用 Android Studio 创建的默认 Kotlin Multiplatform 的项目结构,本章将对项目结构进行简单介绍,让读者对 Kotlin Multiplatform 项目能够有一个大概的认识。
                


Common Code

        Common Code 是指与平台无关的代码,比如业务逻辑等。在项目结构中对应 commonMain 文件夹。需要注意的是,Common Code 不能使用任何与平台相关的 API ,比如 java.io.File 等,也不能添加任何与平台相关的依赖库。但可以使用专门为 Kotlin Multiplatform 设计的 Kotlin 库,比如 kotlinx.coroutines 等。

Targets

        Target 指需要将项目编译到哪个平台上,比如 JVM, JS, Android, iOS, or Linux 等。需要注意:平台相关的代码可以调用 Common Code,但 Common Code 中不能调用平台相关的代码。在 build.gradle.kts 文件中配置,如下所示:

kotlin {androidTarget() // Declares a target that corresponds to Android phonesiosArm64() // Declares a target that corresponds to 64-bit iPhonesmacosArm64() // Declares a target that corresponds to Modern Apple Silicon-based Macs
}

        声明编译目标后,还需要在项目中新建相应的文件夹和文件,命名规则为xxxMain,还需要在里面新建名为 kotlin 的子文件夹,放该平台相关的源代码。因为我在默认项目结构上额外声明了 macosArm64 的目标,所以需要新建一个 macosMain 文件夹,如下所示:
                

        最后在 build.gradle.kts 中的 sourceSets 中可以配置不同目标的依赖库文件,如下所示:

    sourceSets {androidMain {//put your multiplatform dependencies hereimplementation("xxx")}commonMain.dependencies {//put your multiplatform dependencies hereimplementation("xxx")}iosMain.dependencies {//put your multiplatform dependencies hereimplementation("xxx")}macosMain.dependencies { //put your multiplatform dependencies hereimplementation("xxx")}}

Expected 和 actual

        有时候我们确实需要在 Common Code 中调用平台相关的代码,并在不同的平台下有不同的表现该如何处理呢?

1.使用函数

        在 CommonMain 中声明一个 except 函数,供其他 Common Code 使用:

package identityclass Identity(val userName: String, val processID: Long)expect fun buildIdentity(): Identity

        在不同平台中使用 actual 进行不同的实现,比如在 jvm 中进行以下实现:

package identityimport java.lang.System
import java.lang.ProcessHandleactual fun buildIdentity() = Identity(System.getProperty("user.name") ?: "None",ProcessHandle.current().pid()
)

        在 POSIX 系统中进行以下实现:

package identityimport kotlinx.cinterop.toKString
import platform.posix.getlogin
import platform.posix.getpidactual fun buildIdentity() = Identity(getlogin()?.toKString() ?: "None",getpid().toLong()
)
2.使用接口

        在 CommonMain 中声明一个 Identity 接口和 buildIdentity 函数,供其他 Common Code 使用:

// In the commonMain source set:
expect fun buildIdentity(): Identityinterface Identity {val userName: Stringval processID: Long
}

        在不同平台中使用 actual 进行不同的实现,比如在 jvm 中进行以下实现:

// In the jvmMain source set:
actual fun buildIdentity(): Identity = JVMIdentity()class JVMIdentity(override val userName: String = System.getProperty("user.name") ?: "none",override val processID: Long = ProcessHandle.current().pid()
) : Identity

        在 POSIX 系统中进行以下实现:

// In the nativeMain source set:
actual fun buildIdentity(): Identity = NativeIdentity()class NativeIdentity(override val userName: String = getlogin()?.toKString() ?: "None",override val processID: Long = getpid().toLong()
) : Identity
http://www.dtcms.com/wzjs/489355.html

相关文章:

  • 什么蓝色 适合公司网站主色手机百度高级搜索
  • wordpress 仿站 主题创建网站平台
  • 360浏览器主页广州seo关键字推广
  • 海外推广助理seo优化有百度系和什么
  • 淄博一推网络科技有限公司seo网站推广简历
  • 网站建设会议讲话快速优化关键词排名
  • 松岗网站建设营销手机都有什么功能啊
  • 做网站怎么接广告赚钱做网站多少钱
  • 普通网站制作seo公司官网
  • 学校网站建设内容深圳百度开户
  • 服务好的南昌网站制作制作网页多少钱
  • 沧州网站制作信息流广告接单平台
  • wix做的免费网站可以用吗今日最新国内新闻重大事件
  • 苏州外贸营销网站建设首页关键词优化公司
  • 网站搜索排名怎么做网络运营推广怎么做
  • 如何创建外卖网站软文发布网站
  • 最炫表白网站html5源码搜索引擎调词平台多少钱
  • 该怎么做网站编辑主要做什么荥阳seo推广
  • 网站制作难不难口碑营销5t
  • 北京网站建设报价表链接式友谊
  • 广东网站建设人员关于搜索引擎的搜索技巧
  • 聊城手机网站建设会计培训机构
  • 宁夏信用建设官方网站网络媒体推广报价
  • 办公家具温州seo团队
  • 网站安排谷歌推广怎么样
  • 十大国外室内设计网站东莞百度推广优化公司
  • 建站系统源码下载网络推广公司收费标准
  • 教育培训网站建设ppt模板免费优化推广网站的软件
  • 网站备案 多少钱网站推广优化外包公司
  • 金科科技 做网站今日新闻最新头条10条内容