1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
instructor: kris hopkins
kris.hopkins@sait.ca
register for university developer program.
access to all of the wwdc videos.
* publish up to 100 hours of video.
Acronym soup:
* OTA (over the air) download.
xcodeproj - This is the xcode project.
Why is there NS in front of log?
Objective C was created by the company NeXT. NeXT first introduced their
object-oriented operating system, called NeXTSTEP, in the late 80s. Many
of the classes you will see in Objective-C still have NS in front of
them as a reference to Next Step.
Why is there a symbol @ in front of the string we are logging?
Objective-C is a strict superset of C. This means you can compile any C
code in an Objective-C project. In Objective-C we never use C Strings,
we always use object oriented strings, called NSStrings. The @ symbol is
a heads up to the compiler that what is coming next is an Objective-C
NSString and not a C string.
## Objective-C Primitive Data Types
There are 5 primitive datatypes in Objective-C:
1. int - integer
2. float - floating point number
3. double - double precision floating point number
4. char - a single character
5. BOOL - YES or NO
Everything else is a Class data type.
## Objective-C Variables
* variables are used to identify a value.
* variables are always strongly typed.
* every variable must be one data type only and must be declared.
{% highlight objective-c %}
int myInt;
myInt = 5;
int myInt = 5;
{% endhighlight %}
### Format Specifiers
* %d - int
* %f - float, double
* %c - char
* %@ - any object e.g NSString
* Note that when outputting the value for a BOOL data type use %d to get
the int value. 0 for NO and 1 for YES
### NSString
All objects use pointser. A pointer is a variable that holds a reference
to an address in memory instead of an actual value. The address in
memory holds the actual value that has been assigned.
{% highlight objc %}
NSString * myString = @"Hello";
NSLog(@"What the what? %@", myString);
{% endhighlight %}
### Arithmetic Operators
* + Add
* - Subtract
* * Multiply
* / divide
* % remainder (modulus)
* = assignment
* += add R to L, then assign to L
* -= subtract R from L, then assign to L
* *= multiply L by R, then assign to L
* /= divide R into L, then assign result to L
* %= L mod B, then assign to L
* ++ add 1
* -- subtract
### Relational Operators
Work same way they do in other C-based languages.
* == equal to
* != not equal to
* < less than
* <= less than or equal to
* > greater than
* >= greater than or equal to
### Logical Operators
All logical operators work exactly the same way they do in other C-based
languages:
* && and
* || or
* ! not
### Custom classes
NSObject is the top level class and parent class of all other classes in
Objective-C.
Objective-C classes contain two files. The first is the interface file,
identified by the .h suffix. This can also be called the "header" file
(h = header). The second is the implementation file, identified by a .m
suffix.
The interface file contains a declaration of the elements of a class
that other classes can see. The implementation file contains all the
actual content for the class.
All of the declarations inside an interface file are contains within the
@interface and @end keywords.
All of the content inside an implementation file is contained within the
@implementation and @end keywords.
#### Interface
{% highlight objc linenos %}
#import <Foundation/Foundation.h>
@interface Person : NSObject
@end
{% endhighlight %}
1. Preprocessor statement. Imports the Foundation framework so we have
access to the build Objective-C classes such as NSObject.
2. Shows that this is an interface for Person.
3. Closes the interface section of the file.
#### Implementation
{% highlight obj linenos %}
#import "Person.h"
@implementation Person
@end
{% endhighlight %}
1. Import our header file so our implementation file can see it.
2. begin implementation section of the file, and states the name of the classes.
3. close the implementation.
@property keyword tells the compiler that we want to create a property.
strong and nonatomic keywords have to do with memory management and
we'll talk about them later.
{% highlight obj %}
Person * newPerson = [Person new];
{% endhighlight %}
Create a new instance of Person. This is like new Person() in other
languages. Instead of "calling a method" we are "sending a message".
Sending the new message to the Person class will work, but you'll rarely
see it this way. It is much more common to see the instantiation of the
object like this:
{% highlight obj %}
Person * newPerson = [[Person alloc] init];
{% endhighlight %}
This breaks the new message into two parts. First we allocate memory for
our object (alloc), then we initialize it by calling the object's
initialization message (init).
Many objective c classes have many different init messages that do
different things and pass in different arguments.
{% highlight obj %}
NSString * newString = [[NSString alloc] initWithFormat:@"%@", @"my text"];
{% endhighlight %}
Here is how you set and get our firstName property:
{% highlight obj %}
[newPerson setFirstName:@"Mo"];
newPerson.firstName = @"Mo";
[newPerson firstName]
newPerson.firstName;
{% endhighlight %}
### Methods (Messages)
{% highlight objc %}
-(void) doSomething
{
}
-(void) doSomething:(NSString *)firstThing
{
}
{% endhighlight %}
* (+) class method
* (-) instance method
* (void) return type
* A colon is required if there is at least one argument.
* The data type must be stated.
* We assign a name to each argument.
If you want to be able to call your method from other classes, we need
to declare it in our interface (header) file:
### Public and Private
Any property declared in the .h file is public. To make it private, put
it in an @interface block above the @implementation block in the .m
file, like this:
{% highlight objc %}
@interface Person()
@property (strong, nonatomic) NSString * myPrivateString;
@end
{% endhighlight %}
Any method declared in the .h file is public. To make a method private,
simple do not declare it in the .h file.
### Strong/Weak References
A *strong* reference to an object is one where the memory is reserved (and
the object stays alive) until we don't need it any more. This is the
default.
A *weak* reference to an object is one where the memory is reserved only
until all other strong pointers stop pointing to it strongly.
We will almost always use a strong reference.
|