简体   繁体   中英

Using Objective-c classes in Swift Classes

I have forced to use some classes of objective-c language in swift recently, I have searched and found this Apple doc :

To import Objective-C code into Swift from the same target

1) In your Objective-C bridging header file, import every Objective-C header you want to expose to Swift. For example:

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

2) In Build Settings, in Swift Compiler - Code Generation, make sure the Objective-C Bridging Header build setting under has a path to the bridging header file. The path should be relative to your project, similar to the way your Info.plist path is specified in Build Settings. In most cases, you should not need to modify this setting.

Any public Objective-C headers listed in this bridging header file will be visible to Swift. The Objective-C functionality will be available in any Swift file within that target automatically, without any import statements. Use your custom Objective-C code with the same Swift syntax you use with system classes.

I followed steps and added initial code to my swift class:

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

But I got an error with message: Used of undeclared type 'FSPercentageView'.

I have searched this error related using objective-c in swift but I did not find any useful answer. I checked the bridge header file path and It seems to be fine.

I hope your suggestion fix my problem.

UPDATE:

My Bridging-Header.h

#import "FSPercentageView.h"

My Class of 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;
}

and the initial segment of 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];
}

and the swift code:

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

Shows the error in above line.

I hope your suggestion fix my problem.

But you have not given any information, so no suggestion is possible. What Apple says is true; I assure you that this feature does work. The problem can only be that you have not followed the directions that you yourself have quoted.

I found the problem which my code has.

I have two targets in my app and also I am using Test Unit. Because of adding my swift code in all mentioned targets, the project was needed to have 3 bridging class related to each target and also the headers must be imported in all of them. Actually the red message was about unit test bridging file.

Right now, I removed my swift class from Test Unit and The code is compiling and running without any problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM