如何为软件包名称指定前缀。
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
名称映射
Java 使用软件包来非正式定义命名空间;Objective C++ 有 C++ 命名空间,
目标 C 则不然。为了在使用多个软件包中的类时保持名称的唯一性,J2ObjC 会在类型名称前面附加驼峰式软件包版本。例如,java.util.Map
重命名为 JavaUtilMap
。
遗憾的是,采用驼峰命名法的软件包名称可能会降低所生成代码的可读性,尤其是
使用较长的软件包名称例如,Google Guava 的
Beta 版注释
在 com.google.common.annotations
软件包中,而 ComGoogleCommonAnnotationsBeta
更难
比Beta
高。
定义软件包前缀
定义非正式命名空间的 Objective-C 惯例是使用共享前缀,通常为两个
大写字母。例如,iOS Foundation Framework 使用“NS”(来自 NeXTStep)。为了简化 Google Guava 的 Beta 版名称,可以使用“GG”等前缀,将 Beta
称为 GGBeta
,以提高可读性。
J2ObjC 支持开发者指定自己的前缀以映射到软件包名称。此操作在
使用 --prefix package=prefix
在命令行中运行。要缩短
Beta
的软件包“--prefix com.google.common.annotations=GG
”默认选项。单独的
每个软件包都需要进行前缀声明。
为多个软件包定义单个前缀
较小的库通常具有不会冲突的 Java 类名称,因此可以共用单个前缀
和带通配符的软件包规范例如,所有 Joda-Time 软件包都可以使用 --prefix
'org.joda.time.*=JT'
共享相同的 JT 前缀。唯一支持的通配符是“*”,其匹配方式与命令行 shell 对文件名的匹配方式相同。
定义多个软件包前缀
为了简化指定多个前缀定义,可以将属性文件与“--prefixes
file”(文件)参数搭配使用:
cat prefixes.properties
com.google.common.annotations: GG
com.google.common.base: GG
# While GG can be used for all packages, let's mix it up.
com.google.common.collect: GC
com.google.common.io: GIO # A prefix can be more than two characters,
com.google.common.net: GuavaNet # a lot more!
...
j2objc --prefixes prefixes.properties <args>
运行时带前缀的类
由于完成的应用具有带前缀的类,因此无法使用原始 Java 代码找到它们
类名称。但是,如果应用的资源中有一个名为 prefixes.properties 的文件
包含用于翻译的前缀的 bundle,Class.forName(javaName)
将找到映射的类。
如需在 Xcode 中将上述 prefixes.properties 添加到 iOS 应用,请打开 build 目标的“Build Phases”(构建阶段)标签页,展开其“Copy Bundle Resources”(复制软件包资源)部分,然后将 prefixes.properties 文件添加到该列表中。如需详细了解 Java 资源概念如何映射到 iOS 资源,请参阅 Java 资源。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-10-10。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2024-10-10。"],[[["J2ObjC automatically prepends camel-cased package names to Java types in Objective-C to avoid naming conflicts, but this can hinder readability."],["Developers can define custom prefixes for Java packages using the `--prefix` command-line argument to improve code readability."],["Wildcard characters can be used to assign a single prefix to multiple packages, simplifying the configuration for libraries with consistent naming."],["Multiple package prefixes can be defined within a properties file and applied using the `--prefixes` argument for more complex projects."],["Including a *prefixes.properties* file in the application's resources enables runtime lookup of prefixed classes using original Java class names."]]],["J2ObjC addresses name uniqueness by prepending camel-cased package names to Java class names, but this reduces readability. To improve this, developers can define prefixes for packages using the `--prefix` command-line option (e.g., `--prefix com.example.package=EP`). Wildcards (`*`) allow a single prefix for multiple packages. A properties file specified with `--prefixes` allows for multiple prefix definitions. If this file is in the app's resources, the app can locate the prefixed classes using their original Java names.\n"]]