标签: automatic-ref-counting

使用ARC的Objective-C:自定义setter不会保留

注意:这是由于一些XCode beta版本中的错误已经修复.如果您遇到ARC问题,此问题和答案可能无法帮助您.


我正在将我的项目从手动引用计数迁移到ARC,并且偶然发现了一个问题:如何确保保留属性的自定义setter实际保留?

在myClass.h,我宣布了一个财产:@property (retain) NSDate *date.无论我是手动设置__strongivar还是自动生成它都无关紧要.

在实现中,我当然要@synthesize date实现一个自定义setter(或者只是下载demo Xcode项目):

- (void)setDate:(NSDate *)newDate
{
  if (allowedToSetNewDate)
  {
    date = newDate;
  }
}
Run Code Online (Sandbox Code Playgroud)

这似乎没有保留日期,并且message sent to deallocated instance当newName(自动)发布它来自哪里时,尝试myClass.date稍后访问时提供给我(提供Zombie已启用;否则,它只是静默崩溃).

更改使用的setter date = [newDate copy]可以解决错误,但实际上并不是我想要的.删除自定义setter也有效,但显然不是必需的.

我在这里错过了什么?如何确保保留属性的自定义setter实际保留在ARC环境中?这似乎是一项基本而且常见的任务,我认为我忽略了一些非常明显的事情.

(注意:这不属于任何Apple NDA的条款,因为ARC作为LLVM的一部分公开发布)

编辑:我已经创建了一个小型Xcode项目来演示该问题并将其上传到github.随意下载并玩游戏.我的机智已经结束了(尽管我的机智今天并不是最好的,不可否认).

编辑:对于此示例项目,此问题已解决(请参阅已接受的答案).不幸的是,在我无法分享的更大的项目中,问题仍然存在.作为一种解决方法,我添加了strong复合setter的重复属性(ivars不起作用).新的自定义setter现在看起来像这样:

- (void)setDate:(NSDate *)newDate
{
  if (allowedToSetNewDate)
  {
    self.date_arcretain = newDate; //this property is only there as a workaround. ARC properly retains it, but …
Run Code Online (Sandbox Code Playgroud)

objective-c automatic-ref-counting

11
推荐指数
1
解决办法
3961
查看次数

NSStatusItem在启动时会短暂出现,但会立即消失

几个月后我没有做任何事情,我开始回到可可开发.最初我开始使用Snow Leopard和Xcode 3.我现在正在使用Xcode 4.2运行Lion,我遇到了一些我之前没遇到过的问题.

我相信这可能是因为我之前从未使用过ARC,所以我肯定我错过了一些东西.

我正在尝试创建状态栏应用程序,没有主窗口或停靠图标.当我运行应用程序时,我的应用程序的状态栏图标会短暂显示,大约一秒钟,但随后消失.

继承我的代码.

QuickPlusAppDelegate.h

#import <Cocoa/Cocoa.h>

@interface QuickPlusAppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;
@property (assign) NSStatusItem *statusItem;
@property (weak) IBOutlet NSMenu *statusItemMenu;

@property (strong) NSImage *statusItemIcon;
@property (strong) NSImage *statusItemIconHighlighted;
@property (strong) NSImage *statusItemIconNewNotification;

@end
Run Code Online (Sandbox Code Playgroud)

QuickPlusAppDelegate.m

#import "QuickPlusAppDelegate.h"

@implementation QuickPlusAppDelegate
@synthesize statusItemMenu = _statusItemMenu;

@synthesize window = _window, statusItem = _statusItem;
@synthesize statusItemIcon = _statusItemIcon, 
    statusItemIconHighlighted = _statusItemIconHighlighted, 
    statusItemIconNewNotification = _statusItemIconNewNotification;

- (void) awakeFromNib
{
    NSBundle *appBundle = [NSBundle mainBundle];
    _statusItemIcon = [[NSImage …
Run Code Online (Sandbox Code Playgroud)

cocoa objective-c nsstatusbar automatic-ref-counting

11
推荐指数
1
解决办法
2955
查看次数

Objective C - ARC - 何时使用@autoreleasepool

我正在读一点ARC,我看到了这个:

@interface Address : NSObject {
    @public
    NSString *city;
}
@end

@implementation Address
- (Address*) init: (NSString*) c {
    city = c;

    return self;
}

- (void) dealloc {
    NSLog(@"Destroying address: %@", city);
}
@end

@interface Customer : NSObject {
    NSString *name;
    Address *addr;
}
@end

@implementation Customer
- (Customer*) init: (NSString*) n withAddress: (Address*) a {
    //Note 1: Automatic retain on assignment
    name = n;
    addr = a;

    return self;
}

- (void) dealloc {
    NSLog(@"Destroying: %@", name);
    //Note …
Run Code Online (Sandbox Code Playgroud)

objective-c automatic-ref-counting

11
推荐指数
1
解决办法
1万
查看次数

IOS 5下的Haskell内存管理

我想将Haskell引擎作为C库嵌入到IOS 5项目中 - 在IOS应用程序中运行Haskell代码.所以我有几个问题:

  1. Haskell在ARC下是否有任何已知的内存管理问题?
  2. 我怎样才能确保Haskell不会干扰IOS内存管理?
  3. 什么可能是处理这些问题的有效策略?

haskell ios ios5 automatic-ref-counting

11
推荐指数
1
解决办法
490
查看次数

自动引用计数问题:将保留对象分配给unsafe_unretained变量; 对象将在分配后释放

我收到了这个警告

"自动引用计数问题:将保留对象分配给unsafe_unretained变量;对象将在分配后释放"

这是代码

.H

@interface myObject : NSObject
{
}

@property (assign) id progressEnergy;

@end
Run Code Online (Sandbox Code Playgroud)

.M

@implementation myObject

@synthesize progressEnergy;

-(id)init
{
    if ( ( self = [super init] ) )
    {
        progressEnergy = [[progress alloc] init]; //warning appear on this line
    }

    return self;
}

@end
Run Code Online (Sandbox Code Playgroud)

我已经尝试过了

@property (assign) progress* progressEnergy;
Run Code Online (Sandbox Code Playgroud)

但没有运气

你能帮我弄清楚出了什么问题吗?

object objective-c alloc automatic-ref-counting

11
推荐指数
2
解决办法
9942
查看次数

从NSArray中删除对象

我有一个ARC项目.

我NSArray内心有一些东西.在某些时候,我需要更改数组中的对象.

惠特NSMutableArray我会做的:

[array removeAllObjects];
Run Code Online (Sandbox Code Playgroud)

我确信这个方法会释放数组中包含的所有对象.但是对于NSArray我不能那样做!所以,我的问题是:如果我将数组设置为nil然后重新初始化它,那么数组中包含的旧对象是否真的从内存中释放出来?

array = nil;
array = [[NSArray alloc] initWithArray:newArray];
Run Code Online (Sandbox Code Playgroud)

或者我需要使用NSMutableArray?

release nsmutablearray nsarray ios automatic-ref-counting

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

两个弱变量在Swift中相互引用?

我今天再次尝试尝试理解Swift中的保留周期和弱引用.通过阅读文档,我看到了以下代码示例,其中一个引用变量被标记为weak阻止保留周期:

class Person {
    let name: String
    init(name: String) { self.name = name }
    var apartment: Apartment? 
    deinit { print("\(name) is being deinitialized") }
}

class Apartment {
    let unit: String
    init(unit: String) { self.unit = unit }
    weak var tenant: Person?             // <---- This var is marked as 'weak'
    deinit { print("Apartment \(unit) is being deinitialized") }
}

var john: Person?
var unit4A: Apartment?

john = Person(name: "John Appleseed")
unit4A = Apartment(unit: "4A")

john!.apartment = …
Run Code Online (Sandbox Code Playgroud)

weak-references automatic-ref-counting swift

11
推荐指数
2
解决办法
1160
查看次数

具有值类型的参考周期?

当引用类型的属性具有相互较强的所有权(或具有闭包)时,会发生Swift中的引用循环.

但是,是否有可能只使用值类型的参考周期?


我在游乐场尝试了这个没有成功(错误:不允许递归值类型'A').

struct A {
  var otherA: A? = nil
  init() {
    otherA = A()
  }
}
Run Code Online (Sandbox Code Playgroud)

reference-counting value-type reference-type automatic-ref-counting swift

11
推荐指数
3
解决办法
3989
查看次数

捕获内部闭包列表是否需要将"self"重新声明为"weak"或"unowned"?

如果我有一个闭包传递给这样的函数:

 someFunctionWithTrailingClosure { [weak self] in
     anotherFunctionWithTrailingClosure { [weak self] in 
         self?.doSomething()
     }
 }
Run Code Online (Sandbox Code Playgroud)

如果我[weak self]在someFunctionWithTrailingClosure自己的捕获列表中声明自己没有重新声明它weak再次在捕获列表中anotherFunctionWithTrailingClosure self已经成为一种Optional类型但是它也成为一个weak参考?

谢谢!

closures weak-references automatic-ref-counting swift unowned-references

11
推荐指数
2
解决办法
1168
查看次数

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
查看次数