iOS:xcode 中Groups vs Folder References

本文详细介绍了在iOS开发中,xcode添加资源时的两种方式——Groups和Folder References的区别。Groups是xcode内部的组织形式,只影响xcode的目录结构,而Folder References则直接引用文件夹,移动或修改Group内的文件会影响实际存储位置。在多个Target环境下,需设置每个文件的目标成员资格。Folder References更符合文件在Finder中的实际位置,但外部修改可能导致引用断裂。选择Groups或Folder References需根据项目需求和文件管理便捷性来决定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

           前言:当xcode添加资源的时候,有俩种引用方式,分别为Groups 和 Folder Reference俩种方式。它们在xcode中显示也不同,一个是yellow,一个是blue。

 Groups

     A.Xcode只是保存一个对文件(夹)的引用,在xcode中对文件(夹)操作各种操作,只是影响xcode的目录结构,.xcodeproj文件而已,对它的实际存储位置几乎不做影响(如果引用的时候选了copy选项,实际存储在项目工程目录里,也没有影响),group是xcode内部的组织形式(目录结构)。

         1.在xcode中随意移动Groups的文件(夹),只是它在xcode中目录结构改变了,它的实际存储位置不受影响,原来存储什么位置,还是那个位置。

         2.在xcode中修改Groups的文件夹的名字,group名字变了,文件夹的实际存储名字不变,但如果修改Group的文件的名字,实际存储文件的名字会变。

         3.不管该group在xcode中如何移动,它对应的实际文件夹不变。

         4.Group的文件夹都会对应盘上的一个实际存储的文件夹位置。往这个group中新添加文件,该文件会保存到group对应的实际文件夹中。

            在哪个group1中里面new group,新的group对应的文件夹就是它所属group1对应的文件夹。比如在xcode根目录new group,group对应的就是project目录。往group里面添加文件就是往project目录里添加文件。在xcode target名字group中new group,对应的就是target的根文件夹。

            往xcode中添加文件夹同时创建的group,对应的就是该文件夹。该文件夹所存储位置就是所属group对应的文件夹。

      B.Group的引用方式,所属的文件都会参与编译(代码文件),资源文件(图片等)会copy到bundle包中(Xcode flattens the group hierarchy and dumps all the files in the top level of the product’s bundle. )。        

      问题

            1.当有多个target的时候,必须 set the proper target membership for each file。

            2.在finder中,项目的文件夹中可能有文件夹A,但是在x'code project中并没有文件夹A,Because of the mismatch, locating files can get confusing。

            3.If you move a file or rename it outside of Xcode, the reference breaks and the file turns red. File management becomes a real pain。

       优点:

            1.可以自由的选择which files on disk 添加到xcode中

            2.对target membership有个较好控制

            3.[UIImage imageNamed:@"GoPago"]  instead of  [UIImage imageNamed:@"Images/Icons/GoPago"]; 注意,不同的文件的名字冲突。

Folder References 

      A.folder references只能作为资源,整个引用进项目,相当一个独立体。在xcode中不能对它进行编辑。在它的存储位置上任何改动都会立刻反应到xcode中。
           1.不能往folder references文件夹中添加、删除文件,
           2.可以删除整个folder references文件夹。
           3.在folder references存储的文件夹中添加、删除、重命名文件,xcode相应改变。
      B.folder references不能编译代码,也就是说,以folder形式引用进来的文件,不能被放在complie sources列表里面。文件夹里面的东西都会原封不动的拷贝到 bundle 包(该文件夹以及子文件夹都会出现在bundle中)。
      优点:
            1.子文件夹自动添加,避免冲突:All of its files and subfolders are automatically added to the project. This keeps the project file smaller and simpler, with less chance of merge conflicts。
            2.同步变化:If you rename, delete, or move a file in the file system, Xcode automatically updates the folder reference to reflect the change. File management is thus far easier。
            3.xcode中目录跟disk上的目录一致,不会带来混淆
            4.不用当心name conflicts
         缺点:
            1.文件夹中所有东西都have membership,不能改动(有时也是优点)
            2.不能隐藏某一个文件(individual files) from the project navigator
            3.加载一个资源,必须写明整个路径:参考上文
            4.在IB中无法使用fole references里面资源。When editing, IB is able to find the image file and allows you to select it, but when it generates the XIB, it strips the folder location, which prevents it from being found.

Conclusion

                   
         The fact that Interface Builder can’t work with images in (sub)folder references is a deal breaker. And being able to select the target membership for individual files—a feature only provided by groups—can be very useful. For these reasons, you will find that groups are far more common in practice, and it is the option I recommend. You just have to live with the fact that if you move the files around on disk, you’ll have to manually update the Xcode project to match.
----------------------------
参考:
http://vocaro.com/trevor/blog/2012/10/21/xcode-groups-vs-folder-references/
http://www.th7.cn/Program/IOS/201408/269138.shtml

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值