패키지 이름의 접두사를 지정하는 방법
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
이름 매핑
Java는 패키지를 사용하여 비공식적으로 네임스페이스를 정의합니다. Objective C++에는 C++ 네임스페이스가 있지만
Objective C에서는 그렇지 않습니다. 여러 패키지의 클래스를 사용할 때 이름 고유성을 유지하려면
J2ObjC는 유형 이름 앞에 카멜 표기법으로 된 패키지 버전을 추가합니다. 예를 들어
java.util.Map
에서 JavaUtilMap
로 이름이 변경되었습니다.
안타깝게도 카멜 표기법을 사용한 패키지 이름은 특히 생성된 코드의 가독성을 떨어뜨릴 수 있습니다.
긴 패키지 이름이 있는 것을 볼 수 있습니다. 예를 들어 Google Guava의
베타 주석
이(가) com.google.common.annotations
패키지에 있으며, ComGoogleCommonAnnotationsBeta
는
Beta
보다 읽음
패키지 접두사 정의
비공식 네임스페이스를 정의하는 Objective-C 규칙은 공유 접두사(일반적으로 두 개)를
대문자입니다. iOS 기반 프레임워크에서 'NS' 사용 (제공:
NeXTStep)을 입력할 수 있습니다. Google Guava 베타 버전 간소화를 위해
이름, 'GG'와 같은 접두사 Beta
를 GGBeta
로 표시하여 가독성을 개선합니다.
J2ObjC는 개발자가 패키지 이름에 매핑할 자체 접두사를 지정하도록 지원합니다. 이 작업은
--prefix package=prefix
를 사용하여 명령줄에서 모든 클래스 이름을
Beta
님의 패키지, "--prefix com.google.common.annotations=GG
" 사용됩니다. 별도의
접두어 선언이 필요합니다.
여러 패키지의 단일 접두사 정의
작은 라이브러리는 충돌하지 않는 Java 클래스 이름을 가지는 경우가 많으므로 단일 접두사를 공유할 수 있습니다.
와일드 카드가 있는 패키지 사양으로 바꿉니다. 예를 들어 모든 Joda-Time 패키지는 --prefix
'org.joda.time.*=JT'
를 사용하여 동일한 JT 접두사를 공유할 수 있습니다. 지원되는 유일한 와일드 카드 문자는 '*'이며, 이는
명령줄 셸은 파일 이름으로 작동합니다.
여러 패키지 접두사 정의
여러 접두사 정의를 간단하게 지정하기 위해 속성 파일을
"--prefixes
파일" 인수:
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에서 iOS 앱에 위의 prefixes.properties를 추가하려면 빌드 대상의 빌드 단계를 엽니다.
탭에서 번들 리소스 복사 섹션을 펼친 후 해당 목록에 prefixes.properties 파일을 추가합니다.
자바 리소스 개념이 iOS에 매핑되는 방식에 관한 자세한 내용은 자바 리소스를 참고하세요.
리소스를 배포합니다
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2025-07-25(UTC)
[[["이해하기 쉬움","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"]],["최종 업데이트: 2025-07-25(UTC)"],[[["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"]]