这是我的情景.我有一个类A.在它的实现中我创建了类型B的对象并将B的委托设置为self(所以B.delegate = self在A类的实现中的某个地方).
A类有一个实例方法 - (void)printThis;
现在在B的实现中,当我尝试做的时候[delegate printThis];,它给了我这个错误:"没有已知的选择器print的实例方法这个"
当然,这是我启用ARC的时候.上面的委托模式曾经在没有ARC的iOS 4.x中正常工作.当我关掉ARC时它仍然会这样做.ARC与将消息传递给代表有什么关系?
骨架代码:
啊
@class B;
@interface A: blah blah
{
B objB;
}
-(void) printThis;
Run Code Online (Sandbox Code Playgroud)
上午
objB = [[B alloc] init];
objB.delegate = self;
- (void)printThis {
//doSomething
}
Run Code Online (Sandbox Code Playgroud)
BH
@interface B: blah blah
{
//id delegate; //used to be there, now I just property & synthesize
}
@property (nonatomic,weak) id delegate;
Run Code Online (Sandbox Code Playgroud)
BM
@synthesize delegate;
[delegate printThis]; //error with ARC ON, works with OFF
Run Code Online (Sandbox Code Playgroud)
重要编辑:
请注意,这种情况会发生在这里和那里.例如,我在A中有一些其他方法,如printThat等,它们可以正常工作.我对发生的事情毫无头绪!
我有一个使用自动引用计数的类.在课堂上,我有一个如下所述的财产.当我为班级设置提醒时,我希望发生更改按钮标题的副作用.
这是我的代码:
在.h文件中:
@property(nonatomic,strong)Reminder* reminder;
Run Code Online (Sandbox Code Playgroud)
在.m文件中:@synthesize提醒;
-(void)setReminder:(Reminder *)reminder_
{
//what else do I need to do here?
reminder = reminder_;
if(!reminder.useSound.boolValue)
{
onOffButton.title = NSLocalizedString(@"Off", @"Off title");
}else
{
onOffButton.title = NSLocalizedString(@"On", @"On title");
}
}
Run Code Online (Sandbox Code Playgroud)
我知道没有ARC,我会做这样的事情:
-(void)setReminder:(Reminder *)reminder_
{
[reminder release];
reminder = [reminder_ retain];
if(!reminder.useSound.boolValue)
{
onOffButton.title = NSLocalizedString(@"Off", @"Off title");
}else
{
onOffButton.title = NSLocalizedString(@"On", @"On title");
}
}
Run Code Online (Sandbox Code Playgroud)
我是否需要在启用ARC的setter方法中执行任何其他操作以确保正确保留强变量?
memory-management objective-c ios ios5 automatic-ref-counting
我在我的故事板中有一个视图,我分配了一个名为"MainView"的标识符.但是,如果我将其视图添加到子视图,则后面的所有内容都会导致崩溃(例如,按下按钮)
MainViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainView"];
[self.view addSubview:mvc.view];
Run Code Online (Sandbox Code Playgroud)
这是按钮触发的动作:(MainViewController.h)
-(IBAction)showUsername:(id)sender{
[testLabel setText:@"username"];
}
Run Code Online (Sandbox Code Playgroud)
和崩溃日志:
-[MainViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x44e0810
Run Code Online (Sandbox Code Playgroud)
我用ARC.
iphone objective-c storyboard uiviewcontroller automatic-ref-counting
这是你应该如何在ARC中使用CFMutableDictionaryRef?
CFMutableDictionaryRef myDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
NSString *key = @"someKey";
NSNumber *value = [NSNumber numberWithInt: 1];
//ARC doesn't handle retains with CF objects so I have to specify CFBridgingRetain when setting the value
CFDictionarySetValue(myDict, (__bridge void *)key, CFBridgingRetain(value));
id dictValue = (id)CFDictionaryGetValue(myDict, (__bridge void *)key);
//the value is removed but not released in any way so I have to call CFBridgingRelease next
CFDictionaryRemoveValue(myDict, (__bridge void *)key);
CFBridgingRelease(dictValue);//no leak
Run Code Online (Sandbox Code Playgroud) 还有可能吗?我的意思是弱指针会自动为空.强指针只有在指向其他地方时才会解除分配.
我们仍然可以指向解除分配的对象吗?
__unsafe_unretained由知道他们正在做什么的人完成.所以这不是真的错误.
@interface Unicorn
@property (nonatomic, copy) NSString *name;
@end
Run Code Online (Sandbox Code Playgroud)
是这样的吗?
- (void)setName:(NSString *)name
{
_name = name;
}
Run Code Online (Sandbox Code Playgroud)
或者是这样的?
- (void)setName:(NSString *)name
{
_name = [name copy];
}
Run Code Online (Sandbox Code Playgroud) 在ARC我知道你没有调用[super dealloc]任何覆盖-dealloc,所以通常我会删除那里的观察者和计时器而不这样做.但是,如果我要将我发布的视图子类化,-dealloc而不调用[super dealloc]子类的方法实现,则会自动调用超级实现以释放超类处理的观察信息,还是会泄漏?
我正在阅读一些文档,解释如何在NSOperationQueue中管理NSOperation.我的重点是,如果用户在进度面板中按下取消按钮或退出应用程序,则根本不执行操作.因此,取消操作以防止它不必要地消耗CPU时间.
因此,每当我需要取消操作时,我应该触发取消方法以防止进一步执行.然后我将不得不定期使用操作对象状态isCancelled来检查操作是否被取消.以下是我对此的疑问:
(1)在取消请求时,如果从NSOperationQueue中移除NSOperation,那么我们如何仍然引用该NSOperation及其属性被取消?
根据Apple Developer Class Reference:
NSOperationQueue类调节一组NSOperation对象的执行.添加到队列后,操作将保留在该队列中,直到明确取消或完成其任务为止.
(2)如果我使用ARC,我是否需要关心取消请求?我举个例子.我有2个视图控制器A和B.在B中,我使用NSOperation制作8到10个NSURLRequest并将所有请求放在NSOperationQueue中.这里NSOperationQueue的对象是视图控制器B的属性.因此,如果用户按下后退按钮返回到视图A,在ARC下,NSOperationQueue的对象应该被自动删除(当我弹出以查看A时).ARC机制是否会取消所有操作,还是应该有一些机制来避免无用的执行?
objective-c nsoperation nsoperationqueue ios automatic-ref-counting
如果我有一个非免费桥接的 Core Foundation对象,我可以安全地将所有权转移到ARC,还是为免费桥接类型保留的权限?
例如:
- (id)myBundle {
CFBundleRef b = CFBundleCreate(NULL, self.bundleURL);
return b == NULL ? nil : (__bridge_transfer id)b;
}
- (UInt32)myBundleVersionNumber {
return CFBundleGetVersionNumber((__bridge CFBundleRef)self.myBundle);
}
Run Code Online (Sandbox Code Playgroud) 从控件本身的事件处理程序内部,我想删除并释放它.
一个典型的用例TFmxObject.Release,不是吗?但是,它似乎只能在Windows下运行,但不能在Android上运行,而且此方法现在已被弃用.
我知道,不起作用不是一个很好的问题描述,但目前我无法在android下调试它.在Windows下,我看到事件处理程序.Release在完成之后继续正确,在我的控件析构函数中执行我的日志消息.在Android下,应用程序挂起.
当我使用.Free它时,它仍然在Windows下工作(析构函数立即发生,但处理程序在免费后不访问控件),而在Android中没有明显的问题,但析构函数从未被调用,所以我有一个泄漏.
随着.DisposeOf效果是一样的有.Release-视窗OK,Android的挂起.
我也尝试过,MyParent.RemoveComponent(MyControl)但一切都没有用.
我还需要做些什么来发布所有引用,以便ARC可以完成它的工作?或者怎么回事?
objective-c ×9
ios ×4
iphone ×2
android ×1
delphi ×1
firemonkey ×1
ios5 ×1
nsoperation ×1
storyboard ×1