objective-c Swift 项目中未声明类型的使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24627598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use of undeclared type in Swift project
提问by AndrewShmig
I am trying to import this libraryin my Swift project. I am doing all step by step from this documentand this answer, but nothing works.
我正在尝试在我的 Swift 项目中导入这个库。我正在从这个文档和这个答案中一步一步地做所有的事情,但没有任何效果。
Here is my screenshot:

这是我的截图:

Here is my Bridging-Header.h:
这是我的 Bridging-Header.h:
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import <UIKit/UIKit.h>
#import "VKUser.h"
#import "VKAccessToken.h"
#import "VKCache.h"
#import "VKStorage.h"
#import "VKStorageItem.h"
#import "VKRequestManager.h"
#import "VKRequest.h"
#import "VKConnector.h"
#import "VKMethods.h"
#import "NSData+toBase64.h"
#import "NSString+Utilities.h"
The important thing is that I have VKConnector class and VKConnectorDelegate protocol in one file. Maybe thats the problem?
重要的是我在一个文件中有 VKConnector 类和 VKConnectorDelegate 协议。也许这就是问题所在?
//
// Copyright (c) 2013 Andrew Shmig
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "VKMethods.h"
#import "VKAccessToken.h"
#import "VKStorage.h"
#import "NSString+Utilities.h"
#import "VKStorageItem.h"
@class VKConnector;
static NSString *const kVKErrorDomain = @"kVkontakteErrorDomain";
typedef enum
{
kVKApplicationWasDeletedErrorCode
} kVkontakteErrorCode;
/** Protocol incapsulates methods that are triggered during user authorization
process or access token status changes.
*/
@protocol VKConnectorDelegate <NSObject>
@optional
/**
@name Show/hide web view
*/
/** Method is called when user needs to perform some action (enter login and
password, authorize your application etc)
@param connector VKConnector instance that sends notifications
@param webView UIWebView that displays authorization page
*/
- (void)VKConnector:(VKConnector *)connector
willShowWebView:(UIWebView *)webView;
/** Method is called when UIWebView should be hidden, this method is called after
user has entered login+password or has authorized an application (or pressed
cancel button etc).
@param connector VKConnector instance that sends notifications
@param webView UIWebView that displays authorization page and needs to be hidden
*/
- (void)VKConnector:(VKConnector *)connector
willHideWebView:(UIWebView *)webView;
/**
@name UIWebView started/finished loading a frame
*/
/** Method is called when UIWebView starts loading a frame
@param connector VKConnector instance that sends notifications
@param webView UIWebView that displays authorization page
*/
- (void)VKConnector:(VKConnector *)connector
webViewDidStartLoad:(UIWebView *)webView;
/** Method is called when UIWebView finishes loading a frame
@param connector VKConnector instance that sends notifications
@param webView UIWebView that displays authorization page
*/
- (void) VKConnector:(VKConnector *)connector
webViewDidFinishLoad:(UIWebView *)webView;
/**
@name Access token
*/
/** Method is called when access token is successfully updated
@param connector VKConnector instance that sends notifications
@param accessToken updated access token
*/
- (void) VKConnector:(VKConnector *)connector
accessTokenRenewalSucceeded:(VKAccessToken *)accessToken;
/** Method is called when access token failed to be updated. The main reason
could be that user denied/canceled to authorize your application.
@param connector VKConnector instance that sends notifications
@param accessToken access token (equals to nil)
*/
- (void) VKConnector:(VKConnector *)connector
accessTokenRenewalFailed:(VKAccessToken *)accessToken;
/**
@name Connection & Parsing
*/
/** Method is called when connection error occurred during authorization process.
@param connector VKConnector instance that sends notifications
@param error error description
*/
- (void)VKConnector:(VKConnector *)connector
connectionError:(NSError *)error;
/** Method is called if VK application was deleted.
@param connector VKConnector instance that sends notifications
@param error error description
*/
- (void) VKConnector:(VKConnector *)connector
applicationWasDeleted:(NSError *)error;
@end
/** The main purpose of this class is to process user authorization and obtain
access token which then will be used to perform requests from behalf of current
user.
Example:
[[VKConnector sharedInstance] startWithAppID:@"12345567"
permissions:@[@"wall"]
webView:webView
delegate:self];
*/
@interface VKConnector : NSObject <UIWebViewDelegate>
/**
@name Properties
*/
/** Delegate
*/
@property (nonatomic, weak, readonly) id <VKConnectorDelegate> delegate;
/** Application's unique identifier
*/
@property (nonatomic, strong, readonly) NSString *appID;
/** Permissions
*/
@property (nonatomic, strong, readonly) NSArray *permissions;
/**
@name Class methods
*/
/** Returns shared instances of VKConnector class.
*/
+ (id)sharedInstance;
/**
@name User authorization
*/
/** Starts user authorization process.
@param appID application's unique identifier
@param permissions array of permissions (wall, friends, audio, video etc)
@param webView UIWebView which will be used to display VK authorization page
@param delegate delegate which will receive notifications
*/
- (void)startWithAppID:(NSString *)appID
permissons:(NSArray *)permissions
webView:(UIWebView *)webView
delegate:(id <VKConnectorDelegate>)delegate;
/**
@name Cookies
*/
/** Removes all cookies which were obtained after user has authorized VK
application. This method is used to log out current user.
*/
- (void)clearCookies;
@end
I have tried to split VKConnector header file into two - VKConnector class and VKConnectorDelegate, but that didn't work.
我试图将 VKConnector 头文件分成两个 - VKConnector 类和 VKConnectorDelegate,但这没有用。
What am I doing wrong?
我究竟做错了什么?
回答by Essan Parto
Your delegate function name is VKConnectorand you also have a class named VKConnector. That's your conflict. In Objective C your delegate method is VKConnector:withBool:but in Swift it's simply VKConnectorand withBool is not a part of the name.
您的委托函数名称是,VKConnector并且您还有一个名为VKConnector. 那是你的冲突。在 Objective C 中,您的委托方法是,VKConnector:withBool:但在 Swift 中它很简单,VKConnector而 withBool 不是名称的一部分。
If you follow Cocoa patterns, your delegate method should be called - (void) connector:(VKConnector *)connector withBool:(BOOL)boolean;
如果你遵循 Cocoa 模式,你的委托方法应该被调用 - (void) connector:(VKConnector *)connector withBool:(BOOL)boolean;
回答by mic
Did XCode create your bridging header file or did you create the file by yourself?
是 XCode 创建了您的桥接头文件还是您自己创建了该文件?
If you created the bridging header file yourself, make sure that build settings point to your file:
如果您自己创建了桥接头文件,请确保构建设置指向您的文件:


回答by matt
The "undeclared type" error in a mixed project is nearly always solved as I have explained here:
正如我在此处解释的那样,混合项目中的“未声明类型”错误几乎总能解决:
https://stackoverflow.com/a/24216718/341994
https://stackoverflow.com/a/24216718/341994
Basically, wherever you are importing the automatically generated "...-Swift.h"header file into your Objective-C code, you will need to import "VKConnector.h"into that file as well, earlier in the list of imports.
基本上,无论您将自动生成的"...-Swift.h"头文件导入到您的 Objective-C 代码中,您都需要导入"VKConnector.h"到该文件中,在导入列表的前面。
This is counterintuitive and annoying, but it solves the problem, and in fact is actually documented if you look very closely.
这是违反直觉和烦人的,但它解决了问题,实际上,如果您仔细观察,实际上已经记录在案。
回答by treeba
If it helps anyone, on our project this issue was caused because we had a bridging header on the main project target and a bridging header on an extension target.
如果它对任何人有帮助,在我们的项目中,这个问题是因为我们在主项目目标上有一个桥接头,在扩展目标上有一个桥接头。
Our extension target was using a class defined in our main project. This class was defined inside the extension bridging header and was working for the majority of our classes.
我们的扩展目标是使用在我们的主项目中定义的类。这个类是在扩展桥接头中定义的,并且适用于我们的大多数类。
However when we gave target membership of one of our classes to the main project target we were getting this error in that single file. The fix was to ensure the files used were in both bridging header files.
然而,当我们将我们的一个类的目标成员资格赋予主项目目标时,我们在该单个文件中收到此错误。修复是为了确保使用的文件在两个桥接头文件中。

