English 中文(简体)
目标C 释放被宣布为一类的财产?
原标题:Objective-C releasing a property declared in a category?

我有一类是现有类别,为这一类别增加财产和几种方法。

@interface AClass (ACategory) {
    NSString *aProperty;
}

@property (nonatomic, retain) NSString *aProperty;

@end

在执行档案中,当所涉物品被分配时,我要释放这一财产。 然而,如果我宣布这一类别中的<代码>dealloc,则它将从我所理解的原类别中取消交易。 那么,当该物体被分配时,释放该<代码>aproperty的适当方式是什么?

@implementation AClass (ACategory)

@synthesize aProperty;

- (void)dealloc {
    [aProperty release];
    // this will skip the original dealloc method from what I understand
    [super dealloc];
}

@end
最佳回答

但这没有什么问题,因为你的法典是错误的。

  1. You can t declare instance variables in a category; using the latest Objective-C ABI, you can declare new instance variables within a class extension (@interface AClass () {//...), but that is different from a category (@interface AClass (ACategory)).
  2. Even if you could, the syntax for instance variable declaration is that they be enclosed in curly braces after the @interface line.

你可以宣布某类财产,但你必须使用新的试样(hence,@dynamic,而不是@synthesize)来界定其储存。


关于你的实际问题,除非你使用方法宽恕,否则,你可以指先执行压倒性的方法(通过诸如<条码>的操作时间功能而促成)。 我建议不要这样做;这确实令人恐惧和危险。


Update: Explanation of Instance Variables in Class Extensions

班级的延伸与一类,但是一种匿名的,必须列入<代码>。 它希望:

@interface SomeClass () {
    // any extra instance variables you wish to add
}
@property (nonatomic, copy) NSString *aProperty;
@end

其执行<><>must<>>>>>>/strong> 载于“<>>>><<>>>>>>>_>。 因此:

@implementation SomeClass
// synthesize any properties from the original interface
@synthesize aProperty;
// this will synthesize an instance variable and accessors for aProperty,
// which was declared in the class extension.
- (void)dealloc {
    [aProperty release];
    // perform other memory management
    [super dealloc];
}
@end

因此,等级延伸有助于将私人案例变量和方法排除在公共接口之外,但不会帮助你把变量添加到你控制着的一类。 <代码>-dealloc不存在任何问题,因为你只是像你通常那样执行,而列入对你在班级延长范围内提出的变量的任何必要的记忆管理。

<><>>>> 请注意,这只字眼只能与最新的64轨道目标-C ABI.。

问题回答

除此以外,您可使用联系的参考资料至“模拟在现有类别上添加物体的变量”。

基本上,你可以添加一个相关物体如下:

static void* ASI_HTTP_REQUEST;  // declare inside the category @implementation but outside any method    

// And within a method, init perhaps
objc_setAssociatedObject(self, 
    &ASI_HTTP_REQUEST, 
    request, 
    OBJC_ASSOCIATION_RETAIN);

释放相关物体:

// And release the associated object
objc_setAssociatedObject(self,
    &ASI_HTTP_REQUEST, 
    nil, 
    OBJC_ASSOCIATION_RETAIN);

The Apple documentation is here.

我在找我的时候,因此我希望它能帮助别人。





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签