English 中文(简体)
财产和公共财产之间的区别
原标题:difference between property and public

在接口中申报变量时,两者之间的差别是:

@interface thing:NSObject {
    int x;
    int y;
}
@property int x, y;

:

@interface thing:NSObject {
    @public
    int x;
    int y;
}
最佳回答

@public is an access modifier that means You can access an identity direct such as this:

obj->someAttrib;

@property表示,汇编者应当创造出入口方法(如果你重新使用@synthesize)。

重要的是,财产不一定与归属相称。 你们能够创造这样的美好环境:

@property(readonly) int doSmth;

之后执行:

-(int) doSmth {
   return 123+456;
}

然而,这是非常粗略和不完整的解释(在财产方面尤其如此)。 阅读一些关于ObjC的文章/书籍。

问题回答

财产申报和对应的@synthesize声明将为标明的x和 y.公开宣布贵方变量的数值设定标准入口,将使贵方的编码能够直接查阅贵方的储存价值。

default is @protected so the only difference is if you wanted to access them directly, which is generally considered bad practice, but in the first example you could not do:

thing * aThing = [[thing alloc] init];
aThing->x = 5;

Edit: (as I got voted down by answering the actual question not the implied question.)

In Objective-C there are 3 visibility specifiers: @public; @private; and @protected.

The syntax only affects visibility of iVars, not methods, all methods are publicly visible.

the specifier works for all iVars following until it reaches another specifier;

@public allows direct access of ivars from outside of instances of the class or subclass.

@protected allows direct access of ivars from only in instances of the class or subclasses thereof.

允许在班级内直接使用批量,但不得分级。

as for @property it is a language construct introduced with Objective-C 2.0 that allows you to use the . (dot) accessor for calling methods.

the default behavior is @property int x will reference 2 methods depending if you are using it as an left hand(setter) or right hand(getter) operator, the default getter will be -(int)x; and the default setter will be -(void)setX:(int)x;, @property doesn t in itself create those methods, but allows access to them.

the default behavior can be overridden with the following syntax @property(getter=somethingOtherThanX,setter=somethingOtherThanSetX:)int x;





相关问题
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 ...

热门标签