[英]Using instancetype as the return type of a copy in Objective-C?
[英]Objective-C Block return instancetype
我想用 objective-c 編寫一個返回塊和鏈接調用的屬性,但是如何返回實例類型?
@interface Person : NSObject
@property (nonatomic, weak, readonly) instancetype (^age)(int age);
@property (nonatomic, weak, readonly) instancetype (^name)(NSString *name);
@end
我想要 Person.new.age(10).name(@"Tom"),但是 instancetype 在編譯時出錯所以我必須在這里將 instancetype 更改為Person *
,但是如果我編寫 Person 的 class 子類,我必須再寫一遍:
@interface GoodPerson : NSObject
@property (nonatomic, weak, readonly) GoodPerson * (^age)(int age);
@property (nonatomic, weak, readonly) GoodPerson * (^name)(NSString *name);
@end
那么這里如何使用instancetype呢?
你不能。 instancetype
不是有效的塊返回類型。
使用 instancetype 關鍵字作為返回 class 實例的方法的返回類型(或該類的子類)。 這些方法包括 alloc、init 和 class 工廠方法。
匿名塊不滿足這些條件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.