@property @synthesize @dynamic xcode 中的差异
所以我想问的是下面
这是我的头文件
NSString *myString;
在 m.h 中:文件
-(void)someMethod{
myString = [NSString stringWithString = @"Hello"];
NSLog(@"%@",myString);
}
-(void)dealloc{
[myString release];
}
-(void)viewDidUnload{
[myString release];
myString=nil;
}
好的,现在其他情况
在我的头文件中
NSString *myString;
@property (nonatomic,retain) NSString *myString;
在 m.h 中file
@synthesize myString;
-(void)someMethod{
NSString *tempString = [[NSString alloc] initWithString:@"Hello"];
self.myString = tempString;
[tempString release];
NSLog(@"%@",myString);
}
-(void)dealloc{
[myString release];
}
-(void)viewDidUnload{
[myString release];
self.myString=nil;
}
我真的需要一个白痴指南,因为我还不明白它。两者都有效。我在 dealloc 和 viewDidUnload 中使用的版本是否正确?预先感谢您
So what i want to ask is below
Here is my header file
NSString *myString;
In the m. file
-(void)someMethod{
myString = [NSString stringWithString = @"Hello"];
NSLog(@"%@",myString);
}
-(void)dealloc{
[myString release];
}
-(void)viewDidUnload{
[myString release];
myString=nil;
}
Ok now the other situation
In my header file
NSString *myString;
@property (nonatomic,retain) NSString *myString;
In the m. file
@synthesize myString;
-(void)someMethod{
NSString *tempString = [[NSString alloc] initWithString:@"Hello"];
self.myString = tempString;
[tempString release];
NSLog(@"%@",myString);
}
-(void)dealloc{
[myString release];
}
-(void)viewDidUnload{
[myString release];
self.myString=nil;
}
I really need an idiot guide for this cause I do not understant it yet. Both works. Also am i using the release in dealloc and viewDidUnload correct?? Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有关属性的信息,而不是权威指南:
@property
的一个优点是,通过@synthesize
,它们可以创建处理保留和释放(如果适用)和工作的 setter 和 getter带或不带 ARC(稍作修改)。属性不再需要声明其关联的 ivar,它们将自动生成。
属性可以放置在头文件 (.h) 中供公共使用,也可以放置在类扩展中的实现文件 (.m) 中供类中私有使用。
@property
和@synthesize
语句与允许“点表示法”无关。 “点符号”可以被视为访问 setter 和 getter 的括号形式的替代(实际上使用更普遍,但使用最好仅限于 getter/setter)。[self myIvar]
相当于self.myIvar
并且[self setMyIvar:myValue]
相当于self.myIvar = myValue< /代码>。
点表示法可以用于非属性,例如 NSStrings:myString.length 工作正常并且是合理可接受的用法。
点符号与属性无关,但 Xcode 只会为属性提供自动完成功能。
Info about properties, not a definitive guide:
One advantage of
@property
is that with@synthesize
they create setters and getters that handle the retain and releases (as applicable) and work the with or without ARC (with minor modifications).properties no longer need their associated ivars to be declared, they will be automatically generated.
properties can be placed in the header file (.h) for public use or in the implementation file (.m) in a class extension for private use in the class.
@property
and@synthesize
statements have nothing to do with allowing "dot notation". "dot notation" can be viewed as a substitution for the bracket form of accessing a setters and getters (actually use is more general but use is best restricted to getter/setters).[self myIvar]
is equivalent toself.myIvar
and[self setMyIvar:myValue]
is equivalent toself.myIvar = myValue
.dot notation can be used on non-properties such as NSStrings: myString.length works fine and is reasonably acceptable usage.
dot notation has nothing to do with properties however Xcode will only offer auto completions for properties.
@property
和@synthesize
提供 getter 和 setter(访问器)方法,而无需您自己编写。该声明是使用@property
进行的,并使用@synthesize
实现的。因此,在主程序中,当您创建一个新的类对象时(假设您的类名为
MyClass
,其中包含MyClass.m
、MyClass.h
),您可以使用点运算符访问字符串变量myString
。如果您的对象名为NewObject
,那么您可以使用NewObject.MyString
访问主程序内的字符串。您还可以使用它来设置字符串的值(即
NewObject.MyString = OtherString
)。非常方便且节省时间。它们都可以工作,因为您是从类内部访问变量,因此不需要设置访问器。对于
-(void)dealloc
,您还需要其中的[super dealloc]
来释放超类的变量。您无需像在-(void)dealloc
方法中那样在viewDidUnload
中释放MyString
。当您在
-(void)viewDidLoad
中分配内存时,您需要在-(void)viewDidUnload
中释放它,但您没有在这里,所以它不是需要。@property
and@synthesize
provides the getter and setter (accessors) methods rather than you having to write it out yourself. The declaration is made with@property
and it is implemented with@synthesize
.So in the main program when you create a new class object (Assuming your class is called
MyClass
withMyClass.m
,MyClass.h
), you are able to access your string variablemyString
using the dot operator. If your object is calledNewObject
, then you can access the string inside the main program withNewObject.MyString
.You can also use this to set a value for string (i.e.
NewObject.MyString = OtherString
). Very handy and time-saving. They both work because you are accessing the variables from within the class and so you wouldn't need to set the accessors.For the
-(void)dealloc
you also need[super dealloc]
inside there to release the variables of the superclass. You don't need to releaseMyString
inviewDidUnload
as you have done it in the-(void)dealloc
method.When you allocate memory in
-(void)viewDidLoad
, then you would need to release it in-(void)viewDidUnload
, but you haven't here so it isn't needed.