标签: synthesize

@synthesize vs @dynamic,有什么区别?

什么是实现之间的区别@property@dynamic@synthesize

cocoa cocoa-touch objective-c synthesize

553
推荐指数
7
解决办法
15万
查看次数

我什么时候应该明确使用@synthesize?

据我所知,由于XCode 4.4 @synthesize将自动生成属性访问器.但是刚才我已经阅读了一些代码示例NSUndoManager,并在代码中注意到它@synthesize是明确添加的.喜欢:

@interface RootViewController  ()

@property (nonatomic, strong) NSDateFormatter *dateFormatter;
@property (nonatomic, strong) NSUndoManager *undoManager;

@end

@implementation RootViewController
//Must explicitly synthesize this
@synthesize undoManager;
Run Code Online (Sandbox Code Playgroud)

我现在感到困惑...... @synthesize我什么时候应该明确地添加到我的代码中?

properties objective-c synthesize

69
推荐指数
4
解决办法
5万
查看次数

_和自我之间的区别.在Objective-C中

使用下划线并self在调用时使用Objective-C中的关键字是否有区别@property

财产申报:

@property (weak, nonatomic) NSString *myString;
Run Code Online (Sandbox Code Playgroud)

致电@synthesize酒店:

@synthesize myString = _myString;
Run Code Online (Sandbox Code Playgroud)

如果我想在我的代码中使用它,是否有区别?什么时候?在getter/setter中?

self.myString = @"test";
_myString = @"test";
Run Code Online (Sandbox Code Playgroud)

objective-c getter-setter ios synthesize

48
推荐指数
2
解决办法
2万
查看次数

在什么条件下,在Objective-c中自动@synthesize?

在什么条件下,在Objective-c中自动@synthesize?

也许在使用LLVM 3.0及更高版本时?从阅读网络看起来似乎@synthesize是不必要的从Xcode 4开始.但是当我没有@synthesize属性时,我正在使用Xcode 4并收到警告.

对于为什么不自动合成属性的一些响应似乎暗示@synthesize在某些情况下可以在某些时候被省略.

另一个(旧的)参考暗示@synthesize可能在将来的某个时候是自动的.

objective-c llvm synthesize

31
推荐指数
5
解决办法
1万
查看次数

Objective-C - 只用下划线ivar合成的readonly属性?

如果我理解正确,在Objective-C中,属性会自动与getter和setter合成,实例变量声明为属性名称,并带有下划线prepended(_ivar).

所以,这段代码:

的main.m

#import <Foundation/Foundation.h>
#import "hello.h"

int main(int argc, char *argv[])
{

    @autoreleasepool {

        Hello *hello = [[Hello alloc] init];
        NSLog(@"%@", hello.myString);

        return 0;

    }

}
Run Code Online (Sandbox Code Playgroud)

hello.h

#import <Foundation/Foundation.h>

@interface Hello : NSObject
@property (copy, nonatomic) NSString *myString;
@end
Run Code Online (Sandbox Code Playgroud)

hello.m

#import "hello.h"

@implementation Hello

-(Hello *)init
{

    if (self = [super init]) {
        _myString = @"Hello";
    }

    return self;

}

-(NSString *)myString
{
    return [NSString stringWithFormat:@"%@ %@", _myString, @"World"];
}

@end
Run Code Online (Sandbox Code Playgroud)

可以像这样编译和运行:

bash-3.2$ clang -framework Foundation main.m …
Run Code Online (Sandbox Code Playgroud)

cocoa objective-c synthesize

28
推荐指数
1
解决办法
6572
查看次数

自动@property合成不适用于NSManagedObject子类

在昨晚更新到适用于iOS6的最新版Xcode 4.5之后,我得到了类似的警告和错误

属性'mapAnnotation'需要定义方法'mapAnnotation' - 使用@synthesize,@ dynamic或在此类实现中提供方法实现

因为缺少@synthesize语句,甚至是关于未知iVars的错误,如果我使用它们.

问题是,我认为没有必要写这些@synthesize语句自从Mountain Lion发布的最后一个Xcode Update 4.5以来,并且在我昨晚更新Xcode之前我的所有项目都没有它们(我删除了)从我的文件回来的一大堆@synthesize语句然后它甚至还在Release-Notes中:

•使用属性时,默认情况下会生成Objective-C @synthesize命令.

所以我很困惑,我错过了一个新的项目设置,它会自动生成@synthesize一代吗?

但是,当我创建一个新项目并尝试它时,它甚至都无法工作

xcode properties synthesize ios6

25
推荐指数
1
解决办法
9492
查看次数

自动属性合成不会合成协议中声明的属性 - 哪一个?

它很好,它告诉我有这样一个属性,我必须合成,但有什么办法,我可以弄清楚它不是通过文档?我正在实施UITextInput protocol,无法弄清楚缺少什么.

protocols objective-c ios synthesize

16
推荐指数
1
解决办法
2万
查看次数

自动属性合成不会合成属性 - 新警告iOS8.3

在更新到iOS8.3后,我开始收到一些在iOS8.2上没有的新警告.特别引起我注意的一个;

@property (strong, nonatomic) IBOutlet UITableView *tableView;
Run Code Online (Sandbox Code Playgroud)

它是在'.m'文件中声明的.

iOS8.3中发生了哪些变化,使其成为警告?

Auto property synthesis will not synthesize property 'tableView'; it will be implemented by its superclass, use @dynamic to acknowledge intention
Run Code Online (Sandbox Code Playgroud)

ios synthesize ios8.3

12
推荐指数
3
解决办法
1万
查看次数

当我提供getter和setter时,为什么需要编写@synthesize?

因此自动合成属性非常棒.但是,当您同时提供getter和setter时,会出现错误.

@property (strong, nonatomic) NSArray *testArray;

- (NSArray *)testArray {
    return _testArray;
}

- (void)setTestArray:(NSArray *)testArray {
    _testArray = testArray;
}
Run Code Online (Sandbox Code Playgroud)

错误:Use of undeclared identifier '_testArray'.

添加@synthesize testArray = _testArray;解决了问题.我只是想知道为什么会这样?

properties objective-c synthesize

8
推荐指数
2
解决办法
4277
查看次数

self.variableName与_variableName对@sysnthesize variableName

可能重复:
cocoa objective-c类中变量前面的下划线如何工作?


注意:对于那些试图理解这一点的人来说,我想出了我的困惑的根源.在.h,我有:

...
@interface myClass : parentClass {
className *variableName:
}

@property (strong, nonatomic) className  *variableName;
...
Run Code Online (Sandbox Code Playgroud)

这导致self.variableName和_variableName是.m中的两个不同变量.我需要的是:

...
@interface myClass : parentClass {
className *_variableName:
}

@property (strong, nonatomic) className  *variableName;
...
Run Code Online (Sandbox Code Playgroud)

然后,在类'.m中,self.variableName和_variableName是等价的


在全新的Xcode的4.5+,与ARC,针对iOS 5.0及项目,有一个明显的优势(运行时的效率,速度等)使用_variableNameself.variableName与旧式@synthesize variableName

我的理解是Xcode 4.5+将创建一个_variableName相当于的默认访问器,self.variableName并且唯一不使用的原因@synthesize variableName是为了避免iVars和传入变量之间的混淆,对吗?

对我来说,只是使用self.variableName访问iVar似乎是最直接和明确的,你正在寻找哪个变量.除了打字_self.,使用是否有优势_variableName

objective-c synthesize

8
推荐指数
2
解决办法
9289
查看次数