self.filter = [[FilterClass alloc] init];在斯威夫特? self.filter = FilterClass.self?

时间:2015-04-21 00:38:11

标签: objective-c swift

我将一些Obj-C转换为Swift,并且我遇到了一些初始化程序的问题。我已经把注释放在我不清楚init方法的地方。其次,我得到很多"不能下标类型的值' Double'索引类型' int'"像http://i.imgur.com/AXs0bdo.png这样的地方出错。你能帮我清理一下我的代码吗?提前谢谢。

import UIKit
import Foundation
import AVFoundation


let minFramesForFilterToSettle = 10

enum CurrentState {
case statePaused
case stateSampling
}

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {


let session = AVCaptureSession()
var camera : AVCaptureDevice?
var validFrameCounter: Int = 0
var pulseDetector = PulseDetector.self              // Is this initialized correctly?
var filter = Filter.self                            // Is this initialized correctly?
var currentState = CurrentState.stateSampling       // Is this initialized correctly?

override func viewDidLoad() {
    super.viewDidLoad()
    //self.filter = Filter()                        // Do I need this?
    //self.pulseDetector = PulseDetector.self       // Do I need this?
    startCameraCapture() // call to un-used function. TO DO create function
}

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



}

let NZEROS = 10
let NPOLES = 10

class Filter {

var xv = [Float](count: NZEROS + 1, repeatedValue: 0)
var yv = [Float](count: NPOLES + 1, repeatedValue: 0)

func processValue(value: Float) -> Float {

    let gain: Float = 1.894427025e+01

    xv[0] = xv[1]; xv[1] = xv[2]; xv[2] = xv[3]; xv[3] = xv[4]; xv[4] = xv[5]; xv[5] = xv[6]; xv[6] = xv[7]; xv[7] = xv[8]; xv[8] = xv[9]; xv[9] = xv[10]; xv[10] = value / gain;
    yv[0] = yv[1]; yv[1] = yv[2]; yv[2] = yv[3]; yv[3] = yv[4]; yv[4] = yv[5]; yv[5] = yv[6]; yv[6] = yv[7]; yv[7] = yv[8]; yv[8] = yv[9]; yv[9] = yv[10];
    yv[10] =   (xv[10] - xv[0]) + 5 * (xv[2] - xv[8]) + 10 * (xv[6] - xv[4])
    + ( -0.0000000000 * yv[0]) + (  0.0357796363 * yv[1])
    + ( -0.1476158522 * yv[2]) + (  0.3992561394 * yv[3])
    + ( -1.1743136181 * yv[4]) + (  2.4692165842 * yv[5])
    + ( -3.3820859632 * yv[6]) + (  3.9628972812 * yv[7])
    + ( -4.3832594900 * yv[8]) + (  3.2101976096 * yv[9]);
    return yv[10];
}

}

let maxPeriod = 1.5
let minPeriod = 0.1
let invalidEntry = -11
let maxPeriodsToStore = 20
let averageSize = 20



class PulseDetector {

var upVals: Float?
var downVals: Float?
var upValIndex: Int?
var downValIndex: Int?
var lastVal: Float?
var periodStart: Float?
var periods: Double?
var periodTimes: Double?
var periodIndex: Int?
var started: Bool?
var freq: Float?
var average: Float?
var wasDown: Bool?


func reset() {


    for var i=0; i < maxPeriodsToStore; i++ {
        periods[i] = invalidEntry // obviously not Swift so how can I fit it?
    }
    for var i=0; i < averageSize; i++ {
        upVals[i] = invalidEntry    // same here
        downVals[i] = invalidEntry  // same here 
    }
    freq = 0.5
    periodIndex = 0
    downValIndex = 0
    upValIndex = 0
}

1 个答案:

答案 0 :(得分:0)

如果你问你将如何转换

self.filter=[[FilterClass alloc] init];

到Swift,下面的代码行将是答案

self.filter= FilterClass()