2014-07-08 69 views
7

我試圖在Swift項目中導入this library。我正在從this documentthis answer一步一步完成,但沒有任何效果。在Swift項目中使用未聲明類型

這裏是我的screenshotenter image description here

這裏是我的橋接-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" 

重要的是,我有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 

我試圖VKConnector頭文件分割成兩個 - VKConnector類和VKConnectorDelegate,但沒有奏效。

我在做什麼錯?

+0

您是否將必需的標題(VkontakteSDK.h)導入了swift橋接標頭? –

+0

檢查橋接頭。 – CW0007007

+0

@Anil,是的,我已經導入了VkontakteSDK。但它沒有工作,之後,我試圖導入所有在VkontakteSDK.h頭文件中導入的頭文件 - 什麼也沒有... – AndrewShmig

回答

11

您的代理函數名稱爲VKConnector,並且您還有一個名爲VKConnector的類。那是你的衝突。在Objective C中,你的委託方法是VKConnector:withBool:,但是在Swift中它只是VKConnector而withBool不是名稱的一部分。

如果按照可可的模式,你的委託方法應該叫- (void) connector:(VKConnector *)connector withBool:(BOOL)boolean;

+0

但我沒有使用除UIKit和Foundation之外的任何其他框架爲什麼它在ObjC項目中工作,但在Swift項目中不工作 – AndrewShmig

+0

查看已更新的答案 –

+0

好吧,讓我們假設我不喜歡沒有一些庫的源代碼,我可以在Swift項目中如何使用ObjC代碼(在測試項目和SDK中)顯示你的代碼? 謝謝你的幫助,這真的很有道理。 。 – AndrewShmig

2

XCode是否創建了橋接頭文件,或者您自己創建了文件?

如果您創建的橋接報頭文件自己,確保構建設置指向文件:

enter image description here

+0

請再讀一遍我的問題。有兩個鏈接:從文檔和從SO,並且肯定我已經在我的項目中添加了設置(由我自己和XCode也) – AndrewShmig

+0

您提醒我我需要添加導入到我的橋接文件 - 解決:) – OhadM

1

在混合項目中的「未申報類型」的錯誤幾乎總是解決我已經解釋過在這裏:

https://stackoverflow.com/a/24216718/341994

基本上,無論你是進口自動生成"...-Swift.h"頭文件到您的Objective-C代碼,你將需要導入"VKConnector.h"到該文件中,以及之前在進口清單中。

這是違反直覺和煩人的,但它解決了這個問題,事實上,如果你仔細觀察,它實際上是記錄在案的。

+0

謝謝,但它不起作用:(我甚至在Apple開發論壇上創建了這個主題:https://devforums.apple.com/message/1002447和測試項目(https://yadi.sk/d/QxPhrnysWFDGt)在哪裏你可以看到它沒有工作 – AndrewShmig

1

如果有幫助的人,對我們的項目,因爲我們對主要的項目目標上橋接報這個問題被引起的,上一個橋接報擴展目標。

我們的擴展目標是使用我們主項目中定義的類。該類是在擴展橋接頭中定義的,並且適用於大多數類。

但是,當我們將某個類的目標成員資格授予主項目目標時,我們在該單個文件中收到此錯誤。修復是爲了確保使用的文件都在橋接頭文件中。

+0

我有同樣的情況,兩個目標,並試圖將我的Objective-C項目導入另一個Swift類目標。你能解釋一下嗎,也許它會解決我的問題.. –

相關問題