我正在努力通过contextInfo
of typeUnsafeMutablePointer<Void>
to UISaveVideoAtPathToSavedPhotosAlbum
并在回调函数中使用它。由于某种原因我无法访问contextInfo
作为字符串使用UnsafePointer<String>(x).memory
当我在回调函数中时。
我很确定这是我所缺少的一些简单的东西,但我花了很多时间试图弄清楚这一点。
下面是我尝试过的一些代码。
以下代码有效。
var testStr:String = "hello"
takesAMutableVoidPointer(&testStr)
func takesAMutableVoidPointer(x: UnsafeMutablePointer<Void>){
var pStr:String = UnsafePointer<String>(x).memory
println("x = \(x)")
println("pStr = \(pStr)")
}
但是下面的代码不起作用。
var testStr:String = "hello"
if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(filePath){ //the filePath is compatible
println("Compatible")
//UISaveVideoAtPathToSavedPhotosAlbum(filePath, self, nil, nil)
UISaveVideoAtPathToSavedPhotosAlbum(filePath, self, "video:didFinishSavingWithError:contextInfo:", &testStr)
}
else{
println("Not Compatible")
}
func video(video: NSString, didFinishSavingWithError error:NSError, contextInfo:UnsafeMutablePointer<Void>){
var pStr:String = UnsafePointer<String>(contextInfo).memory
println("contextInfo = \(contextInfo)")
println("pStr = \(pStr)")
}
一旦我到达以下行:
var pStr:String = UnsafePointer<String>(contextInfo).memory
我不断收到以下错误:
Thread 1: EXC_BAD_ACCESS(code=1, address=0x0)
任何对此的帮助将不胜感激。
Thanks.
Update
Rintaro 评论说 testStr 需要是顶级的,但以下代码可以工作。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var testStr:String = "hello"
takesAMutableVoidPointer(&testStr)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func takesAMutableVoidPointer(x: UnsafeMutablePointer<Void>){
var answer = UnsafePointer<String>(x).memory
println("x = \(x)")
println("answer = \(answer)")
}
}
除非必须,我尽量不使用全局变量。我可能必须这样做,但由于我能够执行上面的代码,似乎我不需要使用全局变量。