Why am I getting an undeclared type 'PubNub' compiler error with Swift Cocoa App and bridging header?

白昼怎懂夜的黑 提交于 2019-11-28 02:55:12

问题


I am starting a new Cocoa Swift Project that is incorporating the PubNub SDK via CocoaPods with the following 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

In my auto-generated bridging header I have the import for PubNub as:

#import <PubNub/PubNub.h>

And my AppDelegate.swift file:

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
}


}

The project fails to build due to compiler errors on use of undeclared type PubNub. I've checked the build settings and the Swift Compiler - Code Generation section shows it's pointed to the bridging header file of the target (auto-populated).

Using Xcode 6.4 and pods version 0.38.2


回答1:


No Bridging-Header when Importing External Frameworks

Straight from 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

Fix

Add import PubNub framework.

import UIKit
import PubNub

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

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

With a single import, PubNub is declared, auto-completes in Xcode editor, compiles, links, builds and runs.


Step-by-step Swift Framework Tutorial

Since many comments below imply that Bridging-Headers are always required, wrongly so when using External Frameworks as is presently the case with the use_frameworks! directive in the Podfile, find here a pure Swift solution. It is followed by an Xcode project you can download and experience with.

Unambiguously documented in the iOS Developer Library, in concept Using Swift with Cocoa and Objective-C, chapter Mix and Match, section Swift and Objective-C in the Same Project, paragraph Importing External Frameworks:

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', '~>4.0'
pod 'Alamofire', '~> 1.3'
end

Install the pods

] 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 Framework

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
    }
    // ...
}

► Find this solution on GitHub and additional details on Swift Recipes.



来源:https://stackoverflow.com/questions/31642385/why-am-i-getting-an-undeclared-type-pubnub-compiler-error-with-swift-cocoa-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!