【问题标题】:Variable issue during transition from iOS 4 to iOS 5+ARC: "Passing address of non-local object to _autoreleasing parameter for write-back"从 iOS 4 过渡到 iOS 5+ARC 期间的变量问题:“将非本地对象的地址传递给 _autoreleasing 参数以进行回写”
【发布时间】:2011-11-08 19:39:06
【问题描述】:

我在 iOS 4 中执行此操作的“旧”方式是在头文件中声明对象并将对象传递给回写以处理错误参数。

NSError *error;

由于我知识有限的原因,我无法在 iOS5 中继续这种模式并收到错误:

“将非本地对象的地址传递给_autoreleasing参数以进行回写”

//Instantiate an instance of AVAudioSession object
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
//Setup playback and Record
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error ];

我的临时解决方案是这样做:

NSError *theError = nil;

//Instanciste an instance of AVAudioSession object
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
//Setup playback and Record
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&theError];
//Activate session
[audioSession setActive:YES error:&theError];

这很烦人,因为我每次需要在 Xcode 中使用它时都必须创建这个本地对象。

我的问题是:在新的 ARC 范式中是否有更好的方法来做到这一点?

我在堆栈溢出中发现了一个类似的问题,它处理了这个问题...... here

【问题讨论】:

    标签: objective-c ios automatic-ref-counting


    【解决方案1】:

    如果您确实想使用 ivar NSError *error,处理这种情况的首选方法是:

    NSError *theError = nil;
        //...
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&theError];
    if (theError != nil){
        error = theError;
        //Handle failure
    }
    [audioSession setActive:YES error:&theError];
    if (theError != nil){
        error = theError;
        //Handle failure
    }
    

    我的猜测是你的意思是完全忽略错误,如果我错了,请原谅我。只需这样做(虽然不推荐)就可以做到这一点:

    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    [audioSession setActive:YES error:nil];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-16
      • 1970-01-01
      • 2016-04-28
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多