- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个关于属性和 ivars 的一般性问题。
我见过许多使用属性的不同示例,这让我有点困惑。
方法 1 仅使用没有相应 ivar 的属性。
@property (...) Type *name;
@synthesize name;
方法 2 使用属性和 ivar
@interface{
Type *ivarName;
}
@property (...) Type *name;
@synthesize name = ivarName;
方法 3 忽略属性并使用 ivars
@interface{
Type *ivarName;
}
ivar = ...;
我目前对大多数事情都使用方法 1,它很管用。但我开始怀疑我是否可能在这里遗漏了一些东西。我已经阅读了很多关于 ivars VS 属性的问题,但似乎没有人真正关心它们如何协同工作。
在我见过的大多数示例项目中,都使用了方法 2。所以我的问题是:定义一个属性和一个 ivar,然后将属性分配给 ivar,是否比仅仅拥有一个属性有任何优势?
解决方案是否像这样简单:只有通过属性才能从“外部”设置 ivar?
我已阅读:Must every ivar be a property?和 Property vs. ivar in times of ARC但未能得出最终结论。
最佳答案
is the solution as simple as: only with a property can an ivar be set from 'outside'?
基本上,是的。 Obj-C 中的 Ivars(默认情况下)是“ protected ”,这意味着编译器不允许您在对象自身代码的外部访问它们。例如,给定以下类声明:
@interface Dunstable : NSObject
{
NSString * crunk;
}
@end
您可能认为您可以在创建对象后访问 ivar,但尝试会导致错误:
Dunstable * d = [[Dunstable alloc] init];
d->crunk = @"Forsooth"; // Error: "Instance variable 'crunk' is protected
这就是 ObjC 使用访问器方法的原因。在声明的属性出现之前,手动定义它们是强制性的:
@implementation Dunstable
- (NSString *)crunk {
return crunk; // implicit ivar access, i.e. self->crunk
}
- (void)setCrunk: (NSString *)newCrunk {
[newCrunk retain];
[crunk release];
crunk = newCrunk;
}
@end
现在,使用 @property
和 @synthesize
指令为您创建那些访问器方法(以及变量本身)。 (setter 中的手动内存管理当然在 ARC 下也已过时。)
有可能制作一个可以从对象外部访问的ivar:
@interface Dunstable : NSObject
{
@public
NSNumber * nonce;
}
@end
Dunstable * d = [[Dunstable alloc] init];
d->nonce = [NSNumber numberWithInt:2]; // Works fine
但这不是好的 Objective-C 风格。
The Objective-C Programming Language文档包含关于此的“历史注释”:
Note: Historically, the interface required declarations of a class’s instance variables, the data structures that are part of each instance of the class. These were declared in braces after the
@interface
declaration and before method declarations: [...] Instance variables represent an implementation detail, and should typically not be accessed outside of the class itself. Moreover, you can declare them in the implementation block or synthesize them using declared properties. Typically you should not, therefore, declare instance variables in the public interface and so you should omit the braces.
这是一个相当大的变化(我真的很惊讶该文档中不再为 @interface
中声明的 ivar 提供语法),但它肯定是更好的。您应该使用声明的属性;他们做正确的事,让您的代码更干净、更安全。
关于iphone - 具有相应ivars的属性概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9854610/
你能比较一下属性吗 我想禁用文本框“txtName”。有两种方式 使用javascript,txtName.disabled = true 使用 ASP.NET, 哪种方法更好,为什么? 最佳答案 我
Count 属性 返回一个集合或 Dictionary 对象包含的项目数。只读。 object.Count object 可以是“应用于”列表中列出的任何集合或对
CompareMode 属性 设置并返回在 Dictionary 对象中比较字符串关键字的比较模式。 object.CompareMode[ = compare] 参数
Column 属性 只读属性,返回 TextStream 文件中当前字符位置的列号。 object.Column object 通常是 TextStream 对象的名称。
AvailableSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。 object.AvailableSpace object 应为 Drive 
Attributes 属性 设置或返回文件或文件夹的属性。可读写或只读(与属性有关)。 object.Attributes [= newattributes] 参数 object
AtEndOfStream 属性 如果文件指针位于 TextStream 文件末,则返回 True;否则如果不为只读则返回 False。 object.A
AtEndOfLine 属性 TextStream 文件中,如果文件指针指向行末标记,就返回 True;否则如果不是只读则返回 False。 object.AtEn
RootFolder 属性 返回一个 Folder 对象,表示指定驱动器的根文件夹。只读。 object.RootFolder object 应为 Dr
Path 属性 返回指定文件、文件夹或驱动器的路径。 object.Path object 应为 File、Folder 或 Drive 对象的名称。 说明 对于驱动器,路径不包含根目录。
ParentFolder 属性 返回指定文件或文件夹的父文件夹。只读。 object.ParentFolder object 应为 File 或 Folder 对象的名称。 说明 以下代码
Name 属性 设置或返回指定的文件或文件夹的名称。可读写。 object.Name [= newname] 参数 object 必选项。应为 File 或&
Line 属性 只读属性,返回 TextStream 文件中的当前行号。 object.Line object 通常是 TextStream 对象的名称。 说明 文件刚
Key 属性 在 Dictionary 对象中设置 key。 object.Key(key) = newkey 参数 object 必选项。通常是 Dictionary 
Item 属性 设置或返回 Dictionary 对象中指定的 key 对应的 item,或返回集合中基于指定的 key 的&
IsRootFolder 属性 如果指定的文件夹是根文件夹,返回 True;否则返回 False。 object.IsRootFolder object 应为&n
IsReady 属性 如果指定的驱动器就绪,返回 True;否则返回 False。 object.IsReady object 应为 Drive&nbs
FreeSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。只读。 object.FreeSpace object 应为 Drive 对象的名称。
FileSystem 属性 返回指定的驱动器使用的文件系统的类型。 object.FileSystem object 应为 Drive 对象的名称。 说明 可
Files 属性 返回由指定文件夹中所有 File 对象(包括隐藏文件和系统文件)组成的 Files 集合。 object.Files object&n
我是一名优秀的程序员,十分优秀!