Hello Worldチュートリアルでは、「@ synthesize userName=_userName;」をコメントアウトしようとしました。HelloWorldViewController.mファイル内。https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW1によると、「...あなた@synthesizeディレクティブを使用して、プロパティのセッターメソッドやゲッターメソッドを@implementationブロック内に指定しない場合は、それらを合成する必要があることをコンパイラーに通知します。」
私にとって奇妙なことは、「@ synthesize userName=_userName;」でも コメントアウトされた、[self.userNamelength]やself.userName= [[self textField] text]などのHelloWorldViewController.mファイル内のステートメントのその後の出現は、Xcodeによるエラーまたは警告とは見なされません(私はV4.4.1の場合)。これはどのように可能ですか?
私が理解したように、宣言されたプロパティに@synthesizeがない場合、プロパティは定義されていません。さらに、@ synthesizeがないということは、実装されているプロパティのゲッターまたはセッターメソッドがないことを意味します。Hello Worldアプリがコンパイルされ、完全に実行されるのはなぜですか?
コードの一部はここにあります:
#import "HelloWorldViewController.h"
@implementation HelloWorldViewController
//@synthesize userName = _userName;
@synthesize label;
@synthesize textField;
...
- (IBAction)changeGreeting:(id)sender {
[self setUserName:[[self textField] text]];
if([self.userName length]==0){
[self setUserName: @"World"];
}
NSString *greeting=[[NSString alloc] initWithFormat:@"Hello, %@!", self.userName];
self.label.text=greeting;
- (IBAction)changeGreeting:(id)sender {
self.userName = [[self textField] text];
if([self.userName length]==0){
[self setUserName: @"World"];
}
NSString *greeting=[[NSString alloc] initWithFormat:@"Hello, %@!", self.userName];
self.label.text=greeting;
}
@end
Xcodeは、上記のコードに対して警告やエラーを出しません。