我已经开始学习Objective-C(之前没有编程经验),而且我正在研究Kochan的"Objective-C编程(第3版)".它应该是一个较新的版本(它出现在2011年6月,就在一个月前的写作时),但它根本没有引用ARC.文中的代码示例使用NSAutoReleasePool并发布,但我发现Xcode不希望我在ARC实现时使用这些代码.
作为一名新程序员,我是否应该养成了解我的发布池/手动内存管理的习惯,还是应该坚持使用ARC?
我很确定这是100%安全的,但我不想错过任何东西.我有以下代码
- (void) scheduleControlSurfaceProcess {
[self.operationQueueForMessageProcessing addOperationWithBlock:^{
// do something
[self scheduleControlSurfaceProcess];
}];
}
Run Code Online (Sandbox Code Playgroud)
自我是单身人士.该块作为非主线程线程非常出色.我没有在profiler中看到任何内存问题(我不太相信).
那么,我可以忽略这个警告,"Block会被捕获对象强烈保留的对象保留吗?" 如果没有,我怎么能坚持要释放块(用ARC)?让警告消失很容易,但分配id what = self似乎无法解决问题.
编辑:正如我在这个问题中意识到的那么晚,这里真正的问题是我从块内部重新安排.这显然是有问题的,因为每个块保留下一个.
nsoperationqueue ios objective-c-blocks automatic-ref-counting
我有一个看起来像UIViewController的奇怪(非?)问题.看起来控制器在取消分配时不会释放其子视图.我将NSLog消息放在所有子视图的dealloc方法以及视图控制器中.视图控制器dealloc被调用,但子视图没有.但是,如果我然后将该视图控制器的另一个实例推送到导航堆栈,则会显示之前实例的所有子视图都会被释放(我在控制台中收到一堆NSLog消息让我知道).我已经检查过,并且我没有单独引用显示视图控制器中的自定义视图控制器(正在进行推送的控制器).
一个小的(可能)细节:自定义视图控制器确实接收它存储的块,然后在弹出之前执行.但是,我确实发送了nil并且我得到了同样的行为.另外,当弹出堆栈时,呈现视图控制器会释放dealloc,因此没有保留周期.
此外,我确实尝试在自定义视图控制器的dealloc方法中显式释放每个视图.相同的行为.
导航控制器是否可能坚持下去?它似乎不会对我的任何其他视图控制器执行此操作.
我的问题是,这确实代表了内存泄漏(所有这些子视图); 虽然泄漏没有堆积,但它仍然是泄漏.
我正在尝试在使用ARC的项目中使用TPMultiLayoutViewController,但是碰到了以下错误: -
使用ARC不允许将Objective-C指针隐式转换为'const void*'
我真的不确定如何解决这个问题; 它需要明确转换吗?我该怎么做?
- (void)addAttributesForSubviewHierarchy:(UIView*)view associatedWithSubviewHierarchy:(UIView*)associatedView toTable:(NSMutableDictionary*)table {
[table setObject:[self attributesForView:view] forKey:[NSValue valueWithPointer:associatedView]];
if ( ![self shouldDescendIntoSubviewsOfView:view] ) return;
for ( UIView *subview in view.subviews ) {
UIView *associatedSubView = (view == associatedView ? subview : [self findAssociatedViewForView:subview amongViews:associatedView.subviews]);
if ( associatedSubView ) {
[self addAttributesForSubviewHierarchy:subview associatedWithSubviewHierarchy:associatedSubView toTable:table];
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个方法执行NSFetchRequest以获取托管对象数组(特别是XMPPUserCoreDataStorageObjects).在performUserFetch返回数组之前对象是正确的,我可以打印所有的displayNames,但是一旦我将数组返回到printUserInfo,对象就会进入故障状态,除了Core Data不会带来问题他们回来了!
- (NSArray*)performUserFetch
{
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:[xmppRosterStorage persistentStoreCoordinator]];
[context setUndoManager:nil];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject" inManagedObjectContext:context];
NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"sectionNum" ascending:YES];
NSSortDescriptor *sd2 = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, sd2, nil];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSError *err;
NSArray *result = [context executeFetchRequest:fetchRequest error:&err];
return result;
}
- (void)printUserInfo
{
NSArray *result = [self performUserFetch];
for(XMPPUserCoreDataStorageObject *user in result)
{
NSString …Run Code Online (Sandbox Code Playgroud) cocoa core-data nsfetchrequest xmppframework automatic-ref-counting
我在使用ARC时将CFDataRef转换为NSData时遇到问题.我正在使用__bridge_transfer或__bridge强制转换,但它无效.任何人都可以建议我一些其他方式来铸造这两种类型.我收到以下错误
"自动引用计数问题:不兼容的类型将'CFDataRef*'(又名'const struct __CFData**')转换为带有__bridge强制转换的'NSData*'
我正在尝试使用CAlayers进行逐帧动画.我正在使用本教程http://mysterycoconut.com/blog/2011/01/cag1/进行此操作,但一切都适用于禁用ARC,当我尝试用ARC重写代码时,它可以在模拟器上完美地工作但在设备上我的访问内存不好.
Layer Class接口
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
@interface MCSpriteLayer : CALayer {
unsigned int sampleIndex;
}
// SampleIndex needs to be > 0
@property (readwrite, nonatomic) unsigned int sampleIndex;
// For use with sample rects set by the delegate
+ (id)layerWithImage:(CGImageRef)img;
- (id)initWithImage:(CGImageRef)img;
// If all samples are the same size
+ (id)layerWithImage:(CGImageRef)img sampleSize:(CGSize)size;
- (id)initWithImage:(CGImageRef)img sampleSize:(CGSize)size;
// Use this method instead of sprite.sampleIndex to obtain the index currently displayed on screen
- (unsigned int)currentSampleIndex;
@end
Run Code Online (Sandbox Code Playgroud)
图层类实现
@implementation …Run Code Online (Sandbox Code Playgroud) xcode core-animation exc-bad-access objective-c automatic-ref-counting
我试图通过Xcode中的Edit-> Refactor将我的非ARC项目与发布等转换为Objective-C ARC,但在转换期间我在SBJson解析器源文件中出现错误:"此源文件必须在启用ARC的情况下编译!".但是我使用SBJson 3.1,这是ARC版本.
所以我不明白出了什么问题.有人可以帮忙吗?
PS我试着在答案中遵循建议,但结果是一样的:


如你所见,添加这些标志没有帮助.
我有个街区;
typedef void (^SIResponseHandler) (id obj, NSString *error);
Run Code Online (Sandbox Code Playgroud)
和方法:
+ (void)uploadPhoto:(UIImage *)photo
toPathForComponents:(NSArray *)components
completionHandler:(SIResponseHandler)responseHandler;
Run Code Online (Sandbox Code Playgroud)
和另一种调用上述方法的方法:
+ (void)updateProfilePhoto:(UIImage *)photo handler:(SIResponseHandler *)handler {
NSArray *components = @[@"users", sharedInstance.username, @"profile", @"photo", @"upload"];
[SIRequest uploadPhoto:photo
toPathForComponents:components
progressHandler:nil
completionHandler:handler];
}
Run Code Online (Sandbox Code Playgroud)
在最后一行,我收到此错误:
Sending '__autoreleasing SIResponseHandler *' (aka 'void (^__autoreleasing *)(__strong id, NSString *__strong)') to parameter of incompatible type 'SIResponseHandler' (aka 'void (^)(__strong id, NSString *__strong)')
Run Code Online (Sandbox Code Playgroud)
我不知道这是什么意思.有人可以解释一下发生了什么吗?谢谢
在XCode 4.6.2 ARC中,如果你在名为'retain'的类中有一些属性,那么IDE或编译堆栈都会做一些有趣的事情,使得类不能以通常的模式使用:[[MyClass alloc] init].
例如,如果我将类Foo定义为
// Foo.h
#import <Foundation/Foundation.h>
@interface MosquittoMessage : NSObject
@property (nonatomic, assign) BOOL retain;
-(id)init;
@end
// Foo.m
#import "Foo.h"
@implementation MosquittoMessage
-(id) init
{
self = [super init];
return self;
}
@end
Run Code Online (Sandbox Code Playgroud)
它会编译运行,但我不能使用
Foo *foo = [[Foo alloc] init];
Run Code Online (Sandbox Code Playgroud)
创造foo.运行时的上述语句将foo设置为nil.我使用调试器跟踪问题并发现alloc实际上返回了一个有效的Foo,但是在init中,由于某种原因它被释放,并且返回的self是nil.
任何人都对XCode或编译系统中的这个"bug"有任何想法吗?
添加:
当然'保留'是在Obj-C中保留的,我的问题是为什么编译器不抱怨它是否被允许?相反,它生成了错误的代码.
objective-c ×7
ios ×6
xcode ×3
iphone ×2
clang ×1
cocoa ×1
cocoa-touch ×1
core-data ×1
sbjson ×1