Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift:非托管AVAudioPlayer_Ios_Swift_Automatic Ref Counting - Fatal编程技术网

Ios Swift:非托管AVAudioPlayer

Ios Swift:非托管AVAudioPlayer,ios,swift,automatic-ref-counting,Ios,Swift,Automatic Ref Counting,我有AVAudioPlayer实例: var audioPlayer: AVAudioPlayer! self!.audioPlayer = AVAudioPlayer(data: fileData, error: &error) self!.audioPlayer?.numberOfLoops = -1 self!.audioPlayer?.delegate = self if (self?.audioPlayer?.prepareToPlay() != false) { p

我有
AVAudioPlayer
实例:

var audioPlayer: AVAudioPlayer!
self!.audioPlayer = AVAudioPlayer(data: fileData, error: &error)
self!.audioPlayer?.numberOfLoops = -1
self!.audioPlayer?.delegate = self

if (self?.audioPlayer?.prepareToPlay() != false) {
    println("Successfully prepared for playing")
} else {
    println("Failed to prepare for playing")
}
我需要为此
AVAudioPlayer
禁用ARC<代码>非托管没有很好的文档记录,因此很难做到这一点。以下是我尝试过的:

var audioPlayer: Unmanaged<AVAudioPlayer>!
//Stuck after creating nil instance, what to do now?
self!.audioPlayer = AVAudioPlayer(data: fileData, error: &error)
self!.audioPlayer?.numberOfLoops = -1
self!.audioPlayer?.delegate = self

if (self?.audioPlayer?.prepareToPlay() != false) {
    println("Successfully prepared for playing")
} else {
    println("Failed to prepare for playing")
}

var audioPlayer:非托管!
//创建nil实例后卡住了,现在怎么办?
自己audioPlayer=AVAudioPlayer(数据:fileData,错误:&error)
自己音频播放器?.numberOfLoops=-1
自己audioPlayer?.delegate=self
if(self?.audioPlayer?.prepareToPlay()!=false){
println(“已成功准备播放”)
}否则{
println(“未能准备播放”)
}
你应该写:

// Properties in your class
var unmanagedAudioPlayer: Unmanaged<AVAudioPlayer>
var audioPlayer : AVAudioPlayer!

// code
self.audioPlayer = AVAudioPlayer(...)
self.unmanagedAudioPlayer = Unmanaged.passRetained(self.audioPlayer)
//类中的属性
var Unmanaged玩家:非托管
var audioPlayer:AVAudioPlayer!
//代码
self.audioPlayer=AVAudioPlayer(…)
self.unmanagedAudioPlayer=Unmanaged.passRetained(self.audioPlayer)
现在你可以像往常一样使用
self.audioPlayer
作为
AVAudioPlayer
(或者
AVAudioPlayer!
,如果你愿意的话,但我不明白为什么)

self.unmanagedAudioPlayer
保留了对
self.audioPlayer
的保留引用,因此ARC无法将其解除分配

完成此
AVAudioPlayer
对象后,可以调用
self.unmanagedAudioPlayer.release()
(或
autorelease
)将其释放,就像在Objective-C中一样


事实上,您不需要单独存储self.audioPlayer,因为您总是可以通过
unmanagedAudioPlayer.takenDatainedValue()
获取它,但它是一个很好的别名,可以让您的代码更具可读性。

这是一个不存在的问题。“关闭电弧”的建议是错误的,你不应该尝试这样做。如果你有内存问题,你应该直接解决它。例如,如果背景中的AVAudioPlayer出现问题,请在进入背景时将其设置为
nil
,以将其从内存中释放。但更重要的是,你应该问问自己为什么你会有这个问题。我已经使用AVAudioPlayer很多年了,我从来没有遇到过“它从内存中泄漏”的情况(不管你是什么意思)。

“创建零实例”-
var audioPlayer:Unmanaged-空AVAUDIOPLAYRIT很想知道为什么要“禁用ARC”。这可能是一个?
var audioPlayer:非托管声明一个变量,它不会创建任何东西。这里是原始问题:这就是为什么我需要禁用ARC