簡體   English   中英

從Swift調用Objective C方法

[英]Call an Objective C method from Swift

我正在Swift中構建一個導入一些.h文件的應用程序。 我有以下定義:

RTCEAGLVideoView.h

@interface RTCEAGLVideoView : UIView <RTCVideoRenderer>

   @property(nonatomic, weak) id<RTCEAGLVideoViewDelegate> delegate;

@end

RTCVideoRenderer.h

@protocol RTCVideoRenderer<NSObject>

// The size of the frame.
- (void)setSize:(CGSize)size;

// The frame to be displayed.
- (void)renderFrame:(RTCI420Frame*)frame;

@end

現在,在ViewController中,我有一個RTCEAGLVideoView類型的對象,我想調用renderFrame方法。

@IBOutlet weak var remoteView: RTCEAGLVideoView!
...
self.remoteView.renderFrame(nil)

但我收到此錯誤:

Value of type 'RTCVideoRenderer' has no member 'renderFrame'

任何幫助,將不勝感激。

編輯:RTCI420Frame.h

#import <Foundation/Foundation.h>

// RTCI420Frame is an ObjectiveC version of cricket::VideoFrame.
@interface RTCI420Frame : NSObject

@property(nonatomic, readonly) NSUInteger width;
@property(nonatomic, readonly) NSUInteger height;
@property(nonatomic, readonly) NSUInteger chromaWidth;
@property(nonatomic, readonly) NSUInteger chromaHeight;
@property(nonatomic, readonly) NSUInteger chromaSize;
// These can return NULL if the object is not backed by a buffer.
@property(nonatomic, readonly) const uint8_t* yPlane;
@property(nonatomic, readonly) const uint8_t* uPlane;
@property(nonatomic, readonly) const uint8_t* vPlane;
@property(nonatomic, readonly) NSInteger yPitch;
@property(nonatomic, readonly) NSInteger uPitch;
@property(nonatomic, readonly) NSInteger vPitch;

- (BOOL)makeExclusive;

#ifndef DOXYGEN_SHOULD_SKIP_THIS
// Disallow init and don't add to documentation
- (id)init __attribute__((
    unavailable("init is not a supported initializer for this class.")));
#endif /* DOXYGEN_SHOULD_SKIP_THIS */

@end

我嘗試復制您的問題,但使用虛擬文件對我來說效果很好。 我懷疑您的問題不是橋接所有相關標頭。

以下是對我有用的文件:

RTCEAGLVideoView.h
----

#import <UIKit/UIKit.h>
#import "RTCEAGLVideoViewDelegate.h"
#import "RTCEAGLVideoView.h"
#import "RTCVideoRenderer.h"

@interface RTCEAGLVideoView : UIView <RTCVideoRenderer>

@property(nonatomic, weak) id<RTCEAGLVideoViewDelegate> delegate;

@end



RTCEAGLVideoView.m
----

#import "RTCEAGLVideoView.h"

@implementation RTCEAGLVideoView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)renderFrame:(RTCI420Frame*)frame {
    NSLog(@"Render frame was called");
}

- (void)setSize:(CGSize)size {
    NSLog(@"Size set");
}

@end




RTCEAGLVideoViewDelegate.h
----

#import <Foundation/Foundation.h>

@protocol RTCEAGLVideoViewDelegate <NSObject>

@end




RTCI420Frame.h
----

#import <Foundation/Foundation.h>

@interface RTCI420Frame : NSObject

@end



RTCI420Frame.m
----

#import "RTCI420Frame.h"

@implementation RTCI420Frame

@end



RTCVideoRenderer.h
----

#import <Foundation/Foundation.h>
#import "RTCI420Frame.h"

@protocol RTCVideoRenderer <NSObject>

// The size of the frame.
- (void)setSize:(CGSize)size;

// The frame to be displayed.
- (void)renderFrame:(RTCI420Frame*)frame;

@end



RenderThing-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 "RTCEAGLVideoViewDelegate.h"
#import "RTCEAGLVideoView.h"
#import "RTCVideoRenderer.h"




ViewController.swift
----


import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var remoteView: RTCEAGLVideoView!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        self.remoteView.renderFrame(nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

請特別注意RenderThing-Bridging-Header.h文件中的差異

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM