CocoaPods使用Bundle的注意点

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_18683985/article/details/87899663

我在写CHDatePickerView框架的时候用到了Bundle来存放本地化语言.
并且我使用了的普通方式[NSBundle mainBundle]获取Bundle路径.

###问题1.

获取到的Bundle为nil

这里我看了一下我pod进来的框架.发现没有该Bundle.

需要在xxx.podspec文件中引用路径.

	s.resource  = 'CHDatePickerView/CHDatePickerLocalizable.bundle'

=后面的是相对路径.写上的话Bundle就会在Pod的文件里头.

###问题2.

获取到的Bundle为nil…

对,你没看错.获取到的Bundle还是为nil.然而这次是由于Bundle获取代码的问题.

// MARK: 旧的获取方式
[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"CHDatePickerLocalizable.bundle" ofType:@""]]
// MARK: 新的获取方式
[NSBundle bundleWithPath:[[NSBundle bundleForClass:[CHDatePickerView class]] pathForResource:@"CHDatePickerLocalizable" ofType:@"bundle"]];

虽说都是用的bundleWithPath.然而之前的是mainBundle.指的是工程路径下的.CocoaPods路径不算.后改动为bundleForClass:可以根据Class类来获取同路径下的Bundle路径.

这样就可以在CocoaPods中正常使用Bundle了.

猜你喜欢

转载自blog.csdn.net/qq_18683985/article/details/87899663
今日推荐