xcode – 为什么我收到一个未申报的类型“PubNub”编译器错误与Swift Cocoa应用程序和桥接头?

前端之家收集整理的这篇文章主要介绍了xcode – 为什么我收到一个未申报的类型“PubNub”编译器错误与Swift Cocoa应用程序和桥接头?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开始一个新的 Cocoa Swift项目,它通过CocoaPods将PubNub SDK与以下Podfile结合在一起:
target 'myProject' do
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
pod 'PubNub','~>4.0'
pod 'Alamofire','~> 1.3'
end
target 'myProjectTests' do
end

在我自动生成的桥接头我有PubNub的导入为:

#import <PubNub/PubNub.h>

和我的AppDelegate.swift文件

import Cocoa

@NSApplicationMain


class AppDelegate: NSObject,NSApplicationDelegate {

var client:PubNub?

   func applicationDidFinishLaunching(aNotification: NSNotification) {
    let config = PNConfiguration( publishKey: "Your_Pub_Key",subscribeKey:     "Your_Sub_Key")

    client = PubNub.clientWithConfiguration(config)

    client?.addListener(self)

    client?.subscribetochannels(["Your_Channel"],withPresence: false)

    client?.publish("Swift + PubNub!",tochannel: "demo",compressed: false,withCompletion: nil)    }

func client(client: PubNub!,didReceiveMessage message: PNMessageResult!) {
    println(message)
}

func applicationWillTerminate(aNotification: NSNotification) {
    // Insert code here to tear down your application
}


}

由于使用未申报类型PubNub的编译器错误,项目无法构建.我检查了构建设置,Swift编译器 – 代码生成部分显示它指向目标的桥接头文件(自动填充).

使用Xcode 6.4和pods版本0.38.2

解决方法

导入外部框架时无桥接头

Apple Developer Documentation直:

You can import external frameworks that have a pure Objective-C codebase,a pure Swift codebase,or a mixed-language codebase. […] You can import a framework into any Swift file within a different target using the following Syntax:

import FrameworkName

固定

添加导入PubNub框架.

import UIKit
import PubNub

@UIApplicationMain
class AppDelegate: UIResponder,UIApplicationDelegate {

    var window: UIWindow?
    var client:PubNub?
    // ...
}

通过单一导入,PubNub被声明,在Xcode编辑器中自动完成,编译,链接,构建和运行.

一步一步的Swift框架教程

由于以下许多评论意味着桥接头总是需要,所以当使用外部框架时,使用use_frameworks的情况是错误的!指令在Podfile中,在这里找到一个纯粹的Swift解决方案.接下来是一个可以下载和体验的Xcode项目.

iOS Developer Library中明确记录,概念中使用Swift与可可和Objective-C,“混合和匹配”一章,同一项目中的Swift和Objective-C部分引入外部框架:

The process for importing an external framework is the same whether the framework is written in a single language or contains files from both languages.

Podfile

platform :ios,'8.0'
use_frameworks!

target 'SO-31642385' do
pod 'PubNub','~> 1.3'
end

安装pod

] pod install

Downloading dependencies
Installing Alamofire (1.3.1)
Installing CocoaLumberjack (2.0.0)
Installing PubNub (4.0.4)
Generating Pods project
Integrating client project

Please close any current Xcode sessions and use `SO-31642385.xcworkspace` for this project from now on.

导入框架

import UIKit
import PubNub

@UIApplicationMain
class AppDelegate: UIResponder,UIApplicationDelegate {

    var window: UIWindow?
    var client:PubNub?

    func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions:
                         [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        self.client = PubNub()
        return true
    }
    // ...
}

►在GitHub查找此解决方案,并在Swift Recipes上查看更多详细信息.

猜你在找的iOS相关文章