我需要同时变量和财产吗? [英] Do I need variable and property at the same time?

查看:127
本文介绍了我需要同时变量和财产吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS开发的新手,在理解某些情况时遇到麻烦。
我的问题是我需要变量和属性在同一时间吗?

I'm new in iOS development, and have trouble in understanding of some cases. My question is do I need variable and property at the same time?

例如我看到这样的代码:

For example I saw code like this:

#import <Cocoa/Cocoa.h>

@interface Photo : NSObject {
    NSString* caption;
    NSString* photographer;
}
@property (retain) NSString* caption;
@property (retain) NSString* photographer;

@end

但我知道, strong>字幕和摄影师代码仍然有效:

But, as I know, if I delete variables caption and photographer code still will work:

#import <Cocoa/Cocoa.h>

@interface Photo : NSObject

@property (retain) NSString* caption;
@property (retain) NSString* photographer;

@end

因此,请解释使用第一个和

So, please explain the difference of using first one and second.

感谢您的任何建议!

推荐答案

{}括号是实例变量。

Variables inside the {} parenthesis are instance variables. They're accessible only to the class.

@property定义了由(选项)表示的几种不同的东西。

@property defines a few different things denoted by the (options).

大多数人使用@property来方便您的.m和.h文件中的方法。例如

Most people use @property to convenience methods inside your .m and .h files. For example

@property (strong) NSString *string;

在.h和.m文件中创建两个方法,虽然不可见, p>

Creates two methods inside your .h and .m file, invisible to you though, called:

-(NSString *)string; //returns the string object
-(void)setString:(NSString *)string;

因此,它提高了可读性并帮助减少了大量的锅炉板代码。

So what it does is improve readability and helps reduce a lot of boiler plate code.

现在不再需要在{}括号中定义实例变量。

It's no longer necessary to define the instance variable inside the {} parenthesis now.

如果需要实例变量设置为不同的名称你可以放一个

If you need to have the instance variable set to a different name you can put a

@synthesize string = _string;

因为你是新的在这里Ill添加一些奖金的东西你想想。
self.string = @string
可能不是等价的调用
string = @string

Since you're new at this Ill add some bonus stuff for you to think about. self.string = @"string" May not be an equivalent call as string = @"string"

有@property(strong)NSString * string;和@synthesize字符串;
这样做的原因是通过self.string设置字符串是使用方法实现来设置字符串,可以像这样覆盖:

If you have @property (strong) NSString *string; and @synthesize string; The reason for this is setting the string via self.string is using the method implementation to set the string and may be overridden like this:

-(void)setString:(NSString *)str{
    string = [NSString stringWithFormat:@"%@.jpg", str];
}

因此self.string = @hello将是@hello.jpg 其中as string = @hello只是hello

So self.string = @"hello" would be @"hello.jpg" where as string = @"hello" is just "hello"

同样self.string = @string与[self setString:@string];

Also self.string = @"string"; is much the same as [self setString:@"string"];

有关@property(strong)NSString * string
里面的.h补充:

For clarification on what @property(strong)NSString *string does inside the .h adds:

{
   NSString *_string;
}
-(void)setString:(NSString *)string;
-(NSString *)string;

在.m内:

-(void)setString:(NSString *)string{
_string = string;
}

-(NSString *)string{
return _string;
}

所以没有必要调用@synthesize,除非你要将iVar重命名为别的
@synthesize string = wibblyWobblyTimeyWimey;

So there's no need to call @synthesize unless you want to rename the iVar into something else @synthesize string = wibblyWobblyTimeyWimey;

所以在.h中会是

{
NSString *wibblyWobblyTimeyWimey;
}

.m

-(void)setString:(NSString *)string{
wibblyWobblyTimeyWimey = string;
}

-(NSString *)string {
return wibblyWobblyTimeyWimey;
}

这篇关于我需要同时变量和财产吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆