Xcode shows warning with yellow rectangle at the beginning of the line of the code that has problem. You can see the description of the warning by clicking on the rectangle. If the description of the warning is more than a line, you can hover the warning to show the full description of the warning.
In order to force the Xcode compiler to have a warning, we can use #warning message like bellow:
#warning Description of the warning
Errors
Xcode shows compile errors with red signs. The build will fail if application contains any error.
(nonatomic, strong)
nonatomic: Not thread safe. You can read the full description of this attribute here strong: Strong pointer to an object. It means keep the memory for the thing that this property points to, in the heap,as long as I or anyone else has strong pointer to it. Full description could be found here weak: Weak pointer to an object.
#import
In order to include other classes or other frameworks we use #import. We import only .h files. The format for importing classes is as follow:
#import "ViewController.h"
The format for importing a framework is as follow:
#import ""
Why self?
If you define a property and you want to call that property, you need to use self at the beginning of the name of the property. For local or instance variables we don't need to use self. If you are familiar with Java development, self is more like this in Java.
Dot notations and square bracket notation
You may have noticed dot notations in objective-c. They both do the same thing, accessing the methods.Use dot notation only for setters and getters, but for calling a method, use square bracket notation.
self.myTextField.text.length
The equivalent for the above line is as follow:
[[[self myTextField] text] length];
We can use the combination of dot notation and bracket notation like bellow:
[self.myTextField.text length];
MVC
Model-View-Controller
Full explanation of MVC design pattern in objective-c can be found here (Minute 14:18 until 26:00).
It defines a set of methods specifically for setting and accessing the value.
@property (nonatomic, strong) NSNumber *myNumber;
numberWithInt
A wrapper around int to convert it to an object.
int myInt = 2;
self.myNumber = [NSNumber numberWithInt:myInt];
NSNumber has other useful methods for the other primitive types. Methods like: numberWithDouble, numberWithFloat, numberWithBool, numberWithChar, ...
Primitive types
Here are the important primitive types in Objective-C: int, BOOL, float, char, double, short, long.
stringValue
You can convert int type into string by using NSNumber method.
NSString *myStrig = [self.myNumber stringValue];
We have some similar methods like floatValue, intValue, boolValue, shortValue, doubleValue that can convert primitives into each other.
Type id
When we don't know the type of an object we use id as a type. It is useful when we want to assign a variable to a type but we are not sure what kind of object we receive (We will have more examples in the future).
The NSDictionary class declares the programmatic interface to objects that manage immutable associations of keys and values. We can declare and initialize a dictionary with keys and values with this method dictionaryWithObjectsAndKeys
Video Description
NSArray and its subclass NSMutableArray manage ordered collections of objects called arrays. NSArray creates static arrays, and NSMutableArray creates dynamic arrays. You can use arrays when you need an ordered collection of objects. Here is the documentation of this class.
Initialize NSArray
We already know how to define NSArray property and also how to synthesize it. But let's log the value of the array and see what would we get.
Define the property:
@property (nonatomic, strong) NSArray *myArray;
Synthesize it:
@synthesize myMutableArray = _myMutableArray;
Log it like bellow:
NSLog(@" %@", self.myArray);
We expect that the result shows an empty array but The result will be null. Why? Because we have not initialized the array. Before initializing the array, there is no memory allocated for this array. By initializing the array we allocate memory for it then we can send message to that array and get a log for it. The best place to initialize the array property is in it's getter.
Equivalent for Synthesize
As I mentioned in tutorial part 4 When we synthesize properties, in fact we are creating setter and getter for that property. If we want to manually create the setter and getter for the property, we have something like bellow:
Setter
The if statement above means that, if myArray is null, allocate memory for it and then initialize it for me. Now if we log the array, it shows an empty array. Since we don't change anything in setter method we can delete it.
Useful methods of NSArray
arrayWithObjects
You can define array items by using this method like bellow:
Video Description
One of the important frameworks in Objective-C is NSString, there are a lot of useful methods in this frameworks that could save you multiple lines of codes. In this tutorial I will discuss some of useful ones. If you want to see all of the methods you can read the documentation here : NSString Documentation
NSLog
In order to send message to console we use NSLog API.
NSLog(@" %@", @"Hello Console");
%@: reruns the description of the object that is passed after comma (,)
@"": returns whatever string value that is between quotation ""
How to access the value of property
We access the property inside of the instance methods by putting word "self" and the "." and then name of the property. For example for the property like this :
@property (nonatomic, strong) NSString *st1;
If we want to get the value, we put it on the right side of the equation:
= self.st1
If we want to set the value, we put it on the left side of the equation:
self.st1 = //Whatever value
How to put comments in Objective-C
Comments are not compiled in Objective-C. They are just for developers to put brief explanation of their code. If your comment is just one line, you can add // at the beginning of the line and start typing. But if your comment is more than one line it's better to use multiple comment line format which starts with /* and you can end it with */
Useful NSString methods
stringByAppendingString
By using this method you can concatenate two strings together. See example at the end of this tutorial to learn how to use it.
stringByAppendingFormat
By using this method you can give your string(s) a format that you want. See example at the end of this tutorial to learn how to use it.
//
// ViewController.m
// HelloWorld
//
// Created by HuxTek on 1/19/14.
// Copyright (c) 2014 HuxTek. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) NSString *st1;
@property (nonatomic, strong) NSString *st2;
@property (nonatomic, strong) NSString *st3;
@end
@implementation ViewController
@synthesize st1 = _st1;
@synthesize st2 = _st2;
@synthesize st3 = _st3;
//The following method is View life cycle (Don't worry about it now I will dedicate special session for view life cyle. I used it here because I wanted to send message to console
-(void) viewDidLoad {
[super viewDidLoad];
//Log static values
NSLog(@" %@", @"Hello Console");
//Result: Hello Console
//Set a vale for property (We set the value of st1 to be "string one"
self.st1 = @"string one";
//If we want to log the value of the st1, we should write
NSLog(@" %@", self.st1);
//Result: string one
//Get a vlue of propert
//We want to get a value of st1 and set it for st2
//In simple word we want to copy st1 into st2
self.st2 = self.st1;
//If we want to log the value of the st2, we should write
NSLog(@" %@", self.st2);
//Result: string one
//How to comments
//One line comment by adding // at the begging of line
//Compiler will not compile the comments
/*
Multiple comment line
line 2
line 3 ...
*/
//Append (concatinate st1 to st2 by the follwing method)
//Open bracket [ and close bracket ] is for sending message to objects
//We send message to st1 to append st2 and at the same time we set st3 to be the result value of this operation
//Lets change the value of st2 so we can differentiate st1 from st2
self.st2 = @"string two";
self.st3 = [self.st1 stringByAppendingString:self.st2];
NSLog(@" %@", self.st3);
//Result: string onestring two
//String format
//If we want to have format like bellow
//(string one) -> **string two**
//We should use a method called: stringByAppendingFormat
self.st3 = [self.st3 stringByAppendingFormat:@"(%@) -> **%@**", self.st1, self.st2];
NSLog(@" %@", self.st3);
//Result: (null)
// Result is null because we have not initialized st3
// We initialize it with empty string before setting it to the desired format
self.st3 = @"";
self.st3 = [self.st3 stringByAppendingFormat:@"(%@) -> **%@**", self.st1, self.st2];
NSLog(@" %@", self.st3);
//Result: (string one) -> **string two**
}
@end
Foundation Frameworks (Property and variable types)
Video Description
NSObject
NSObject is the root class of most Objective-C class hierarchies. Through NSObject, objects inherit a basic interface to the runtime system and the ability to behave as Objective-C objects (Don't worry if you don't understand the definition, we will talk about it later).
NSString & NSMutableString
In order to define a String type variable or property in Objective-C language we use NSString like bellow:
@property (nonatomic, strong) NSString *myString;
If you want to modify the existing string, instead of NSString type you should use NSMutableString, which is mutable version of the NSString
Look up objects using a key to get a value. In order to define a dictionary with keys and values in Objective-C language we use NSDictionary like this:
Object wrapper around primitive types like int, float, double, BOOL, etc.
It is useful when you want to put these primitive types in an NSArray or NSDictionary. We can define it like bellow:
In terms of memory management it's better to use properties instead of instant variables. It's more efficient and easier to use. It also creates setter and getter by writing one line of code. You declare properties like below:
NSString means the property, is type of string. You need to add * before variable name. At this stage don't worry about the (nonatomic, strong), just happily use it for your properties. Later on I'll explain what are these words good for. In order to make setter and getter for this property you need to synthesize (implement) it. You synthesize it by the following line of code: