iOS & Objective-C Diary
Disclaimer
This 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.
Table of contents
- Purchasing a developer license
- Create, download and install certificates
- Create an App ID
- Objective-C variable types
- NSString and NSMutableString
- Actions
- Outlets
- Labels
- TextField
- Dismissing the Keyboard
- NSTimer
Purchasing a developer license
In 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.
- Go to http://developer.apple.com
- Then click Program on the top bar
- Click Enroll
- Sign in or create a new Apple ID
- Purchase the license for iOS Development
Create, download and install certificates
- Go to http://developer.apple.com
- Enter member center
- Login
- Go to Certificates, Identifiers & Profiles
- Click Certificates
- Development
- Click + to add a new iOS App Development certificate
- Production
- Click + to add a new Distribution Profile certificate
- Development
- Go to iOS Provisioning profiles
- Click + to add a new iOS Distribution_ profile
- Download and add to Xcode all the Certificates
Create an App ID
- On your browser:
- Go to http://developer.apple.com
- Click member center
- Certificates, identifiers & profiles
- Identifiers
- iOS App Ids
- Add (+) a new one
- On Xcode:
- Create new project with same bundle identifier as the app added before
- Go to Info.plist
- Exchange Bundle Identifier for the one created before
Objective-C variable types
Basic types
Objective-C inherits most of the primitive types, syntax, and flow control statements of C, so we have basic types like:
int
float
double
char
But 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.
NSInteger
CGFloat
NSNumber
BOOL boolean values, uses YES or NO instead of the usual true or false,
and translates to 1 and 0 respectively
As an example, here's the NSInteger implementation:
#if __LP64__
typedef long NSInteger;
#else
typedef int NSInteger;
#endif
This 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.
NSString and NSMutableString
NSString 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.
NSString objects are immutable. If we want to mainly edit a given string we should use NSMutableString instead.
String Creation
// String literal
NSString *myString = @"This is a string";
// String with a custom format
int someInt = 666;
NSString *myOtherString = [NSString stringWithFormat: @"%@. I can write %d different ones!",
myString, someInt];
NSLog(@"%@", myOtherString);
Output will be "This is a string. I can write 666 different ones!"
String Manipulation
A list of useful methods for string manipulation:
String length
int stringLength = [myString length];
Compare Strings
NSString *stringA = @"A string";
NSString *stringB = @"B string";
if([stringA isEqualToString: stringB]) {
NSLog(@"Equal Strings");
}
if([stringA hasPrefix: @"A"]) {
NSLog(@"It's A string");
}
if([stringB hasSuffix: @"string"]) {
NSLog(@"Well...not that useful here");
}
Combining Strings
NSString *firstString = @"NS";
NSString *secondString = @"String";
NSString *thirdString = [firstString stringByAppendingString:secondString];
// with format
NSString *thirdString = [firstString stringByAppendingFormat:@"%@ rocks!", secondString];
NSMutableString - With NSMutableString we don't need to create a new string to do content transformations
NSString *firstString = [NSMutableString stringWithString: @"NS"];
NSString *secondString = @"String";
[firstString appendString: secondString];
Replace Characters in a String
NSString *myString = @"Hello World";
NSString *otherString = [myString stringByReplacingOccurrencesOfString:@"Hello"
withString:@"Goodbye"];
// otherString is now "Goodbye World"
Actions
Actions enable us to trigger objects to perform tasks.
Can be created manually or trough the interface builder.
Example
/* ViewController.h */
- (IBAction)actionButton:(id)sender;
/* ViewController.m */
- (IBAction)actionButton:(id)sender {
//DO SOMETHING
}
Outlets
Outlets display information triggered by the actions performed on the screen.
Can be created manually or trough the interface builder or storyboard.
Example
/* ViewController.h */
@property (weak, nonatomic) IBOutlet UILabel *label;
Labels
Labels are objects used to display text.
Example
/* Declaration */
@property (weak, nonatomic) IBOutlet UILabel *label;
/* Change label text */
self.label.text = @"Hello World";
TextField
TextField is an object that allows input from the user (like a search bar for example).
Example
/* Declaration */
@property (weak, nonatomic) IBOutlet UITextField *textField;
/* Change label text to textField input */
self.label.text = self.textField.text;
Text Properties
Change the text properties of objects that display text.
Examples
/* Change text color */
self.label.textColor = [UIColor blueColor];
/* Set text font size */
[self.label setFont:[UIFont fontWithName:@"Arial" size:30]];
/* Set text shadow */
self.label.layer.shadowColor = [[UIColor greyColor] CGColor];
self.label.layer.shadowOpacity = 0.7;
self.label.layer.shadowRadius = 2.0f;
self.label.layer.shadowOffset = CGSizeMake(1, 2); // x & y offset
/* Align Text */
self.label.textAlignment = NSTextAlignmentLeft;
self.label.textAlignment = NSTextAlignmentRight;
self.label.textAlignment = NSTextAlignmentCenter;
Custom font
- Download font
- Drag and drop to Xcode Supporting Files
- Go to info.plist
- Add Fonts provided by application
- Add new item with font file name
/* On ViewDidLoad, if you want to apply the font right after the view is loaded */
self.label.font = [UIFont fontWithName:@"BebasNeue" size:30];
Dismissing the Keyboard
- Create a Did End On Exit action on a textField, for example
- Change textField atribute Return Key to Done
/* For compatibility with older iOS versions, add this to the action button method*/
[self resignFirstResponder];
NSTimer
Declaration example
/* ViewController.h */
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
NSTimer *timer;
int timeCounter;
}
@property (weak, nonatomic) IBOutlet UILabel *label;
- (IBAction)startCount:(id)sender;
@end
Implementation example
/* ViewController.m
@implementation ViewControler
[...]
*/
// Action for a button that starts a timer
- (IBAction)startCount:(id)sender {
timeCounter = 0;
self.label.text = [NSString stringWithFormat:@"%i", timeCounter];
// Timer that refreshes every second by calling the countTime method
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(countTime)
userInfo:nil
repeats:YES];
}
- (void)countTime {
timeCounter += 1;
self.label.text = [NSString stringWithFormat:@"%i", timeCounter];
}