渺小的我
渺小的我

Reputation: 23

Objective-C Block return instancetype

I want write a property with objective-c that return block and linked call, but how to return instancetype?

@interface Person : NSObject

@property (nonatomic, weak, readonly) instancetype (^age)(int age);
@property (nonatomic, weak, readonly) instancetype (^name)(NSString *name);

@end

I Want Person.new.age(10).name(@"Tom"), but instancetype is error in compile time So i have to change instancetype to Person * here, but if I write a class subclass of Person, I have to write it again:

@interface GoodPerson : NSObject

@property (nonatomic, weak, readonly) GoodPerson * (^age)(int age);
@property (nonatomic, weak, readonly) GoodPerson * (^name)(NSString *name);

@end

So how to use instancetype here?

Upvotes: 0

Views: 214

Answers (1)

Kamil.S
Kamil.S

Reputation: 5543

You can't. instancetype is not a valid block return type.

From https://developer.apple.com/library/archive/releasenotes/ObjectiveC/ModernizationObjC/AdoptingModernObjective-C/AdoptingModernObjective-C.html

Use the instancetype keyword as the return type of methods that return an instance of the class they are called on (or a subclass of that class). These methods include alloc, init, and class factory methods.

An anonymous block does not fulfil these criteria.

Upvotes: 1

Related Questions