objective-c - 为什么 instancetype 不能使用点语法获取属性?instancetype 有时是 id 类型吗?
问题描述
@interface TestInstancetypeObject : NSObject
@property (nonatomic, copy) NSString *testString;
+ (instancetype)shareInstance;
+ (NSString *)getString;
@end
@implementation TestInstancetypeObject
+ (instancetype)shareInstance {
static TestInstancetypeObject *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[TestInstancetypeObject alloc] init];
});
return instance;
}
+ (NSString *)getString {
NSString *string0 = [[self shareInstance] testString];
NSString *string1 = [TestInstancetypeObject shareInstance].testString;
NSString *string3 = [self shareInstance].testString;//error:: Property 'testString' not found on object of type 'id'
return string0;
}
@end
当我尝试在类方法中通过 [self shareInstance].propertyName 获取属性时,出现错误:Property 'xxx' not found on object of 'id'.But [[self shareInstance] propertyName] 是成功的。为什么[self shareInstance].propertyName 错了吗?
解决方案
推荐阅读
- php - 创建动态 404 页面
- angular - 安装时不显示离子cordova-plugin-android-permissions
- mysql - 使用 SQL (MySQL) 选择一周中的每隔一天
- vbscript - 在网页上查找值并返回消息提示的 VBScript
- unity3d - 使用触发事件创建 2D 精灵?
- python - 尝试设置全局对象,但在尝试从另一个文件访问子类时未定义获取 NameError 对象
- python - Travis pylint 构建失败并出现关于单次调度的错误
- android - 为什么 Android Studio 不会在 Android Pie (9.0) 上运行应用程序?
- python - python-docx 中的项目符号列表
- python - SonarQube 不扫描 Maven 项目中的其他文件夹