2015-10-09 49 views
-1

我已经被迫最近使用SWIFT Objective-C语言的一些类使用Objective-C类,我已经搜查,这个发现Apple doc在斯威夫特类

要导入Objective-C代码为斯威夫特相同的目标

1)在您的Objective-C桥接头文件中,导入您想暴露给Swift的每个Objective-C 头。例如:

#import "XYZCustomCell.h" 
#import "XYZCustomView.h" 
#import "XYZCustomViewController.h" 

2)在生成设置,在夫特编译器 - 代码生成,确保目标C桥接报头 构建设置下具有对桥接报头文件的路径。路径 应该与您的项目有关,类似于在Build Settings中指定Info.plist 路径的方式。在大多数情况下,您不应该 需要修改此设置。

在 中列出的任何公共Objective-C头文件对于Swift都是可见的。 Objective-C 功能将自动在目标 内的任何Swift文件中提供,而不需要任何导入语句。使用您的自定义 Objective-C代码与您在系统 类中使用的Swift语法相同。

我跟着步骤并添加初始代码到我的SWIFT类:

let percentageView: FSPercentageView = FSPercentageView(frame: CGRectMake(15, 65, 300, 300)) 

但是我得到消息的错误:使用未声明的类型“FSPercentageView”的。

我在swift中搜索了使用objective-c的这个错误,但是我没有找到任何有用的答案。 我检查了桥头文件路径,它似乎很好。

我希望你的建议能解决我的问题。

UPDATE:

我的桥接-Header.h

#import "FSPercentageView.h" 

我FSPercentageView.h级别:

#import <Foundation/Foundation.h> 

#import "MCCore.h" 

@class FCPercentageView; 

typedef enum { 
    FSPercentageViewTextStyleAutomaticShowsPercentage, 
    FSPercentageViewTextStyleUserDefined 
} FSPercentageViewTextStyle; 



@interface FSPercentageView : MCNewCustomLayeredView 

/* 
Sets the percentage of the view. 
*/ 
@property (nonatomic) CGFloat percentage; 

/* 
Sets the percentage of the view. 
*/ 
@property (nonatomic) CGFloat initialPercentage; 

/* 
Defines the border percentage for both filled and unfilled portions. 
*/ 
@property (nonatomic) CGFloat borderPercentage; 

/* 
The label of the text in the center of the doughnut. 
*/ 
@property (nonatomic, retain) UILabel *textLabel; 

/* 
The color for the filled portion of the doughnut. 
*/ 
@property (nonatomic, retain) UIColor *fillColor; 

/* 
The color for the unfilled portion of the doughnut. 
*/ 
@property (nonatomic, retain) UIColor *unfillColor; 
} 

和FSPercentageView.m的初始段:

#import "FSPercentageView.h" 
#import "FSNewCustomLayeredView+FSCustomLayeredViewSubclass.h" 

typedef enum { 
    FSPercentageViewStateNormal, 
    FSPercentageViewStatePushed 
} FSPercentageViewTouchState; 

@interface FSPercentageView() 

@property (nonatomic, retain) UIView *centerView; 
@property (nonatomic) FSPercentageViewTouchState touchState; 

@end 

@implementation FSPercentageView 

- (void)setDefaults 
{ 
    [super setDefaults]; 

    self.linePercentage     = 0.15; 
    self.borderPercentage    = 0; 
    self.showTextLabel     = YES; 
    self.animationDuration    = 0.5; 
    self.unfillColor     = [MCUtil iOS7DefaultGrayColorForBackground]; 
    self.borderColorForFilledArc  = [UIColor blackColor]; 
    self.borderColorForUnfilledArc  = [UIColor clearColor]; 
    self.borderPercentageForFilledArc = -1; 
    self.borderPercentageForUnfilledArc = -1; 
    self.adjustsFontSizeAutomatically = YES; 
    self.roundedImageOverlapPercentage = 0; 
    self.touchState      = FSPercentageViewStateNormal; 
    self.gradientColor1     = [MCUtil iOS7DefaultBlueColor]; 
    self.gradientColor2     = [MCUtil iOS7DefaultGrayColorForBackground]; 

    _percentage       = 0.0; 
    _initialPercentage     = 0.0; 

    _centerView = [[UIView alloc] init]; 
    _textLabel = [[UILabel alloc] init]; 
} 

- (void)willDrawSublayers 
{ 
    if (!self.fillColor) { 
     self.fillColor = self.tintColor; 
    } 
} 

- (Class)classForSublayers { 
    return [MCSliceLayer class]; 
} 

和SWIFT代码:

let percentageView: FSPercentageView = FSPercentageView(frame: CGRectMake(15, 35, 289, 311)) 

示出了在上面的行中的错误。

+1

显示更多代码。显示桥接头。显示声明FSPercentageView的文件。 – matt

+0

哦,你可能想看看我的书,我认为这比Apple的文档更清晰:http://www.apeth.com/swiftBook/apa.html#SECbilingual – matt

+0

FSPercentageView.h应该以' @ end',不是花括号。 – matt

回答

1

我希望你的建议能解决我的问题。

但是你还没有提供任何信息,所以没有任何建议是可能的。苹果说的是真的;我向你保证这个功能确实有效。问题只能是你没有遵循你自己引用的方向。

+0

我已添加我的代码。你可以帮我吗? –

0

我发现我的代码有问题。

我在我的应用程序中有两个目标,我也使用测试单元。 由于在所有提到的目标中添加了我的swift代码,项目需要有3个与每个目标相关的桥接类,并且还必须导入其中的所有标题。其实红色的信息是关于单元测试桥接文件。

现在,我从测试单元中删除了我的swift类,并且代码编译和运行没有任何问题。