问题描述
我有一个在Xcode 7上运行的Swift 2项目.我正在使用CocoaPods(带有use_frameworks!
的v0.38.2),并且已经设法使这些框架正常工作:
I have a Swift 2 project running on Xcode 7. I'm using CocoaPods (v 0.38.2 with use_frameworks!
) and have already managed to get those frameworks working:
- CryptoSwift
- Pluralize_swift
两个Pod都是用Swift编写的,因此到目前为止没有问题.它们发挥了魅力.
Both Pods are written in Swift, so no problems so far. They work as charm.
现在,我正在尝试使用用Objective-C编写的 SMPageControl .我已经看到可以在Swift 2中使用Obj-C Pods ,尝试了许多不同的组合,但我无法使其正常工作. SMPageControl
类不会显示自动完成建议,很显然,当我尝试使用它时,编译器会抛出 Use of undeclared type 'SMPageControl'
.
Now I'm trying to use SMPageControl which is written in Objective-C. I've seen it's possible to use Obj-C Pods with Swift 2, tried a lot of different combinations but I can't get it to work. SMPageControl
class doesn't show on autocomplete suggestions, and obviously when I try to use it the compiler throws Use of undeclared type 'SMPageControl'
.
到目前为止我所做的:
- 将
pod 'SMPageControl'
添加到了我的Podfile
- Ran
pod install
(我可以在Xcode项目树上看到Pod的源代码/框架) - 在构建阶段"中添加了
SMPageControl.framework
>将二进制文件与库链接"(就像我对其他可用框架所做的那样) - 创建了
PROJECT_NAME-Bridging-Header.h
并将其添加到项目的构建设置"中(我可以说它正在工作,因为在我正确导入SMPageControl
之前它抛出了错误) - 在上面的文件中添加了
#import <SMPageControl/SMPageControl.h>
- Added
pod 'SMPageControl'
to myPodfile
- Ran
pod install
(I can see the Pod source/framework on Xcode project tree) - Added
SMPageControl.framework
in Build Phases > Link Binary With Libraries (as I've done with the other frameworks that work) - Created
PROJECT_NAME-Bridging-Header.h
and added it on the project's Build Settings (I can tell it's working because it threw errors until I got to importSMPageControl
correctly) - Added
#import <SMPageControl/SMPageControl.h>
to the file above
到目前为止,项目编译没有错误,但是我无法在Swift类中使用SMPageControl
.在Swift文件中尝试了import SMPageControl
,但是没有运气(它甚至不会自动完成).我知道有可能,但是我没有找到任何示例代码.
Up to here the project compiles without errors, but I can't get to use SMPageControl
in my Swift classes. Tried import SMPageControl
in the Swift file, but no luck (it doesn't even autocomplete). I understood it's possible, but I didn't find any example codes.
真的有可能吗?如果是这样,我想念什么?
Is it really possible? If so, what am I missing?
推荐答案
最后使它起作用,事实证明我实际上是缺少某些东西.
Finally got it to work, it turns out that I was actually missing something.
完成上述所有步骤,需要import
Swift文件顶部的框架:
Along with all steps above, it's needed to import
the framework at the top of the Swift file:
import SMPageControl
由于Xcode没有提供任何自动完成功能,所以我认为不可能做到这一点.添加import
语句后,项目成功编译,并且可以使用SMPageControl()
类.
Since Xcode was not providing any autocomplete I thought it wouldn't be possible to do that. Once I added the import
statement, the project compiled successfully and I was able to use SMPageControl()
class.
这篇关于无法在Swift 2项目中使用Objective-C Pod框架(“使用未声明类型的'SMPageControl'")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!