Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用@synthesis时,哪些方法是正确的_Iphone_Ios4 - Fatal编程技术网

Iphone 使用@synthesis时,哪些方法是正确的

Iphone 使用@synthesis时,哪些方法是正确的,iphone,ios4,Iphone,Ios4,当涉及到合成属性时,我有点困惑 假设我在*.h文件中有此属性 @property (nonatomic, retain) NSString *aString; 在我的*.m文件中,我合成了它。 @综合收敛 我知道当我使用synthesis时,编译器将生成setter和getter。但是这些二传手和传接手看起来怎么样 当我给阿斯汀一个值时,哪些方法是正确的 self.aString = @"My New String"; // This will use the setter? aString

当涉及到合成属性时,我有点困惑

假设我在*.h文件中有此属性

@property (nonatomic, retain) NSString *aString;
在我的*.m文件中,我合成了它。 @综合收敛

我知道当我使用synthesis时,编译器将生成setter和getter。但是这些二传手和传接手看起来怎么样

当我给阿斯汀一个值时,哪些方法是正确的

self.aString = @"My New String"; // This will use the setter?
aString = @"My new String"; //This will not use the setter and retain the string, right?
aString = [@"My new string" retain]; //This will not use the setter, but it will retain the string ?
[self setAString:@"My new string"]; //This will also use the setter?

提前谢谢

关于代码的外观,请看一看

关于哪一行使用getter/setter:只有第1行和第4行使用getter/setter

self.aString = @"My New String";
[self setAString:@"My new string"];

关于代码的外观,请看一看

关于哪一行使用getter/setter:只有第1行和第4行使用getter/setter

self.aString = @"My New String";
[self setAString:@"My new string"];

下面是正确的设置器

self.aString = @"My New String";
[self setAString:@"My new string"];

下面是正确的设置器

self.aString = @"My New String";
[self setAString:@"My new string"];

当您使用“点”语法(例如
self.aString
self.aString=…
)或消息语法(例如
[self-aString]
[self-setAString:…]
)时,将使用getter和setter

直接分配给支持变量(在您的情况下,名称有点混乱,aString)将不会使用合成方法。要更改支持变量的名称,请在接口中将其声明为私有变量(在.h中):

然后像这样合成它:

@synthesize aString = aString_;

这将使用
aString\
作为属性的支持变量,因此您可以通过使用
aString\

直接引用属性的存储。当您使用“点”语法(例如
self.aString
self.aString=…
)或消息语法(例如
[self aString]时,会使用getter和setter)
[自我设置:…]

直接分配给支持变量(在您的情况下,名称有点混乱,aString)将不会使用合成方法。要更改支持变量的名称,请在接口中将其声明为私有变量(在.h中):

然后像这样合成它:

@synthesize aString = aString_;
这将使用
aString\uu
作为属性的支持变量,因此您可以在执行以下操作时使用
aString\u
直接引用属性的存储:

@property (nonatomic, retain) NSString *aString;
……然后是:

@synthesize aString;
…你们班有三件基本的东西:

  • 一个名为
    aString
    的实例变量,可以在类中直接引用
  • 一个带有签名的getter函数
    -(NSString*)
  • 具有签名
    -(void)setAString:(NSString*)值的setter函数(并释放任何以前保留的值,如果存在)
    就实现而言,这些生成的方法和字段可能如下所示:

    //in the .h file
    @interface MyClass {
        NSString* aString;
    }
    
    - (NSString*) aString;
    - (void) setAString: (NSString*) value;
    
    @end
    
    
    //in the .m file
    @implementation MyClass
    
    - (NSString*) aString {
        return aString;
    }
    
    - (void) setAString: (NSString*) value {
        if (aString != value) {
            [aString release];
            aString = [value retain];
        }
    }
    
    @end
    
    因此,要回答您其余的问题:

    self.aString = @"My New String"; // This will use the setter?  Yes, and it will retain the string.
    aString = @"My new String"; //This will not use the setter and retain the string, right?  Right.
    aString = [@"My new string" retain]; //This will not use the setter, but it will retain the string?  Right again.
    [self setAString:@"My new string"]; //This will also use the setter?  Yep.
    
    至于哪一种是正确的,它们都是正确的,这取决于您想要什么,并且假设您了解使用每一种方法之间的行为差异,特别是在释放/保留属性值方面。例如,此代码将泄漏内存:

    self.aString = @"Value 1";  //go through the setter, which retains the string
    aString = @"Value 2";        //bypass the setter, the 'Value 1' string will not be released
    
    …此等效代码不会:

    self.aString = @"Value 1";  //go through the setter, which retains the string
    self.aString = @"Value 2";  //go through the setter again to release the first string
    
    当您这样做时:

    @property (nonatomic, retain) NSString *aString;
    
    ……然后是:

    @synthesize aString;
    
    …你们班有三件基本的东西:

  • 一个名为
    aString
    的实例变量,可以在类中直接引用
  • 一个带有签名的getter函数
    -(NSString*)
  • 具有签名
    -(void)setAString:(NSString*)值的setter函数(并释放任何以前保留的值,如果存在)
    就实现而言,这些生成的方法和字段可能如下所示:

    //in the .h file
    @interface MyClass {
        NSString* aString;
    }
    
    - (NSString*) aString;
    - (void) setAString: (NSString*) value;
    
    @end
    
    
    //in the .m file
    @implementation MyClass
    
    - (NSString*) aString {
        return aString;
    }
    
    - (void) setAString: (NSString*) value {
        if (aString != value) {
            [aString release];
            aString = [value retain];
        }
    }
    
    @end
    
    因此,要回答您其余的问题:

    self.aString = @"My New String"; // This will use the setter?  Yes, and it will retain the string.
    aString = @"My new String"; //This will not use the setter and retain the string, right?  Right.
    aString = [@"My new string" retain]; //This will not use the setter, but it will retain the string?  Right again.
    [self setAString:@"My new string"]; //This will also use the setter?  Yep.
    
    至于哪一种是正确的,它们都是正确的,这取决于您想要什么,并且假设您了解使用每一种方法之间的行为差异,特别是在释放/保留属性值方面。例如,此代码将泄漏内存:

    self.aString = @"Value 1";  //go through the setter, which retains the string
    aString = @"Value 2";        //bypass the setter, the 'Value 1' string will not be released
    
    …此等效代码不会:

    self.aString = @"Value 1";  //go through the setter, which retains the string
    self.aString = @"Value 2";  //go through the setter again to release the first string
    

    self.aString=@“我的新字符串”;和[自我设置:@“我的新字符串”];我将使用seter!self.aString=@“我的新字符串”;和[自我设置:@“我的新字符串”];我将使用seter!非常好的回答,学习了一些关于obj-c编程的新知识,可能会修复我的一些错误!那个设定者应该检查
    aString!=值
    或保留计数可能意外降至0<代码>如果(aString!=value){[aString release];aString=[value retain];}
    谢谢!这和我想的差不多。但很高兴得到证实:)回答得很好,学到了一些关于obj-c编程的新东西,可能会修复我的一些错误!那个设定者应该检查
    aString!=值
    或保留计数可能意外降至0<代码>如果(aString!=value){[aString release];aString=[value retain];}
  • 谢谢!这和我想的差不多。但很高兴得到确认:)