Objective C静态const in swift

时间:2015-10-11 06:29:21

标签: objective-c swift

如何在Swift中编写这个Objective C代码?

#import "FFTViewController.h"

//only this part
static vDSP_Length const FFTViewControllerFFTWindowSize = 4096;

@implementation FFTViewController 

我的想法是

let vDSP: vDSP_Length = 4096

我确定这不正确,但我不知道。

感谢您的帮助

更新

我希望将此Objective C代码移植到swift

#import "FFTViewController.h"

static vDSP_Length const FFTViewControllerFFTWindowSize = 4096;

@implementation FFTViewController


- (void)viewDidLoad
{
[super viewDidLoad];

//
// Setup the AVAudioSession. EZMicrophone will not work properly on iOS
// if you don't do this!
//
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
if (error)
{
    NSLog(@"Error setting up audio session category: %@", error.localizedDescription);
}
[session setActive:YES error:&error];
if (error)
{
    NSLog(@"Error setting up audio session active: %@", error.localizedDescription);
}

//
// Setup time domain audio plot
//
self.audioPlotTime.plotType = EZPlotTypeBuffer;
self.maxFrequencyLabel.numberOfLines = 0;

//
// Setup frequency domain audio plot
//
self.audioPlotFreq.shouldFill = YES;
self.audioPlotFreq.plotType = EZPlotTypeBuffer;
self.audioPlotFreq.shouldCenterYAxis = NO;

//
// Create an instance of the microphone and tell it to use this view controller instance as the delegate
//
self.microphone = [EZMicrophone microphoneWithDelegate:self];

//
// Create an instance of the EZAudioFFTRolling to keep a history of the incoming audio data and calculate the FFT.
//
self.fft = [EZAudioFFTRolling fftWithWindowSize:FFTViewControllerFFTWindowSize
                                     sampleRate:self.microphone.audioStreamBasicDescription.mSampleRate
                                       delegate:self];

//
// Start the mic
//
[self.microphone startFetchingAudio];
}

这是我的开始

import UIKit
import Accelerate


class ViewController: UIViewController, EZMicrophoneDelegate{

var mic: EZMicrophone!
var fft: EZAudioFFTRolling!
private let FFTViewControllerFFTWindowSize: vDSP_Length = 4096

override func loadView() {
    super.loadView()

    mic = EZMicrophone(delegate: self, startsImmediately: true)
    fft = EZAudioFFTRolling(windowSize: FFTViewControllerFFTWindowSize, sampleRate: mic.audioStreamBasicDescription().mSampleRate, delegate: self)


}


}

我成为以下错误:

编译失败,出现以下错误: "无法为类型' EZAudioFFTRolling'调用初始化程序。使用类型'的参数列表(windowSize:vDSP_Length,sampleRate:Float64,delegate:ViewController)'"

我的问题怎么样? 对不起这个问题我没有ObjectiveC背景,对我这个端口来说是一个小挑战。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您遇到错误的问题在于类型。当您在XCode中开始键入EZAudioFFTRolling初始化程序时,自动完成功能可以通过显示规范来帮助您。它看起来像这样:

EZAudioFFTRolling(windowSize: vDSP_Length, sampleRate: Float, delegate: EZAudioFFTDelegate!)

如果将规范中的参数类型与您传递的实际值进行比较,您将发现mic.audioStreamBasicDescription()。mSampleRate具有Float64类型但应该是Float(它们实际上并不相同)并且self不符合EZAudioFFTDelegate协议。在将您的行更改为此

之后
fft = EZAudioFFTRolling(windowSize: FFTViewControllerFFTWindowSize, sampleRate: Float(mic.audioStreamBasicDescription().mSampleRate), delegate: self)

并添加协议一致性,您将能够构建代码。

与Objective-C相比,Swift是类型安全,因此它是所有Objective-C开发人员在开始使用Swift时遇到的常见问题。但错误实际上对你有所帮助,所以请认真阅读。