xcode 在原始 iOS App 和 App Extension 之间共享代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28797242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:44:50  来源:igfitidea点击:

Sharing code between original iOS App and App Extension

iosxcodeios-app-extensionios8-extension

提问by Ruben Martinez Jr.

Sharing class between iOS app and extension app

iOS 应用和扩展应用之间的共享类

I want to use a custom class in both my app and the extension app. If I just have the class in the main app folder, the extension doesn't pick up on it. I tried making a copy of it and putting it into the extension folder, but I of course get a invalid redeclaration of [my class]error. What's the best solution for this? It's just one class, not multiple resources.

我想在我的应用程序和扩展应用程序中使用自定义类。如果我只在主应用程序文件夹中有该类,则扩展程序不会接受它。我尝试制作它的副本并将其放入扩展文件夹,但我当然得到一个invalid redeclaration of [my class]错误。什么是最好的解决方案?它只是一个类,而不是多个资源。

回答by Avt

Nothing should be copied. Just add required files to compile sources for your extension:

什么都不应该被复制。只需添加所需的文件即可为您的扩展编译源代码:

enter image description here

在此处输入图片说明

回答by Duc

Just make sure that the files you want to share gets a tick in the section Target Membership.

只需确保您要共享的文件在Target Membership部分中打勾。

Select your file, open the utility panel on the right and tick the appropriate target under Target Membership.

选择您的文件,打开右侧的实用程序面板,然后在Target Membership下勾选相应的目标。

enter image description here

在此处输入图片说明

回答by Praveen Matanam

Embedded frameworks are a great way to reuse code between your application and your extension. Having moved some of the code to embedded frameworks will provide us with a great opportunity to write some unit tests around it.

嵌入式框架是在应用程序和扩展之间重用代码的好方法。将一些代码移到嵌入式框架中将为我们提供一个很好的机会来围绕它编写一些单元测试。

See section Using an Embedded Framework to Share Codehttps://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1

请参阅使用嵌入式框架共享代码部分https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1