如何初始化以下代码?
import Foundation
class RecordedAudio: NSObject{
var filePathUrl: NSURL!
var title: String!
}
答案 0 :(得分:0)
像这样:
var myVariable = RecordedAudio()
但是,鉴于filePathUrl
和title
是隐式解包的选项(我不得不建议反对),您可能想要创建一个更好的初始化方法:
init(filePathUrl: NSURL, title: String) {
self.filePathUrl = filePathUrl
self.title = title
}
然后使用该初始化。
var myVariable = RecordedAudio(filePathUrl: someUrlVar, title: someStringVar)
答案 1 :(得分:0)
在Recorded Audio Class中,添加初始化代码:
class RecordedAudio: NSObject{
var filePathUrl: NSURL!
var title: String!
init(filePathUrl: NSURL, title: String) {
self.filePathUrl = filePathUrl
self.title = title
}
}
然后,在你的audioRecorderDidFinishRecording方法中,在你正在进行录制的ViewController中,用这样的实时代码替换注释掉的代码:
func audioRecorderDidFinishRecording(recorder: AVAudioRecorder!, successfully flag: Bool) {
if(flag){
// Save the recorded audio
// prior to adding initialization:
// recordedAudio = RecordedAudio()
// recordedAudio.filePathUrl = recorder.url
// recordedAudio.title = recorder.url.lastPathComponent
// Using initialization
recordedAudio = RecordedAudio(filePathUrl: recorder.url, title: recorder.url.lastPathComponent!)
// move to next UI
self.performSegueWithIdentifier("stopRecording", sender: recordedAudio)
} else {
// If recording not successful
recordingUIEnabled()
}
}