简体   繁体   English

目标c:我需要属性的自动内存管理,但不能从其他类进行访问

[英]Objective c: i want the automatic memory management of properties, but no access from other classes

I've kind of been confused about properties. 我对属性有些困惑。 Some people say to always use setters and getters for ivars, even within the ivar's class. 有人说,即使在ivar的类中,也总是使用setter和getter来代替ivars。 So if "name" is an ivar, when referring to it, one should always use "self.name". 因此,如果“名称”是一个ivar,则在引用它时,应始终使用“ self.name”。 Always. 总是。 Even if you're in the same class that "name" is declared in. 即使您在同一类中,也声明了“名称”。

First, is that correct advice? 首先,这是正确的建议吗? Second, what if I wish to reap the automatic memory management that comes with declaring "name" as a property and synthesizing it, but I don't want to give other classes access to change "name"? 其次,如果我希望获得将“名称”声明为属性并进行综合所带来的自动内存管理,但又不想让其他类访问更改“名称”的权限,该怎么办? I guess it would be sort of a private property? 我想那会是私有财产?

Thanks! 谢谢!

Yes, you should always try to use the property accessors when possible. 是的,您应该尽可能尝试使用属性访问器。 Using ARC alleviates these concerns somewhat, but it's still good style. 使用ARC可以在某种程度上缓解这些问题,但是它仍然是不错的样式。 As for your second question, you can declare the property as readonly in the public header file and redefine it in a class extension: 关于第二个问题,您可以在公共头文件中将该属性声明为readonly ,然后在类扩展中重新定义该属性:

In MyClass.h : MyClass.h

@interface MyClass : NSObject

@property (strong, readonly, nonatomic) id foo;

@end

In MyClass.m : MyClass.m

@interface MyClass()

@property (strong, readwrite, nonatomic) id foo;

@end


@implementation MyClass

@synthesize foo = _foo;

// The rest of your code goes here.

@end

This will allow you to call [self setFoo:foo] all day inside of MyClass 's implementation, but not other classes. 这将允许您全天在MyClass的实现中调用[self setFoo:foo] ,但不能调用其他类。

For ivars which are accessed externally, I generally use properties to access the ivar from within the class, for ivars which are only used internally (usually BOOL, NSUInteger, NSInteger, etc), I use the ivar directly. 对于从外部访问的ivars,我通常使用属性从类内部访问ivar,对于仅在内部使用的ivars(通常是BOOL,NSUInteger,NSInteger等),我直接使用ivar。 I do however access an consistently within the class (ie if I'm using a property to access it, I always use a property). 但是,我确实在类中访问一个一致的对象(即,如果我使用属性来访问它,则我总是使用属性)。

For the second part of your question. 对于您问题的第二部分。 You can create a readonly property in the class's interface definition and within the same file as the implementation create a category with the read-write property. 您可以在类的接口定义中创建一个只读属性,并在与实现相同的文件中创建一个具有读写属性的类别。 For example: 例如:

MyClass.h MyClass.h

@interface MyClass : NSObject
{
    NSString * name;
}
@property (nonatomic, readonly) NSString * name;
@end 

MyClass.m MyClass.m

@interface MyClass()
@property (nonatomic, retain) NSString * name;
@end

@implementation MyClass
@synthesize name;
-(void)dealloc
{
    [name release];
    [super dealloc];
    return;
}
@end

Keep in mind, that although another class accessing the method -setName: may cause compile warnings or errors, another class may still call -(id)performSelector:withObject: with without an error. 请记住,尽管另一个类访问方法-setName:可能导致编译警告或错误,但是另一个类仍可以调用-(id)performSelector:withObject:且没有错误。

For instance: 例如:

MyClass * test = [[MyClass alloc] init];
test.name = @"David";

is functionally the same as: 在功能上与:

MyClass * test = [[MyClass alloc] init];
[test performSelector:@selector(setName:) withObject:@"David"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM