我不确定将项目转换为自动参考计数(ARC).它仍支持iOS 4.X或更低版本?
谢谢
我有为iOS 4编写的数千行代码.代码库包含许多调用retain和release,当项目更新到iOS 5和ARC时会导致错误.
有没有办法自动将手动保留释放(MRR)代码转换为自动参考计数(ARC)?
我在xcode和iOS工作,我有一个没有删除的对象,因为其他一些对象仍然有引用,但我无法弄清楚谁.使用Instruments,我发现它的引用计数为1,但据我查看代码,它应该为零.
我已经运行了仪器,并没有将其视为泄漏.
如果有一种方法在仪器(或调试器)中找出谁还有参考?
__weak在方法的实现签名中使用存储修饰符是否有效?特别是如果它不是方法的公共签名的一部分?例如:
- (UIView *)tableView:(__weak UITableView *)tableView viewForHeaderInSection:(NSInteger)sectionIndex
{
UIView *view = [ABHeaderView view];
view.actionBlock = ^{
[tableView doSomething];
}
// ...
return view;
}
Run Code Online (Sandbox Code Playgroud)
这是否正确tableView用作弱指针?或者我应该真的做一些类似的事情__weak *weakTableView = tableView;并weakTableView在街区内使用?
我没有收到任何警告或错误,并且clang Static Analyzer不会发出任何警告.
memory-management objective-c objective-c-blocks automatic-ref-counting
我有一个Xcode 3.2目标项目.我想将此项目转换为ARC以改进
我遵循这些步骤.
编辑 - >重构 - >转换为目标C ARC
我有一些.mm文件,我使用-fno-objc-arc禁用ARC.但即便如此
我遇到了很多错误(ARC问题).例如,主要在self = [super init]调用中,
错误是无法在init系列中的方法之外分配给self.能够
有人告诉我,我是按照正确的步骤吗?
根据过渡到ARC发行说明:
__autoreleasing用于表示通过引用(id*)传递的参数,并在返回时自动释放.
例如:
-(BOOL)performOperationWithError:(NSError * __autoreleasing *)error;
但与上述相比,上述优点有哪些:
-(BOOL)performOperationWithError:(NSError * __strong *)error;
更新:
几个答案是指临时VAR招编译器处理为var和参数之间的不匹配的优势的__autoreleasing.我不明白为什么编译器不能为__strong参数做同样的技巧.我的意思是,对于__weakvar和__strong参数,编译器可以类似地执行此操作:
NSError * __weak error;
NSError * __strong tmp = error;
BOOL OK = [myObject performOperationWithError:&tmp];
error = tmp;
if (!OK) {
// Report the error.
}
Run Code Online (Sandbox Code Playgroud)
编译器知道-(BOOL)performOperationWithError:(NSError * __strong *)error;返回一个强引用(+1),所以它像任何new-family方法一样处理它.由于tmp生活在相同的范围内error,编译器可以合理地保持它活着,只要error这样__weak引用(error)现在由__strongreference(tmp)支持,并且在范围结束之前不会无效.
我很惊讶地发现以下行为......
@interface Foo : NSObject
- (void)addBar:(id)aBar withCompletion:(void(^)(void))completion;
@end
@interface AwesomeClass : NSObject
@property (strong, nonatomic) Foo *foo;
- (void)doSomethingWithBar:(id)bar;
@end
@implementation AwesomeClass
- (void)doSomethingWithBar:(id)bar
{
[self.foo addBar:bar withCompletion:^{
NSLog(@"%@", self.foo);
}];
}
Run Code Online (Sandbox Code Playgroud)
在Xcode 4.6.1中,我在实现中得到警告-doSomethingWithBar:,"在这个块中强烈捕获'自我'可能会导致保留周期."
但是,如果我重构名称的方法-addBar:withCompletion:来-setupBar:withCompletion:此警告消失.看来我的惊喜表明我对Objective-C命名约定的知识存在差距!
当用户单击按钮时,它会显示一个带有两个视图控制器的新标签栏视图控制器.我就是这样做的
ACLevelDownloadController *dvc = [[ACLevelDownloadController alloc] initWithNibName:@"ACLevelDownloadController" bundle:[NSBundle mainBundle]];
ACInstalledLevelsController *ivc = [[ACInstalledLevelsController alloc] initWithNibName:@"ACInstalledLevelsController" bundle:[NSBundle mainBundle]];
UITabBarController *control = [[UITabBarController alloc] init];
control.viewControllers = @[dvc, ivc];
dvc.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:0];
ivc.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:1];
[self presentViewController:control animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
这很好用.我关闭那些视图控制器具有dismiss在两个方法ACLevelDownloadController和ACInstalledLevelsController.这也很好.奇怪的是,当我呈现视图控制器时,内存使用率会上升

但它永远不会失败.如果我再次呈现它,它会更上升
我正在使用ARC.为什么视图控制器使用的内存在被解除后不被释放?
他们被解雇的方式是两者ACLevelDownloadController并且ACInstalledLevelsController已经连接了IBActions,当他们被点击时调用这个方法
- (void)dismiss:(id)sender{
[self dismissViewControllerAnimated:YES completion:nil];
}
Run Code Online (Sandbox Code Playgroud) 考虑一下:
id observer = [[NSNotificationCenter defaultCenter]
addObserverForName:MyNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
[[NSNotificationCenter defaultCenter]
removeObserver:observer
name:MyNotification
object:nil
];
// do other stuff here...
}
];
Run Code Online (Sandbox Code Playgroud)
我正在使用此模式观察一次通知,然后停止观察它.但是LLVM告诉我(在ARC下)当被块捕获时,变量'观察者'是未初始化的.
我怎么能解决这个问题,因为块必须在初始化之前捕获变量,它是初始化器的一部分?是否会使用__block限定符observer来做诀窍?
objective-c compiler-warnings objective-c-blocks automatic-ref-counting