简体   繁体   English

两个Xcode项目,相同的目标

[英]Two Xcode projects, same target

I am working on version 2 of an iOS app. 我正在开发iOS应用程序的版本2。 Since I am rewriting it in Swift, I started a new Xcode project. 自从我用Swift重写它以来,我开始了一个新的Xcode项目。 Even though the target names are the same, I get two different apps on my device or simulator, so they don't share data and preferences, etc. 即使目标名称相同,我在设备或模拟器上还是得到了两个不同的应用程序,因此它们不会共享数据和首选项等。

Is it possible to have two different projects share the same target? 是否可以使两个不同的项目共享相同的目标? Or should I brute-force replace everything (after a triple backup :) in the original project folder? 还是应该用蛮力替换原始项目文件夹中的所有内容(三次备份后:)?

No, it's not possible. 不,不可能。 Better update original project. 更好地更新原始项目。

I spend a couple of days and many tries to get this to work, and am outlining below the steps I took. 我花了几天时间,尝试了很多方法来使它正常工作,并概述了我所采取的步骤。 Hopefully this can be useful to others as well. 希望这对其他人也有用。

Background: To learn Swift and also modernize my app, I decided to completely re-write it, using all the goodies of Swift and the newer iOS APIs. 背景:为了学习Swift并使我的应用程序现代化,我决定使用Swift的所有优点和更新的iOS API完全重新编写它。 Instead of slowly converting one class from Objective C to Swift, as it is described in many tutorials, I just started from scratch. 我没有像许多教程中所描述的那样将一个类从Objective C缓慢地转换为Swift,而是从头开始。 So I made a new project, with the same name of the target, added some basic functionality, and ran it on the simulator. 因此,我创建了一个与目标名称相同的新项目,添加了一些基本功能,并在模拟器上运行了该项目。 To my surprise, or maybe not, a new app was created, so I had two apps, one written in Objective C, and the new one in Swift. 令我惊讶的是,也许没有,我创建了一个新应用程序,所以我有两个应用程序,一个是用Objective C编写的,另一个是用Swift编写的。 Since my app uses Core Data, they need to be the same, otherwise the user will lose all his/her data when upgrading. 由于我的应用使用核心数据,因此它们必须相同,否则用户在升级时会丢失所有数据。 So with a lot of trial and error I was finally able to get this to work, here are the steps I used: 因此,经过大量的试验和错误,我终于能够使它起作用,这是我使用的步骤:

Disclaimer: use at your own risk - make sure to have backups so you can always revert. 免责声明:使用风险自负-确保备份,以便您始终可以还原。

  1. Make sure both projects have git enabled. 确保两个项目都启用了git。
  2. Make backups for both projects. 为两个项目进行备份。 If you are syncing with Dropbox or something similar, turn that off. 如果您要与Dropbox或类似的东西同步,请关闭它。
  3. In the ObjC project repository, create a 'Swift' branch. 在ObjC项目存储库中,创建一个“ Swift”分支。
  4. In Xcode, import all source files form the Swift project to the ObjC project (on the Swift branch). 在Xcode中,将所有源文件从Swift项目导入到ObjC项目(在Swift分支上)。 For easier housekeeping, I recommend to keep everything in separate groups inside Xcode. 为了简化内务处理,我建议将所有内容保留在Xcode中的不同组中。
  5. Under 'Build Phases' in the ObjC project (Swift branch), remove all ObjC related source files and linked frameworks. 在ObjC项目(Swift分支)的“ Build Phases”下,删除所有与ObjC相关的源文件和链接的框架。
  6. Build and run. 生成并运行。
  7. You may have some project related errors and will need to fix that. 您可能会遇到一些与项目相关的错误,需要进行修复。 For instance in my case Xcode couldn't find the info.plist file and I had to remove a build script. 例如,在我的案例中,Xcode找不到info.plist文件,因此我必须删除构建脚本。 YMMV. 因人而异。
  8. Commit the changes on git (still on the Swift branch). 在git上提交更改(仍在Swift分支上)。
  9. Switch between the Swift and ObjC branch and make sure that there is indeed only one build on the simulator and that they share data (or whatever is specific for your project). 在Swift和ObjC分支之间切换,并确保在模拟器上确实只有一个构建,并且它们共享数据(或项目中特定的任何数据)。

Optional if you want to have two different projects: 如果要具有两个不同的项目,则为可选:

  1. Switch to the Swift branch and quit Xcode. 切换到Swift分支并退出Xcode。
  2. Make a copy of the whole project folder and name this eg MyApp_Swift, but don't rename the target and pbxproj file. 复制整个项目文件夹并将其命名为MyApp_Swift,但不要重命名目标文件和pbxproj文件。
  3. Open the original project, and switch back to the ObjC branch, run and test. 打开原始项目,然后切换回ObjC分支,运行并测试。
  4. Open the copied project, and switch to the Swift branch, run and test. 打开复制的项目,然后切换到Swift分支,运行并测试。
  5. Delete (to trash) all ObjC related files from the new Swift project, run and test. 从新的Swift项目中删除(删除)所有与ObjC相关的文件,运行并测试。
  6. Quit Xcode, and move the .git folder from the original Swift project folder to the new Swift project folder. 退出Xcode,然后将.git文件夹从原始Swift项目文件夹移动到新的Swift项目文件夹。 I did this in the Terminal. 我在航站楼做到了。
  7. Open the new Swift project, run and test. 打开新的Swift项目,运行并测试。 If everything is ok, commit the changes. 如果一切正常,请提交更改。
  8. Switch back to the ObjC branch of the original project, run and test. 切换回原始项目的ObjC分支,运行并测试。 If everything is ok, you can delete the Swift branch in git. 如果一切正常,则可以在git中删除Swift分支。
  9. You should now have two different projects, both use the same app on the simulator. 您现在应该有两个不同的项目,都在模拟器上使用相同的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM