@property @synthesize @dynamic xcode 中的差异

发布于 2024-12-18 07:08:39 字数 921 浏览 1 评论 0原文

所以我想问的是下面

这是我的头文件

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Spring初心 2024-12-25 07:08:39

有关属性的信息,而不是权威指南:

@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 to self.myIvar and [self setMyIvar:myValue] is equivalent to self.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.

听你说爱我 2024-12-25 07:08:39

@property@synthesize 提供 getter 和 setter(访问器)方法,而无需您自己编写。该声明是使用 @property 进行的,并使用 @synthesize 实现的。

因此,在主程序中,当您创建一个新的类对象时(假设您的类名为 MyClass ,其中包含 MyClass.mMyClass.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 with MyClass.m, MyClass.h), you are able to access your string variable myString using the dot operator. If your object is called NewObject, then you can access the string inside the main program with NewObject.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 release MyString in viewDidUnload 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文