首页 > 解决方案 > 为什么 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 错了吗?

标签: objective-c

解决方案


推荐阅读