1

私はヘッダー ファイル (.h) を持っていて、宣言したいのですnameが、これらの方法はすべて同じように機能すると思います。機能に違いは見られなかったからです。次の違いを教えてください。

これは両方の宣言で:

@interface someClass : UIViewController
{
    NSString *name;
}

@property (nonatomic, copy) NSString *name;

@end

変数なし:

@interface someClass : UIViewController
{

}

@property (nonatomic, copy) NSString *name;

@end

またはプロパティなし:

@interface someClass : UIViewController
{
    NSString *name;
}

@end
4

2 に答える 2

4
   @interface someClass : UIViewController
   {
     NSString *name;
   }

   @property (nonatomic, copy) NSString *name;

   @end

Doing this you will explicitly declare both a property and an ivar.

A property is just a set of methods:

 - (void)setName:(NSString*)name;
 - (NSString*)name;

An ivar is the memory store holding the value that the property methods manage. This allows you to do:

self.name = ... // access through setter method
name = ... // direct access

The advantage of using properties is that they deal with memory management for you. E.g., in your case, the property is of type copy: this means that with the first syntax (self.name = ...) a copy of the object will be done. If not using properties, you would explicitly need to do: name = [originalString copy]; to obtain the same effect.

Other options you can specify for properties (but not ivars) are: strong and weak ownerships.

Furthermore, a property also represents a public interface to access the variable from outside your class.

Using direct access you are on your own as to memory management (if you are not using ARC). If you are using ARC and don't define properties, you will not be able to control how the memory is managed by specifying the ownership: strong, weak, retain).

 @interface someClass : UIViewController
 {

 }

 @property (nonatomic, copy) NSString *name;

 @end

Here you only declare the properties; the ivar is "inferred" by the @synthesize directive in your implementation file. This is only possible in Objective C 2.0 and later (previously, the ivar declaration as above was mandatory).

The same considerations as above applies, with a minor nuance: with older versions of LLVM (ObjC compiler) you will not be able to reference directly the auto-synthesized ivar; with current version of LLVM, if you omit the @synthesize directive, then an automatic ivar named after your property would also be declared (in your case it would be _name).

This last paragraph may seem a bit "advanced", or contrived, but you can safely ignore it.

 @interface someClass : UIViewController
 {
    NSString *name;
 }

 @end

In this case you are only declaring the ivar. No accessor methods. You will need to handle memory management on your own (if not using ARC), futhermore you will not be able to access the variable from outside the class. For that you need accessors.

Hope this helps.

于 2013-01-22T09:56:54.257 に答える
0

ケース 1:

は古い方法です。ここでは、@propertyと 変数は関連していません@synthesize name = name

アクセス方法 : 変数 :name = @"hello"; //direct access to viariable

セッター/ゲッター:self.name = @"hello" // set value to name using setName: selector

最新の xcode では、プロパティだけで十分です。

ケース 2: 新しい xcode スタイル。ここでは、合成と変数の作成はコンパイラによって処理されます。(コードが 2 行少なくなり、メモリ管理にも役立ちます)

アクセス方法 : 変数 :_name = @"hello"; //direct access to viariable

セッター/ゲッター:self.name = @"hello" // set value to name using setName: selector

ケース 3: ここでは、名前は単なる変数であり、setter も getter もありません。プロパティ(または)セッターとゲッターがないと、これはローカル変数と同じくらい良く、他のオブジェクトからアクセスできません。

于 2013-01-22T09:59:25.673 に答える