diff options
Diffstat (limited to 'objective-c.html.markdown')
-rw-r--r-- | objective-c.html.markdown | 59 |
1 files changed, 54 insertions, 5 deletions
diff --git a/objective-c.html.markdown b/objective-c.html.markdown index f130ea0c..097cb846 100644 --- a/objective-c.html.markdown +++ b/objective-c.html.markdown @@ -1,13 +1,12 @@ --- - language: Objective-C contributors: - ["Eugene Yagrushkin", "www.about.me/yagrushkin"] - ["Yannick Loriot", "https://github.com/YannickL"] - ["Levi Bostian", "https://github.com/levibostian"] - ["Clayton Walker", "https://github.com/cwalk"] + - ["Fernando Valverde", "http://visualcosita.xyz"] filename: LearnObjectiveC.m - --- Objective-C is the main programming language used by Apple for the OS X and iOS operating systems and their respective frameworks, Cocoa and Cocoa Touch. @@ -20,6 +19,10 @@ It is a general-purpose, object-oriented programming language that adds Smalltal Multi-line comments look like this */ +// XCode supports pragma mark directive that improve jump bar readability +#pragma mark Navigation Functions // New tag on jump bar named 'Navigation Functions' +#pragma mark - Navigation Functions // Same tag, now with a separator + // Imports the Foundation headers with #import // Use <> to import global files (in general frameworks) // Use "" to import local files (from project) @@ -148,13 +151,13 @@ int main (int argc, const char * argv[]) [mutableDictionary setObject:@"value1" forKey:@"key1"]; [mutableDictionary setObject:@"value2" forKey:@"key2"]; [mutableDictionary removeObjectForKey:@"key1"]; - + // Change types from Mutable To Immutable //In general [object mutableCopy] will make the object mutable whereas [object copy] will make the object immutable NSMutableDictionary *aMutableDictionary = [aDictionary mutableCopy]; NSDictionary *mutableDictionaryChanged = [mutableDictionary copy]; - - + + // Set object NSSet *set = [NSSet setWithObjects:@"Hello", @"Hello", @"World", nil]; NSLog(@"%@", set); // prints => {(Hello, World)} (may be in different order) @@ -599,6 +602,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 /////////////////////////////////////// |