-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams.json
1 lines (1 loc) · 10.1 KB
/
params.json
1
{"name":"iOS & Objective-C Diary","tagline":"A small diary about useful iOS/Objective-C topics","body":"# iOS & Objective-C Diary\r\n\r\n### Disclaimer\r\n\r\nThis is a page created with the purpose of gathering what I consider to be the most important and useful topics I've learned about iOS and Objective-C. Keep in mind that the contents of this document may present some errors or misconceptions regarding this topic. Nevertheless, you are free to use or reference it any way you want.\r\n\r\n***\r\n\r\n## Table of contents\r\n\r\n+ [Purchasing a developer license](#purchasing-a-developer-license)\r\n+ [Create, download and install certificates](#create-download-and-install-certificates)\r\n+ [Create an App ID](#create-an-app-id)\r\n+ [Objective-C variable types](#objective-c-variable-types)\r\n+ [NSString and NSMutableString](#nsstring-and-nsmutablestring)\r\n+ [Actions](#actions)\r\n+ [Outlets](#outlets)\r\n+ [Labels](#labels)\r\n+ [TextField](#textField)\r\n+ [Dismissing the Keyboard](#dismissing-the-keyboard)\r\n+ [NSTimer](#nstimer)\r\n\r\n\r\n***\r\n\r\n## Purchasing a developer license\r\nIn order to be able to submit your apps to the store, you will need to enroll on a paid membership for iOS app distribution, but **you do not have to do this step** if, for now, you just want to learn or start creating your app.\r\n\r\n1. Go to http://developer.apple.com\r\n2. Then click **Program** on the top bar\r\n3. Click **Enroll**\r\n4. Sign in or create a new Apple ID\r\n5. Purchase the license for iOS Development\r\n\r\n\r\n***\r\n\r\n## Create, download and install certificates\r\n\r\n1. Go to http://developer.apple.com\r\n * Enter **member center**\r\n * Login\r\n * Go to **Certificates, Identifiers & Profiles**\r\n * Click **Certificates**\r\n 1. Development\r\n + Click **+** to add a new **iOS App Development** certificate\r\n 2. Production\r\n + Click **+** to add a new **Distribution Profile** certificate\r\n * Go to **iOS Provisioning profiles**\r\n + Click **+** to add a new **iOS Distribution_ profile**\r\n2. Download and add to Xcode all the Certificates\r\n\r\n***\r\n\r\n\r\n## Create an App ID\r\n\r\n+ On your browser:\r\n 1. Go to http://developer.apple.com\r\n 2. Click member center\r\n 3. Certificates, identifiers & profiles\r\n 4. Identifiers\r\n 5. iOS App Ids\r\n 6. Add (+) a new one\r\n+ On Xcode:\r\n 1. Create new project with same bundle identifier as the app added before\r\n 2. Go to _Info.plist_\r\n 3. Exchange _Bundle Identifier_ for the one created before\r\n\r\n***\r\n\r\n## Objective-C variable types\r\n\r\n### Basic types\r\n\r\nObjective-C inherits most of the primitive types, syntax, and flow control statements of C, so we have basic types like:\r\n\r\n```objc\r\n\r\nint\r\nfloat\r\ndouble\r\nchar\r\n\r\n```\r\n\r\nBut the most commonly used types in Objective-C are the following ones, mainly due to the fact that this variables can change its memory allocation size when declared according to the architecture where the code is running.\r\n\r\n```objc\r\n\r\nNSInteger\r\nCGFloat\r\nNSNumber\r\nBOOL boolean values, uses YES or NO instead of the usual true or false,\r\nand translates to 1 and 0 respectively\r\n\r\n```\r\nAs an example, here's the NSInteger implementation:\r\n\r\n```objc\r\n#if __LP64__\r\ntypedef long NSInteger;\r\n#else\r\ntypedef int NSInteger;\r\n#endif\r\n```\r\nThis is done automatically, and in most cases when passing a value as an argument to a function or returning a value from a function, this are the types of variables returned, nevertheless you should pay attention if the functions or APIs being called return this types or the others, to avoid problems with truncated values and so on.\r\n\r\n***\r\n\r\n## NSString and NSMutableString\r\n\r\nNSString is one of the most important classes that Objective-C offers, since it provides several powerful methods for creation, manipulation and searching of string contents.\r\n\r\nNSString objects are immutable. If we want to mainly edit a given string we should use **NSMutableString** instead.\r\n\r\n### String Creation\r\n\r\n```objc\r\n// String literal\r\nNSString *myString = @\"This is a string\";\r\n\r\n// String with a custom format\r\nint someInt = 666;\r\nNSString *myOtherString = [NSString stringWithFormat: @\"%@. I can write %d different ones!\",\r\n myString, someInt];\r\n\r\nNSLog(@\"%@\", myOtherString);\r\n\r\nOutput will be \"This is a string. I can write 666 different ones!\"\r\n\r\n```\r\n### String Manipulation\r\n\r\nA list of useful methods for string manipulation:\r\n\r\n#### String length\r\n```objc\r\nint stringLength = [myString length];\r\n```\r\n\r\n#### Compare Strings\r\n```objc\r\nNSString *stringA = @\"A string\";\r\nNSString *stringB = @\"B string\";\r\n\r\nif([stringA isEqualToString: stringB]) {\r\n NSLog(@\"Equal Strings\");\r\n}\r\n\r\nif([stringA hasPrefix: @\"A\"]) {\r\n NSLog(@\"It's A string\");\r\n}\r\n\r\nif([stringB hasSuffix: @\"string\"]) {\r\n NSLog(@\"Well...not that useful here\");\r\n}\r\n```\r\n\r\n#### Combining Strings\r\n```objc\r\nNSString *firstString = @\"NS\";\r\nNSString *secondString = @\"String\";\r\nNSString *thirdString = [firstString stringByAppendingString:secondString];\r\n\r\n// with format\r\nNSString *thirdString = [firstString stringByAppendingFormat:@\"%@ rocks!\", secondString];\r\n```\r\n**NSMutableString** - With NSMutableString we don't need to create a new string to do content transformations\r\n```objc\r\nNSString *firstString = [NSMutableString stringWithString: @\"NS\"];\r\nNSString *secondString = @\"String\";\r\n[firstString appendString: secondString];\r\n```\r\n\r\n#### Replace Characters in a String\r\n```objc\r\nNSString *myString = @\"Hello World\";\r\nNSString *otherString = [myString stringByReplacingOccurrencesOfString:@\"Hello\"\r\n withString:@\"Goodbye\"];\r\n\r\n// otherString is now \"Goodbye World\"\r\n```\r\n\r\n***\r\n\r\n## Actions\r\n\r\nActions enable us to trigger objects to perform tasks.\r\n\r\nCan be created manually or trough the interface builder.\r\n\r\n#### Example\r\n\r\n\r\n***\r\n\r\n```objc\r\n/* ViewController.h */\r\n- (IBAction)actionButton:(id)sender;\r\n```\r\n\r\n```objc\r\n/* ViewController.m */\r\n\r\n- (IBAction)actionButton:(id)sender {\r\n\r\n //DO SOMETHING\r\n}\r\n```\r\n\r\n***\r\n\r\n## Outlets\r\n\r\nOutlets display information triggered by the actions performed on the screen.\r\n\r\nCan be created manually or trough the interface builder or storyboard.\r\n\r\n#### Example\r\n\r\n***\r\n\r\n```objc\r\n/* ViewController.h */\r\n\r\n@property (weak, nonatomic) IBOutlet UILabel *label;\r\n```\r\n***\r\n\r\n## Labels\r\n\r\nLabels are objects used to display text.\r\n\r\n#### Example\r\n\r\n***\r\n\r\n```objc\r\n/* Declaration */\r\n\r\n@property (weak, nonatomic) IBOutlet UILabel *label;\r\n\r\n```\r\n```objc\r\n/* Change label text */\r\n\r\nself.label.text = @\"Hello World\";\r\n```\r\n***\r\n\r\n## TextField\r\n\r\nTextField is an object that allows input from the user (like a search bar for example).\r\n\r\n#### Example\r\n\r\n***\r\n\r\n```objc\r\n/* Declaration */\r\n\r\n@property (weak, nonatomic) IBOutlet UITextField *textField;\r\n\r\n```\r\n```objc\r\n/* Change label text to textField input */\r\n\r\nself.label.text = self.textField.text;\r\n```\r\n***\r\n\r\n## Text Properties\r\n\r\nChange the text properties of objects that display text.\r\n\r\n### Examples\r\n\r\n***\r\n\r\n```objc\r\n/* Change text color */\r\n\r\nself.label.textColor = [UIColor blueColor];\r\n\r\n```\r\n```objc\r\n/* Set text font size */\r\n\r\n[self.label setFont:[UIFont fontWithName:@\"Arial\" size:30]];\r\n\r\n```\r\n```objc\r\n/* Set text shadow */\r\n\r\nself.label.layer.shadowColor = [[UIColor greyColor] CGColor];\r\nself.label.layer.shadowOpacity = 0.7;\r\nself.label.layer.shadowRadius = 2.0f;\r\nself.label.layer.shadowOffset = CGSizeMake(1, 2); // x & y offset\r\n\r\n```\r\n```objc\r\n/* Align Text */\r\n\r\nself.label.textAlignment = NSTextAlignmentLeft;\r\nself.label.textAlignment = NSTextAlignmentRight;\r\nself.label.textAlignment = NSTextAlignmentCenter;\r\n\r\n```\r\n\r\n***\r\n\r\n#### Custom font\r\n\r\n- Download font\r\n- Drag and drop to _Xcode Supporting Files_\r\n- Go to _info.plist_\r\n- Add _Fonts provided by application_\r\n- Add new item with font file name\r\n\r\n```objc\r\n/* On ViewDidLoad, if you want to apply the font right after the view is loaded */\r\n\r\nself.label.font = [UIFont fontWithName:@\"BebasNeue\" size:30];\r\n\r\n```\r\n\r\n***\r\n\r\n## Dismissing the Keyboard\r\n\r\n- Create a _Did End On Exit_ action on a _textField_, for example\r\n- Change _textField_ atribute _Return Key_ to _Done_\r\n\r\n***\r\n\r\n```objc\r\n/* For compatibility with older iOS versions, add this to the action button method*/\r\n\r\n[self resignFirstResponder];\r\n```\r\n***\r\n\r\n## NSTimer\r\n\r\n#### Declaration example\r\n\r\n***\r\n```objc\r\n/* ViewController.h */\r\n\r\n#import <UIKit/UIKit.h>\r\n\r\n@interface ViewController : UIViewController {\r\n\r\n NSTimer *timer;\r\n int timeCounter;\r\n\r\n}\r\n\r\n@property (weak, nonatomic) IBOutlet UILabel *label;\r\n\r\n- (IBAction)startCount:(id)sender;\r\n\r\n@end\r\n\r\n\r\n```\r\n\r\n#### Implementation example\r\n\r\n***\r\n\r\n```objc\r\n/* ViewController.m\r\n\r\n @implementation ViewControler\r\n\r\n [...]\r\n*/\r\n\r\n\r\n// Action for a button that starts a timer\r\n\r\n- (IBAction)startCount:(id)sender {\r\n\r\n\r\n timeCounter = 0;\r\n\r\n self.label.text = [NSString stringWithFormat:@\"%i\", timeCounter];\r\n\r\n\r\n // Timer that refreshes every second by calling the countTime method\r\n\r\n timer = [NSTimer scheduledTimerWithTimeInterval:1.0\r\n target:self\r\n selector:@selector(countTime)\r\n userInfo:nil\r\n repeats:YES];\r\n}\r\n\r\n- (void)countTime {\r\n\r\n timeCounter += 1;\r\n\r\n self.label.text = [NSString stringWithFormat:@\"%i\", timeCounter];\r\n\r\n}\r\n```\r\n","google":"","note":"Don't delete this file! It's used internally to help with page regeneration."}