如何指定套件名稱的前置字串。
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
名稱對應
Java 會使用套件來非正式定義命名空間;Objective C++ 有 C++ 命名空間,但 Objective C 則沒有。如要在使用多個套件中的類別時保留名稱不重複性,
J2ObjC 會在類型名稱中加上駝峰式大小寫版本的套件。舉例來說,java.util.Map
已重新命名為 JavaUtilMap
。
不幸的是,使用駝峰式命名的套件名稱會降低產生的程式碼可讀性,尤其是
名稱較長例如 Google Guava
Beta 版註解
位於 com.google.common.annotations
套件中,ComGoogleCommonAnnotationsBeta
不易
讀取超過 Beta
次。
定義套件前置字串
Objective-C 非正式命名空間的定義方式是使用共用的前置字元,通常是兩個
大寫字母。舉例來說,iOS 基礎架構會使用「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 前置字元。唯一支援的萬用字元是「*」,與
指令列殼層會使用檔案名稱。
定義多個套件前置字串
為簡化多個前置字元定義的指定作業,您可以使用屬性檔案搭配「--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 的檔案,
如果軟體包含有要翻譯的前置字串,Class.forName(javaName)
就會找到對應的類別。
如要在 Xcode 中將上述 prefixes.properties 新增至 iOS 應用程式,請開啟建構目標的建構階段
分頁,展開其「Copy Bundle Resources」部分,然後在該清單中新增 prefixes.properties。
Java 資源進一步說明 Java 資源概念與 iOS 的對應方式
再複習一下,機構節點
是所有 Google Cloud Platform 資源的根節點
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間: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"]],["上次更新時間: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"]]