标签: automatic-ref-counting

使用ARC在块中设置NSError

我希望使用自动引用计数从项目的块中设置NSError指针.以下是我的代码的简化版本:

- (BOOL)frobnicateReturningError:(NSError **)error
{
    NSArray *items = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];

    __block Frobnicator *blockSelf = self;
    [items enumerateObjectsUsingBlock:^(id item, NSUInteger idx, BOOL *stop) {
        [blockSelf doSomethingWithItem:item error:error];
    }];
}
Run Code Online (Sandbox Code Playgroud)

这个编译但是给定error可以修改 doSomethingWithItem我尝试为要修改的块创建一个本地NSError,然后用于error在枚举之后设置原始(我没有显示):

- (BOOL)frobnicateReturningError:(NSError **)error
{
    NSArray *items = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];

    __block Frobnicator *blockSelf = self;
    __block NSError *blockError = nil;
    [items enumerateObjectsUsingBlock:^(id item, NSUInteger idx, BOOL *stop) {
        [blockSelf doSomethingWithItem:item error:&blockError];
    }];
}
Run Code Online (Sandbox Code Playgroud)

无法编译时出现以下错误:

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

谷歌搜索此错误只返回Clang源代码本身的结果.

一个看起来有效但有点难看的解决方案是有一个内部和外部错误指针:

- (BOOL)frobnicateReturningError:(NSError **)error …
Run Code Online (Sandbox Code Playgroud)

objective-c nserror objective-c-blocks automatic-ref-counting

10
推荐指数
1
解决办法
6077
查看次数

iOS:如何在启用ARC的情况下从内存中删除对象?

我正在使用iOS 5 SDK开发iOS应用程序,启用了自动引用计数.但我有一个特定的对象正在大量创建,必须在一秒钟之后释放,否则设备将变得非常慢.看起来它们没有被释放,因为设备非常慢.有没有办法在启用ARC时手动释放对象?

编辑:我的代码,这被称为每秒200次生成火花.它们在0.8秒后淡出,因此在那之后它们没用了.

    int xanimationdiff = arc4random() % 30;
    int yanimationdiff = arc4random() % 30;
    if (arc4random()%2 == 0) {
        xanimationdiff = xanimationdiff * -1;
    }
    if (arc4random()%2 == 0) {
        yanimationdiff = yanimationdiff * -1;
    }

    Sparkle *newSparkle = [[Sparkle alloc] initWithFrame:CGRectMake(20 + arc4random() % 280, 20, 10, 10)];
    //[newSparkle setTransform:CGAffineTransformMakeRotation(arc4random() * (M_PI * 360 / 180))]; //Rotatie instellen (was niet mooi, net sneeuw)
    [self.view addSubview:newSparkle];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.8];
    [newSparkle setFrame:CGRectMake(newSparkle.frame.origin.x - xanimationdiff, newSparkle.frame.origin.y - yanimationdiff, …
Run Code Online (Sandbox Code Playgroud)

ios automatic-ref-counting

10
推荐指数
3
解决办法
3万
查看次数

ARC错误:脆弱的abi不支持-fobjc-arc

可能重复:
Objective-C ARC错误:脆弱的abi不支持-fobjc-arc

Clang LLVM 1.0错误错误:脆弱的abi不支持-fobjc-arc

我正在构建一个OSX应用程序,当我用"Profile"编译它时,我得到了同样的错误,与Instruments合作.知道怎么解决吗?

objective-c xcode4.2 automatic-ref-counting

10
推荐指数
1
解决办法
4588
查看次数

EXC_BAD_ACCESS消息发送到解除分配的实例,但我正在使用ARC?

我有一个应用程序从SOAP Web服务获取信息,我想在一个显示结果UITableView.

我有一个以前版本的这个应用程序,我正在创建一个新版本,基本上清理东西,摆脱一堆已弃用,不再使用的遗留代码.

在之前的版本中,这很有效.在新版本中,没有那么多.

基本上,当前场景返回3个字符串,我试图用作我的数据的基础UITableView.

我正在努力解决这个问题,因为它太难以追查EXC_BAD_ACCESS错误了!

(顺便说一句,如果有人有办法使调试体验更像Visual Studio,我很乐意听到它!没有任何想法导致错误,并且无法通过我查看,这是非常令人沮丧的崩溃时的局部变量,看看是什么.我已经在异常断点中添加了,但这似乎并不多.)

无论如何,导致出错的行是:

return [[self Libraries] count];
Run Code Online (Sandbox Code Playgroud)

它发生在tableView:numberOfRowsInSection:.

错误消息我得到APPEARS引用应该存储在中的字符串NSMutableArray [self Libraries].

这里发生了什么?
我正在使用ARC,所以不应该正确处理所有内存管理吗?
ANYWHERE我的代码中没有任何手动发布声明!

请帮我解决这个问题!

memory exc-bad-access objective-c automatic-ref-counting

10
推荐指数
1
解决办法
5741
查看次数

ARC和UIAlertView:发送到实例的无法识别的选择器

这是我如何显示UIAlertView和委托clickedButtonAtIndex -

UIAlertView *alert =
    [[UIAlertView alloc] initWithTitle: @"title"
                               message: @"message"
                              delegate: self
                     cancelButtonTitle: @"Cancel"
                     otherButtonTitles: @"Continue", nil];

    [alert show];


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    //something
}
Run Code Online (Sandbox Code Playgroud)

这段代码在没有ARC的情况下完美运行.但是使用ARC它会抛出此错误 - 由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSCFType alertView:clickedButtonAtIndex:]:无法识别的选择器发送到实例0x859d790'

关于代表为何抛出此错误的任何想法?

uialertview ios automatic-ref-counting

10
推荐指数
1
解决办法
4522
查看次数

-fno-objc-arc无法禁用ARC

我已经尝试过这篇文章中的解决方案来禁用AFNetworking文件中的ARC,但无济于事:

在此输入图像描述

我失败的任何想法?显然答案越简单越好.我还读到创建静态库可能有所帮助,但这看起来很复杂.

EDITS

我已经尝试删除派生数据和清理和构建并重新启动Xcode.没有交易:(.

此外,我的工作区中没有其他项目.

xcode memory-management objective-c automatic-ref-counting afnetworking

10
推荐指数
2
解决办法
8648
查看次数

桥接演员:__ bridge_transfer vs __bridge with synthesis setter

我正在使用ARC,并且在使用时会产生混淆__bridge_transfer.我有一个属性userName如下:

@property (nonatomic, retain) NSString *userName;
...
@synthesize userName = _userName;
...
Run Code Online (Sandbox Code Playgroud)

案例1:

NSString *name = (__bridge_transfer NSString *)ABRecordCopyCompositeName(person);
self.userName = name;
Run Code Online (Sandbox Code Playgroud)

案例2:

self.userName = (__bridge_transfer NSString *)ABRecordCopyCompositeName(person);
Run Code Online (Sandbox Code Playgroud)

哪里person是类型ABRecordRef.

在案例1中,ARC将发布本地变量名称(根据我的理解,如果我错了,请纠正我),但是在案例2中会发生什么?我应该__bridge在案例2中使用还是不应该使用案例2?在案例2中使用__bridge_transfer或__bridge,如何平衡引用计数?

在CASE 2中,__bridge_transfer将ARC释放对象(该对象,它作为参数传递给setter (void)setUserName:(NSString *)userName)?

objective-c automatic-ref-counting

10
推荐指数
1
解决办法
9995
查看次数

Xcode 4.6用于游戏中心身份验证的ARC警告

这是一个新的编译器警告,只有在我将XCode更新为4.6时才出现.我的代码直接取自Apple的文档(这是我的iOS 6代码顺便说一句).

GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {
    [self setLastError:error];
    if(localPlayer.authenticated){
Run Code Online (Sandbox Code Playgroud)

警告 - 在此块中强烈捕获"localPlayer"可能会导致保留周期

xcode ios game-center automatic-ref-counting xcode4.6

10
推荐指数
1
解决办法
1645
查看次数

弱IBOutleCollection总是零

我的问题是为什么弱IBOutletCollection总是为零?如果改变弱到强,我的所有按钮都在那里,这真的很奇怪.我试图理解苹果的逻辑,我可以看到单个按钮和一系列按钮在内存管理方面没有区别.我错过了什么吗?

memory-management interface-builder storyboard ios automatic-ref-counting

10
推荐指数
2
解决办法
1270
查看次数

为什么ARC将此块复制到堆中?

考虑这个简单的例子

int i = 42;
int (^aBlock)() = ^ {
    return i;
};
NSLog(@"Class: %@", [aBlock class]);
Run Code Online (Sandbox Code Playgroud)

没有ARC,上面的代码打印出来

Class: __NSStackBlock__
Run Code Online (Sandbox Code Playgroud)

而与ARC一起打印

Class: __NSMallocBlock__
Run Code Online (Sandbox Code Playgroud)

我放置了一个符号断点,_Block_copy看起来ARC正在插入一个Block_Copy()调用,导致该块被移动到堆中

这似乎是一种不必要的开销,它首先打破了堆栈上的块的整个目的.

这是ARC的限制还是设计选择?

objective-c objective-c-blocks automatic-ref-counting

10
推荐指数
1
解决办法
1297
查看次数