summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAdam Bard <github@adambard.com>2015-10-31 18:20:13 +0800
committerAdam Bard <github@adambard.com>2015-10-31 18:20:13 +0800
commit208c35decd500c53c2a22929e7de0f839f0c7271 (patch)
tree7a1fec21556c686ca3c139c6868f6487167528be
parent4f1b8654a01f8c56ee2f42f70406a497e4456ff5 (diff)
parent4508ee45d8924ee7b17ec1fa856f4e273c1ca5c1 (diff)
Merge pull request #1925 from benpious/master
[Objective-C/en] Adds description of how to define generic classes in Xcode 7.0
-rw-r--r--objective-c.html.markdown46
1 files changed, 46 insertions, 0 deletions
diff --git a/objective-c.html.markdown b/objective-c.html.markdown
index f130ea0c..05bb5c6a 100644
--- a/objective-c.html.markdown
+++ b/objective-c.html.markdown
@@ -599,6 +599,52 @@ int main (int argc, const char * argv[]) {
@end
+// Starting in Xcode 7.0, you can create Generic classes,
+// allowing you to provide greater type safety and clarity
+// without writing excessive boilerplate.
+@interface Result<__covariant A> : NSObject
+
+- (void)handleSuccess:(void(^)(A))success
+ failure:(void(^)(NSError *))failure;
+
+@property (nonatomic) A object;
+
+@end
+
+// we can now declare instances of this class like
+Result<NSNumber *> *result;
+Result<NSArray *> *result;
+
+// Each of these cases would be equivalent to rewriting Result's interface
+// and substituting the appropriate type for A
+@interface Result : NSObject
+- (void)handleSuccess:(void(^)(NSArray *))success
+ failure:(void(^)(NSError *))failure;
+@property (nonatomic) NSArray * object;
+@end
+
+@interface Result : NSObject
+- (void)handleSuccess:(void(^)(NSNumber *))success
+ failure:(void(^)(NSError *))failure;
+@property (nonatomic) NSNumber * object;
+@end
+
+// It should be obvious, however, that writing one
+// Class to solve a problem is always preferable to writing two
+
+// Note that Clang will not accept generic types in @implementations,
+// so your @implemnation of Result would have to look like this:
+
+@implementation Result
+
+- (void)handleSuccess:(void (^)(id))success
+ failure:(void (^)(NSError *))failure {
+ // Do something
+}
+
+@end
+
+
///////////////////////////////////////
// Protocols
///////////////////////////////////////