-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorials-ios.html
1192 lines (930 loc) · 53.7 KB
/
tutorials-ios.html
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>PrograMix</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<style>
body {
padding-top: 70px;
/* Required padding for .navbar-fixed-top. Remove if using .navbar-static-top. Change if height of navigation changes. */
font-size: 15px;
font-style: verdana;
}
li{
color: #33AAFF;
}
</style>
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html"><img src="images/logo-trans-134px.png" style="margin-top: -10px"></a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse " id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<!-- languages nav and dropdown-->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="false" aria-expanded="false">Programming Languages<span class="caret"></span></a>
<!-- languages dropdown menu -->
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<ul>
<li><a class="dropdown-item" href="programming-language-java.html">Java</a></li>
<li><a class="dropdown-item" href="programming-language-python.html">Python</a></li>
<li><a class="dropdown-item" href="programming-language-haskell.html">Haskell</a></li>
<li><a class="dropdown-item" href="programming-language-php.html">PHP</a></li>
<li><a class="dropdown-item" href="programming-language-c++.html">C++</a></li>
<li><a class="dropdown-item" href="programming-language-c_sharp.html">C#</a></li>
<li><a class="dropdown-item" href="programming-language-ios.html">IOS - Objective-C</a></li>
<li><a class="dropdown-item" href="programming-language-SQL.html">SQL</a></li>
<li><a class="dropdown-item" href="programming-language-Perl.html">Perl</a></li>
<li><a class="dropdown-item" href="programming-language-JavaScript.html">JavaScript</a></li>
</ul>
</div>
</li><!--end of languages dropdown-->
<!--tutorials in nav and dropdown-->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="false" aria-expanded="false">Tutorials<span class="caret"></span></a>
<!-- Tutorials dropdown menu -->
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<ul>
<li><a class="dropdown-item" href="tutorials-java.html">Java</a></li>
<li><a class="dropdown-item" href="tutorials-python.html">Python</a></li>
<li><a class="dropdown-item" href="tutorials-haskell.html">Haskell</a></li>
<li><a class="dropdown-item" href="tutorials-php.html">PHP</a></li>
<li><a class="dropdown-item" href="tutorials-c++.html">C++</a></li>
<li><a class="dropdown-item" href="tutorials-c_shar.html">C#</a></li>
<li><a class="dropdown-item" href="tutorials-ios.html">IOS - Objective-C</a></li>
<li><a class="dropdown-item" href="tutorials-sql.html">SQL</a></li>
<li><a class="dropdown-item" href="tutorials-perl.html">Perl</a></li>
<li><a class="dropdown-item" href="tutorials-js.html">JavaScript</a></li>
</ul>
</div>
</li><!-- end of tutorials -->
<!--programming software in navbar with dropdown-->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="false" aria-expanded="false">Programming Softwares<span class="caret"></span></a>
<!-- languages dropdown menu -->
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<ul>
<li><a class="dropdown-item" href="prosoft-nb.html">NetBeans</a></li>
<li><a class="dropdown-item" href="prosoft-jc.html">JCreator</a></li>
<li><a class="dropdown-item" href="prosoft-st.html">Sublime Text</a></li>
<li><a class="dropdown-item" href="prosoft-eclipse.html">Eclipse</a></li>
<li><a class="dropdown-item" href="prosoft-xcode.html">Xcode</a></li>
</ul>
</div>
</li><!-- end of programming softwares -->
<!-- search bar icon w/ dropdown searchbox-->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="false" aria-expanded="false"><img src="images/search-icon.png"></a>
<!-- languages dropdown menu -->
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<form method="get" action="/search" id="search">
<input name="q" type="text" size="40" placeholder="Search..." />
</form>
</div>
</li><!-- end of search bar-->
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<!-- Page Content w/ container-->
<div class="container">
<div class="row">
<div class="col-lg-12 text-left">
<div class="jumbotron jumbotron-fluid">
<div class="container" style="text-align: center">
<h1 class="display-3">IOS</h1>
<p class="lead"> </p>
</div>
</div>
<p class="lead">
<h1>Strings</h1>
<h1>Creating and Formatting the String</h1>
<p style="text-indent: 40px">The most common and the one you’re likely to be using is stringWithFormat:</p>
<div class="alert alert-info" role="alert">
NSString *yourString = @"iOS-Blog";<br>
NSString *yourNewString = [NSString stringWithFormat:@"Check out: %@ for awesome tutorials.];<br>
// yourNewString is now @"Check out: iOS-Blog for awesome tutorials."
</div>
<p style="text-indent: 40px">Common string format specifiers are: <b>%@</b> another object. (this calls the :description verb on the specified object, which must then return an NSString.), <b>%d</b> an integer number, <b>%f, %.4f</b> floating point number (with .n digits after the decimal place).</p>
<p style="text-indent: 40px">You can also get the length of a string which can be obtained using the length method:</p>
<div class="alert alert-info" role="alert">
NSString *yourString = @"iOS-Blog";<br>
int yourLength = [yourString length]; // yourLength is equals to 8
</div>
<h1>Joining an Array of Strings</h1>
<p style="text-indent: 40px">For this, you need to use NSArray’s <b class="c" style="color: royalblue">componentsJoinedByString:</b></p>
<div class="alert alert-info" role="alert">
NSArray *yourWords = [NSArray arrayWithObjects:@"iOS-Blog", @"is", @"just", @"awesome", nil];<br>
NSString *sentence = [yourWords componentsJoinedByString:@" "];<br>
<br>
// *sentence is now: @"iOS-Blog is just awesome"
</div>
<h1>Equals to</h1>
<p style="text-indent: 40px">There may become a time you need to determine if two of your strings are equal, for this we use: <b class="c" style="color: royalblue">isEqualToString</b> and we implement it like so:</p>
<div class="alert alert-info" role="alert">
NSString *yourString = @"iOS-Blog";<br>
<br>
if ([yourString isEqualToString:@"iOS-Blog"]) {<br>
/// Out put the result to the NSLog<br>
NSLog(@"Yes, They are both equal");<br>
}
</div>
<h1>Replacing Characters in a String</h1>
<p style="text-indent: 40px">Many languages offer you the chance to replace information within a string, for example PHP used the <b class="c" style="color: royalblue">str_replace</b> method. However, in Objective-C we use the <b class="c" style="color: royalblue">stringByReplacingOccurrencesOfString</b> method.</p>
<p style="text-indent: 40px">So lets say that you wanted to take out all the references to the word red:</p>
<div class="alert alert-info" role="alert">
NSString *newString = [textField.text stringByReplacingOccurrencesOfString:@"red" withString:@""];
</div>
<h1>Splitting Strings into an Array</h1>
<p style="text-indent: 40px">We use a method called componentsSeparatedByString to achieve this:</p>
<div class="alert alert-info" role="alert">
NSString *yourString = @"This is a test string";<br>
NSArray *yourWords = [myString componentsSeparatedByString:@" "];<br>
<br>
// yourWords is now: [@"This", @"is", @"a", @"test", @"string"]
</div>
<p style="text-indent: 40px">if you need to split on a set of several different characters, use NSString’s componentsSeparatedByCharactersInSet:</p>
<p style="text-indent: 40px">Just like other languages, strings are enclosed and defined by the use of “double quotes”, however in Objective-C an NSString also comes with the prefix of the @ sign, Like so:</p>
<div class="alert alert-info" role="alert">
NSString *yourString = @"This is a string, you like?";
</div>
<h1>Creating and Formatting the String</h1>
<p style="text-indent: 40px">Take a look at the apple documentation on NSString Class Reference to check the number of ways that you can create and return strings. The most common and the one you’re likely to be using is stringWithFormat:</p>
<div class="alert alert-info" role="alert">
NSString *yourString = @"iOS-Blog";<br>
NSString *yourNewString = [NSString stringWithFormat:@"Check out: %@ for awesome tutorials.];<br>
// yourNewString is now @"Check out: iOS-Blog for awesome tutorials."
</div>
<p style="text-indent: 40px">Common string format specifiers are: %@ another object. (this calls the :description verb on the specified object, which must then return an NSString.), %d an integer number, %f, %.4f floating point number (with .n digits after the decimal place).</p>
<p style="text-indent: 40px">You can also get the length of a string which can be obtained using the length method:</p>
<div class="alert alert-info" role="alert">
NSString *yourString = @"iOS-Blog";<br>
int yourLength = [yourString length]; // yourLength is equals to 8 <br>
Joining an Array of Strings
</div>
<p style="text-indent: 40px">For this, you need to use NSArray’s componentsJoinedByString:</p>
<div class="alert alert-info" role="alert">
NSArray *yourWords = [NSArray arrayWithObjects:@"iOS-Blog", @"is", @"just", @"awesome", nil];<br>
NSString *sentence = [yourWords componentsJoinedByString:@" "];<br>
<br>
// *sentence is now: @"iOS-Blog is just awesome"
</div>
<h1>Equals to</h1>
<p style="text-indent: 40px">There may become a time you need to determine if two of your strings are equal, for this we use: isEqualToString and we implement it like so:</p>
<div class="alert alert-info" role="alert">
NSString *yourString = @"iOS-Blog";<br>
<br>
if ([yourString isEqualToString:@"iOS-Blog"]) {<br>
/// Out put the result to the NSLog<br>
NSLog(@"Yes, They are both equal");
}
</div>
<h1>Replacing Characters in a String</h1>
<p style="text-indent: 40px">Many languages offer you the chance to replace information within a string, for example PHP used the str_replace method. However, in Objective-C we use the stringByReplacingOccurrencesOfString method.</p>
<p style="text-indent: 40px">So lets say that you wanted to take out all the references to the word red:</p>
<div class="alert alert-info" role="alert">
NSString *newString = [textField.text stringByReplacingOccurrencesOfString:@"red" withString:@""];
</div>
<h1>Splitting Strings into an Array</h1>
<p style="text-indent: 40px">We use a method called componentsSeparatedByString to achieve this:</p>
<div class="alert alert-info" role="alert">
NSString *yourString = @"This is a test string";<br>
NSArray *yourWords = [myString componentsSeparatedByString:@" "];<br>
<br>
// yourWords is now: [@"This", @"is", @"a", @"test", @"string"]
</div>
<p style="text-indent: 40px">if you need to split on a set of several different characters, use NSString’s componentsSeparatedByCharactersInSet:</p>
<div class="alert alert-info" role="alert">
NSString *yourString = @"Foo-bar/iOS-Blog";<br>
NSArray *yourWords = [myString componentsSeparatedByCharactersInSet:<br>
[NSCharacterSet characterSetWithCharactersInString:@"-/"]<br>
];<br>
<br>
// yourWords is now: [@"Foo", @"bar", @"iOS", @"Blog"]
</div>
<p style="text-indent: 40px">Note however that the separator string <b>can’t</b> be blank. If you need to separate a string into its individual characters, just loop through the length of the string and convert each char into a new string:</p>
<div class="alert alert-info" role="alert">
NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[myString length]];<br>
for (int i=0; i < [myString length]; i++) {<br>
NSString *ichar = [NSString stringWithFormat:@"%c", [myString characterAtIndex:i]];<br>
[characters addObject:ichar];<br>
}
</div>
<h1>Trim Spaces from the End of a String</h1>
<p style="text-indent: 40px">Sometimes you might need to remove white spacing, sometimes you just want to, for this we will use the whitespaceCharacterSet method to remove the white spaces from both ends of the string:</p>
<div class="alert alert-info" role="alert">
NSString *yourString = [whitespaceString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
</div>
<p style="text-indent: 40px">If you want to also remove newlines from the string however, use the whitespaceAndNewlineCharacterSet:</p>
<div class="alert alert-info" role="alert">
NSString *yourString = [whitespaceString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
</div>
<h1>Concatenating and Appending Strings</h1>
<p style="text-indent: 40px">This is quite possibly the most useful thing that you will need to know. Concatenating strings. So you have two strings and you need them to go together. Like firstName and LastName. Then this is how you would do that:</p>
<div class="alert alert-info" role="alert">
NSString *firstName = @"iOS-";<br>
NSString *lastName = @"Blog";<br>
// Concatenate them into a new string<br>
NSString *fullName = [string1 stringByAppendingString:string2];<br>
// fullName is now @"iOS-Blog" <br>
// firstName and lastName are unchanged and can be used again.
</div>
<p style="text-indent: 40px">This is a little different if you are using <b class="c" style="color: royalblue">NSMutableString</b> method, you just simply call the appendString method</p>
<div class="alert alert-info" role="alert">
NSMutableString *string1 = [NSMutableString stringWithString:@"This is"];<br>
NSString *string2 = @" a test.";<br>
[string1 appendString:string2];<br>
NSLog(@"string1: %@", string1); // string1 is now @"This is a test."
</div>
<p style="text-indent: 40px">If you would like to concatenate 3 strings then it’s probably easier to construct a new string out of the three by using stringWithFormat: method</p>
<div class="alert alert-info" role="alert">
NSString *allThreeStrings = [NSString stringWithFormat:@"%@ %@ and boring %@",<br>
firstString, secondString, thirdString];
</div>
<h1>Finding and Replacing Substrings</h1>
<p style="text-indent: 40px">For this we will use the NSString method: rangeOfString to find out whether a substring exists within another string:</p>
<div class="alert alert-info" role="alert">
NSString *theString = @"iOS-Blog.com";<br>
NSRange isCom = [theString rangeOfString:@".com"];
</div>
<p style="text-indent: 40px">NSRange is a struct with two parts:</p>
<div class="alert alert-info" role="alert">
.location<br>
.length
</div>
<p style="text-indent: 40px">If the substring is not found, rangeOfString returns <b>{NSNotFound, 0}</b></p>.
<p style="text-indent: 40px">There is a shorter and easier way to test whether a s string is found:</p>
<div class="alert alert-info" role="alert">
if ( [theString rangeOfString:@".com"].location != NSNotFound ) {<br>
// do something here if the string is found
}
</div>
<h1>Replacing or removing a Substring:</h1>
<div class="alert alert-info" role="alert">
NSString *theString = @"ios-blog.com";<br>
NSRange tldr = [domainName rangeOfString:@".com"];<br>
<br>
if (tldr.location != NSNotFound) {<br>
NSLog(@"range of .com: %d, %d", tldr.location, tldr.length);<br>
domainName = [domainName stringByReplacingCharactersInRange:tldr withString:@""];<br>
NSLog(@"removed .com, domain is now: %@", domainName);<br>
}
</div>
<h1>Comments</h1>
<p style="text-indent: 40px">Comments in Objective-C come directly from C and are similar to what you find in other languages. One line comments are written like this:</p>
<div class="alert alert-info" role="alert">
// This is a single line comment.
</div>
<p style="text-indent: 40px">Everything after the // is ignored by the compiler until the end of the line. They also work after instructions:</p>
<div class="alert alert-info" role="alert">
int counter = 0; // This is a counter.
</div>
<p style="text-indent: 40px">C allows also multiline comments, which some languages don’t have</p>
<div class="alert alert-info" role="alert">
/*<br>
This is a multiline comment <br>
which spans more than one line.<br>
*/
</div>
<h1>Variables and basic types</h1>
<p style="text-indent: 40px">As in many other languages, you need to store your values and object inside variables. In some languages you can just use variables freely when you need them. Moreover you can change the content of the variable to anything you want: an integer, a decimal number, a string or an arbitrary object.</p>
<p style="text-indent: 40px">This does not happen in C. Variables are typed, which means that you have to declare the type of data a variable will contain and you won’t be able to change it later. Variable declarations in C look like this:</p>
<div class="alert alert-info" role="alert">
int variable1 = 7;<br>
float variable2 = 3.14;<br>
char variable3 = 'm';<br>
</div>
<p style="text-indent: 40px">Variable names can contain digits but cannot start with them and cannot include spaces, but can include capital letters (used for <b class="c" style="color: royalblue">camel case</b>) or the underscore character _</p>
<p style="text-indent: 40px">As you might have noticed in the example above, each instruction in C ends with a semicolon, which is required. Many languages do to, but some don’t, so this might be new for you. You will learn fast to end instructions with semicolons.</p>
<p style="text-indent: 40px">Take the habit of always initializing your variables to a specific value, even 0. In some languages variables are always initialized to 0 if no other value is specified. This does not happen in an Objective-C method, where non initialized variables will contain garbage and cause strange behaviours in your programs.</p>
<p style="text-indent: 40px">These are the most common types of C used in Objective-C programs.</p>
<table class="table">
<thead>
<tr>
<th>Type </th>
<th>Description </th>
<th>Examples</th>
</tr>
</thead>
<tbody>
<tr class="table-info">
<td>int </td>
<td>integer numbers, including negatives</td>
<td> 0, 105, -12</td>
</tr>
<tr>
<td>unsigned int</td>
<td>positive integers</td>
<td>0, 27, 315</td>
</tr>
<td>float, double</td>
<td>floating point decimal numbers </td>
<td>1.61, -17.375</td>
</tr>
</tr>
<td>bool</td>
<td>boolean values</td>
<td>true, false</td>
</tr>
</tr>
<td>char</td>
<td>single text character </td>
<td>'a', 'F', '?'</td>
</tr>
</tbody>
</table>
<p style="text-indent: 40px">If you need bigger numbers that the int type can contain, there are the long and the long long types, with the related unsigned types.</p>
<p style="text-indent: 40px">Now, the fact is that when you look at Objective-C code, you rarely find these basic types, which some developers and Apple SDKs still use, while you find others more. Here are some common ones you will find often:</p>
<div class="alert alert-info" role="alert">
NSInteger, for integer numbers<br>
NSUInteger, for positive integers
</div>
<p style="text-indent: 40px">CGFloat, for floating point decimal numbers, used for graphical values like drawing coordinates or sizes of graphical objects</p>
<p style="text-indent: 40px">BOOL, for booleans, which uses YES and NO instead of true and false</p>
<p style="text-indent: 40px">The reason these types where created was to make transition from 32 bits to 64 bits architectures easier. C types map to a specific sizes in memory, while the types above change regarding to the architecture the code is compiled for. Objective-C also has its own <b>BOOL</b> type (<b class="c" style="color: royalblue">the reason for it is a bit more complicated</b>).</p>
<p style="text-indent: 40px">Try to use the latter types when you can and pay attention to which types are used and returend by APIs, or you might end with weird results if values are truncated when switching between the two types.</p>
<p style="text-indent: 40px">C allows also to define other kind of simple types, a practice that is used extensively in Objective-C. We will see these types later.</p>
<h1>Operators</h1>
<p style="text-indent: 40px">Like any other language, C has its own arithmetic operators to work with the numeric values stored in the variables (which are probably the same as the ones you already know).</p>
The arithmetic operators of C are the following:
<table class="table">
<thead>
<tr>
<th>Operator</th>
<th>Description </th>
<th>Syntax</th>
</tr>
</thead>
<tbody>
<tr class="table-info">
<td>+</td>
<td>addition</td>
<td>a + b</td>
</tr>
<tr>
<td>-</td>
<td>subtraction </td>
<td>a - b</td>
</tr>
<tr>
<td>*</td>
<td>multiplication</td>
<td>a * b</td>
</tr>
<tr>
<td>/</td>
<td>division</td>
<td>a / b</td>
</tr>
<tr>
<td>% </td>
<td>module (integer remainder of division)</td>
<td>a % b</td>
</tr>
</tbody>
</table>
<p style="text-indent: 40px">Arithmetic operators follow standard math operator precedence, so multiplication, division and module have precedence over addition and subtraction.</p>
<p style="text-indent: 40px">C has also increment and decrement operators:</p>
<table class="table">
<thead>
<tr>
<th>Operator</th>
<th>Description</th>
<th>Syntax</th>
</tr>
</thead>
<tbody>
<tr class="table-info">
<td>++</td>
<td>increment</td>
<td>a++ or ++a</td>
</tr>
<tr>
<td>--</td>
<td>decrement</td>
<td>a-- or --a</td>
</tr>
</tbody>
</table>
<p style="text-indent: 40px">Increment and decrement operators have precedence over the arithmetic operators. Pay particular attention to these operators. When used in isolation, they are equivalent:</p>
<div class="alert alert-info" role="alert">
someVariable++;
</div>
<p style="text-indent: 40px">produces the same result as
<div class="alert alert-info" role="alert">
++someVariable;
</div>
<p style="text-indent: 40px"><p style="text-indent: 40px">The difference comes out when you use them inside another expression. When put before the variable, they increment or decrement the value before it is used. When put after, they increments or decrement the value after using it.</p>
<h1>Trim Spaces from the End of a String</h1>
<p style="text-indent: 40px"><p style="text-indent: 40px">Sometimes you might need to remove white spacing, sometimes you just want to, for this we will use the whitespaceCharacterSet method to remove the white spaces from both ends of the string:</p>
<div class="alert alert-info" role="alert">
NSString *yourString = [whitespaceString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
</div>
<p style="text-indent: 40px"><p style="text-indent: 40px">If you want to also remove newlines from the string however, use the whitespaceAndNewlineCharacterSet:</p>
<div class="alert alert-info" role="alert">
NSString *yourString = [whitespaceString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
</div>
<h1>Concatenating and Appending Strings</h1>
<p style="text-indent: 40px">This is quite possibly the most useful thing that you will need to know. Concatenating strings. So you have two strings and you need them to go together. Like firstName and LastName. Then this is how you would do that:</p>
<div class="alert alert-info" role="alert">
NSString *firstName = @"iOS-";<br>
NSString *lastName = @"Blog";<br>
// Concatenate them into a new string<br>
NSString *fullName = [string1 stringByAppendingString:string2];<br>
// fullName is now @"iOS-Blog" <br>
// firstName and lastName are unchanged and can be used again.
</div>
<p style="text-indent: 40px">This is a little different if you are using NSMutableString method, you just simply call the appendString method</p>
<div class="alert alert-info" role="alert">
NSMutableString *string1 = [NSMutableString stringWithString:@"This is"];<br>
NSString *string2 = @" a test.";<br>
[string1 appendString:string2];<br>
NSLog(@"string1: %@", string1); // string1 is now @"This is a test."
</div>
<p style="text-indent: 40px">If you would like to concatenate 3 strings then it’s probably easier to construct a new string out of the three by using stringWithFormat: method</p>
<div class="alert alert-info" role="alert">
NSString *allThreeStrings = [NSString stringWithFormat:@"%@ %@ and boring %@",<br>
firstString, secondString, thirdString];
</div>
<h1>Finding and Replacing Substrings</h1>
<p style="text-indent: 40px">For this we will use the NSString method: rangeOfString to find out whether a substring exists within another string:</p>
<div class="alert alert-info" role="alert">
NSString *theString = @"iOS-Blog.com";<br>
NSRange isCom = [theString rangeOfString:@".com"];
</div>
<p style="text-indent: 40px">NSRange is a struct with two parts:</p>
<div class="alert alert-info" role="alert">
.location<br>
.length
</div>
<p style="text-indent: 40px">If the substring is not found, rangeOfString returns {NSNotFound, 0}.</p>
<p style="text-indent: 40px">There is a shorter and easier way to test whether a s string is found:</p>
<div class="alert alert-info" role="alert">
if ( [theString rangeOfString:@".com"].location != NSNotFound ) {<br>
// do something here if the string is found<br>
}
</div>
<h1>Replacing or removing a Substring:</h1>
<div class="alert alert-info" role="alert">
NSString *theString = @"ios-blog.com";<br>
NSRange tldr = [domainName rangeOfString:@".com"];<br>
<br>
if (tldr.location != NSNotFound) {<br>
NSLog(@"range of .com: %d, %d", tldr.location, tldr.length);<br>
domainName = [domainName stringByReplacingCharactersInRange:tldr withString:@""];<br>
NSLog(@"removed .com, domain is now: %@", domainName);<br>
}
</div>
<h1>Comments</h1>
<p style="text-indent: 40px">Comments in Objective-C come directly from C and are similar to what you find in other languages. One line comments are written like this:</p>
<div class="alert alert-info" role="alert">
// This is a single line comment.
</div>
<p style="text-indent: 40px">Everything after the // is ignored by the compiler until the end of the line. They also work after instructions:</p>
<div class="alert alert-info" role="alert">
int counter = 0; // This is a counter.
</div>
<p style="text-indent: 40px">C allows also multiline comments, which some languages don’t have</p>
<div class="alert alert-info" role="alert">
/*<br>
This is a multiline comment <br>
which spans more than one line.<br>
*/
</div>
<h1>Variables and basic types</h1>
<p style="text-indent: 40px">As in many other languages, you need to store your values and object inside variables. In some languages you can just use variables freely when you need them. Moreover you can change the content of the variable to anything you want: an integer, a decimal number, a string or an arbitrary object.</p>
<p style="text-indent: 40px">This does not happen in C. Variables are typed, which means that you have to declare the type of data a variable will contain and you won’t be able to change it later. Variable declarations in C look like this:</p>
<div class="alert alert-info" role="alert">
int variable1 = 7;<br>
float variable2 = 3.14;<br>
char variable3 = 'm';
</div>
<p style="text-indent: 40px">Variable names can contain digits but cannot start with them and cannot include spaces, but can include capital letters (used for camel case) or the underscore character _</p>
<p style="text-indent: 40px">As you might have noticed in the example above, each instruction in C ends with a semicolon, which is required. Many languages do to, but some don’t, so this might be new for you. You will learn fast to end instructions with semicolons.</p>
<p style="text-indent: 40px">Take the habit of always initializing your variables to a specific value, even 0. In some languages variables are always initialized to 0 if no other value is specified. This does not happen in an Objective-C method, where non initialized variables will contain garbage and cause strange behaviours in your programs.</p>
<p style="text-indent: 40px">These are the most common types of C used in Objective-C programs.</p><br>
<br>
<table class="table">
<thead>
<tr>
<th>Type</th>
<th>Description</th>
<th>Examples</th>
</tr>
</thead>
<tbody>
<tr>
<td>int</td>
<td>integer numbers, including negatives</td>
<td>0, 105, -12</td>
</tr>
<tr>
<td>unsigned int</td>
<td>positive integers</td>
<td> 0, 27, 315</td>
</tr>
<tr>
<td>float, double</td>
<td>floating point decimal numbers</td>
<td>1.61, -17.375</td>
</tr>
<tr>
<td>bool</td>
<td>boolean values</td>
<td> true, false</td>
</tr>
<tr>
<td>char</td>
<td>single text character</td>
<td>'a', 'F', '?'</td>
</tr>
</tbody>
</table>
<p style="text-indent: 40px">If you need bigger numbers that the int type can contain, there are the long and the long long types, with the related unsigned types.</p>
<p style="text-indent: 40px">Now, the fact is that when you look at Objective-C code, you rarely find these basic types, which some developers and Apple SDKs still use, while you find others more. Here are some common ones you will find often:</p>
<div class="alert alert-info" role="alert">
NSInteger, for integer numbers<br>
NSUInteger, for positive integers<br>
CGFloat, for floating point decimal numbers, used for graphical values like drawing coordinates or sizes of graphical objects<br>
BOOL, for booleans, which uses YES and NO instead of true and false
</div>
<p style="text-indent: 40px">The reason these types where created was to make transition from 32 bits to 64 bits architectures easier. C types map to a specific sizes in memory, while the types above change regarding to the architecture the code is compiled for. Objective-C also has its own BOOL type (the reason for it is a bit more complicated).</p>
<p style="text-indent: 40px">Try to use the latter types when you can and pay attention to which types are used and returend by APIs, or you might end with weird results if values are truncated when switching between the two types.</p>
<p style="text-indent: 40px">C allows also to define other kind of simple types, a practice that is used extensively in Objective-C. We will see these types later.</p>
<h1>Operators</h1>
<p style="text-indent: 40px">Like any other language, C has its own arithmetic operators to work with the numeric values stored in the variables (which are probably the same as the ones you already know).</p>
The arithmetic operators of C are the following:<br>
<br>
<table class="table">
<thead>
<tr>
<th>Operator</th>
<th>Description </th>
<th>Syntax</th>
</tr>
</thead>
<tbody>
<tr>
<td>+</td>
<td>addition</td>
<td>a + b</td>
</tr>
<tr>
<td>-</td>
<td>subtraction</td>
<td>a - b</td>
</tr>
<tr>
<td>*</td>
<td>multiplication</td>
<td> a * b</td>
</tr>
<tr>
<td>/</td>
<td>division</td>
<td>a / b</td>
</tr>
<tr>
<td>%</td>
<td>module (integer remainder of division)</td>
<td>a % b</td>
</tr>
</tbody>
</table>
<p style="text-indent: 40px">Arithmetic operators follow standard math operator precedence, so multiplication, division and module have precedence over addition and subtraction.</p>
<p style="text-indent: 40px">C has also increment and decrement operators:</p><br>
<br>
<table class="table">
<thead>
<tr>
<th>Operator</th>
<th>Description</th>
<th>Syntax</th>
</tr>
</thead>
<tbody>
<tr>
<td>++</td>
<td>increment</td>
<td>a++ or ++a</td>
</tr>
<tr>
<td>--</td>
<td>decrement</td>
<td>a-- or --a</td>
</tr>
</tbody>
</table>
<p style="text-indent: 40px">Increment and decrement operators have precedence over the arithmetic operators. Pay particular attention to these operators. When used in isolation, they are equivalent:</p>
<div class="alert alert-info" role="alert">
someVariable++;
</div>
<p style="text-indent: 40px">produces the same result as</p>
<div class="alert alert-info" role="alert">
++someVariable;
</div>
<p style="text-indent: 40px">The difference comes out when you use them inside another expression. When put before the variable, they increment or decrement the value before it is used. When put after, they increments or decrement the value after using it.</p>
<p style="text-indent: 40px">This example explains how this works:</p>
<div class="alert alert-info" role="alert">
int x;<br>
int y;<br>
<br>
// Increment operators<br>
x = 1;<br>
y = ++x; // x is now 2, y is also 2<br>
y = x++; // x is now 3, y is 2<br>
<br>
// Decrement operators<br>
x = 3;<br>
y = x--; // x is now 2, y is 3<br>
y = --x; // x is now 1, y is also 1
</div>
<p style="text-indent: 40px">We have already seen the assignment operator <b>=</b> which might be different in the languages you are used to. That is not the only assignment operator. The arithmetic operators (with the exception of the modulo) attach to the assignment and produce another four assignment operators, which are used as a shorthand for incrementing, decrementing, multiplying and dividing the content of a variable:</p><br>
<br>
<table class="table">
<thead>
<tr>
<th>Operator</th>
<th>Example</th>
<th>Shorthand for</th>
</tr>
</thead>
<tbody>
<tr>
<td>+=</td>
<td>a += 10;</td>
<td>a = a + 10;</td>
</tr>
<tr>
<td>-=</td>
<td>a -= 7;</td>
<td>a = a - 7;</td>
</tr>
<tr>
<td>*=</td>
<td>a *= 3;</td>
<td>a = a * 3;</td>
</tr>
<tr>
<td>/=</td>
<td>a /= 5;</td>
<td>a = a / 5;</td>
</tr>
</tbody>
</table>
<p style="text-indent: 40px">Unlike in other languages, in Objective-C there is no operator overloading. This means that these operators (and the other operators we will see later) only work with basic types and they do not work with objects (to concatenate strings, for example).</p>
<h1>Object variables</h1>
<p style="text-indent: 40px">Let’s have a first look at objects. Objective-C is an object oriented language after all, so we need variables to contain objects. The generic type for an object variable is <b>id</b></p>
<div class="alert alert-info" role="alert">
id myObject = nil;
</div>
<p style="text-indent: 40px">The <b>id</b> type means that the variable can contain any type of object. The <b>nil</b> value is equivalent to what is called <b>null</b> in some other languages (and in C) and means the variable is empty and does not contain any object. Objective-C is a dynamically typed language, so we can declare variables of type <b>id</b> and use them for any object. We can then call any method we want on that object, regardless of the fact that that method might exist or not. This might be useful in some cases, but the majority of the time we want the compiler to check for our mistakes as soon as possible and give variables a defined type.</p>
<p style="text-indent: 40px">Object variables with a specific type are declared in a slightly different way:</p>
<div class="alert alert-info" role="alert">
NSString *aString = nil;
</div>
<p style="text-indent: 40px">Now what does that <b>*</b> mean? Well, strictly speaking that means that this variable is a pointer, a thing that makes many programmers recoil in horror. Pointers are one of those things coming from C you don’t want to deal with. Many modern languages don’t have pointers, so this might be a concept you are not familiar with. Although in the future, when you will be more familiar with the language, it might be useful to know what a pointer is, this is something you don’t have to worry about for now. You can write Objective-C without knowing this concept yet.</p>
<p style="text-indent: 40px">When programming I rarely think in terms of pointers. Just remember this simple rule: you need a <b>*</b> when you declare an object variable and you don’t when you use it.</p>
<p style="text-indent: 40px">A note on prefixes and namespaces</p>
<p style="text-indent: 40px">As you probably noticed, all types in Objective-C have some prefix at the beginning of the name (<b>NSString, CGFloat</b>, etc). The reason is that Objective-C does not have namespaces. To avoid name collisions (different objects ending up having the same name) Apple decided to prepend a two letters prefix to its types and classes. The two letter are generally coming from the framework declaring the type: UIKit classes and types have a UI prefix (<b>UIView, UILabel</b>, etc.), Core Graphics uses the CG prefix (<b>CGFloat, CGRect</b>, etc.) and the Foundation and AppKit frameworks (the latter present only on the Mac) use NS (<b>NSString, NSArray</b>, etc.). Why NS? Because these libraries were first developed at NeXT for the NeXTSTEP operating system.</p>
<p style="text-indent: 40px">It is a good practice to you add a prefix to your classes and types too. Even if you are not developing a framework but a normal app (which is often the case), just use letters from the app name to be sure to avoid collisions to code you might add later from a different source (like some open source code you might use in your project). There is no strict check from the compiler, so you can omit it, but it’s better not to.</p>
<h1>Branching and decisions</h1>
<p style="text-indent: 40px">Let’s now see how flow control works in Objective-C. Flow control is another part that the language inherits directly from C, so the structures are the same. Decisions and branching happen, in Objective-C, through three branching statements.</p>
<b>If-else statement</b>
<p style="text-indent: 40px">The main statement to perform decisions in Objective-C is the <b>if-else</b> statement:</p>
<div class="alert alert-info" role="alert">
if (expression) {<br>
if-body<br>
} else {<br>
else-body<br>
}
</div>
<p style="text-indent: 40px">The <b>if-body</b> is executed only if the expression to <b>YES, true</b> or any non zero value. Otherwise the <b>else-body</b> is executed. The <b>else</b> clause can be omitted like in many other languages. In the case of just one instruction in the <b>if-body</b> or <b>else-body</b>, the respective curly braces can be omitted too. If you come from a language that has no curly braces, there are <b class="c" style="color: royalblue">many different styles</b> that state where to place them.</p>
<p style="text-indent: 40px">I personally use the K&R style now, that in my experience seems to be quite widespread in the Objective-C community, but I’ve moved through many different styles during my programming career. The only solid advice I can give you regarding this topic is to be consistent: pick one style and always use it everywhere.</p>
<p style="text-indent: 40px">If you want to use more than one condition, the if-else statement allows multiple branches. This is usually the case in many languages, although it’s not possible in a few.</p>
<div class="alert alert-info" role="alert">
if (expression1) {<br>
if-body<br>
} else if (expression2) {<br>
else-if-body<br>
} else {<br>
else-body<br>
}
</div>
<p style="text-indent: 40px">The <b>else if</b> clause can be repeated as many times as needed.</p>
<h1>Switch statement</h1>
<p style="text-indent: 40px">When you have multiple choices you can use, as we saw, the if-else statement with as many branches as needed. If the expression evaluates to an integer, though, C offers another branching facility, the switch statement:</p>
<div class="alert alert-info" role="alert">
switch (expression) {<br>
case value1:<br>
case-body-1<br>
break;<br>
case value2:<br>
case-body-2<br>
break;<br>
...<br>
default:<br>
default-body<br>
break;<br>
}
</div>
<p style="text-indent: 40px">The values for the different cases must be constant integers, which means that you can either write a number or a constant in them, but not a variable.</p>
<p style="text-indent: 40px">The <b>break</b> statements are actually used to exit immediately from the switch statement. They are not mandatory, but pay attention not to forget them, otherwise the execution will continue into the next <b>case</b>.</p>
<p style="text-indent: 40px">The <b>default</b> statement is optional, and is executed when the value of the expression does not match any case. The <b>break</b> in the <b>default</b> statement is not mandatory either and not necessary unless you put another case after the <b>default</b>, which I strongly advise against, since it would be a poor coding practice.</p>
<h1>Ternary operator</h1>
<p style="text-indent: 40px">Another branching facility that Objective-C inherits from C is the ternary operator, also known as conditional operator. This is an operator that in some languages might not be available, and it’s a shorthand of the <b>if-else</b> statement and a <b>return</b> statement (which we will see later, but you are probably already familiar with). The ternary operator takes this form:</p>
<div class="alert alert-info" role="alert">
condition ? expression1 : expression2;
</div>
<p style="text-indent: 40px">If the condition evaluates to <b>YES, true</b>, or any non zero value, <b>expression1</b> is executed and it’s value is returned, otherwise the same happens for <b>expression2</b>. Thus, it is equivalent to the following:</p>
<div class="alert alert-info" role="alert">
if (condition)<br>
return expression1;<br>
else<br>
return expression2;
</div>
<p style="text-indent: 40px">The ternary operator has the advantage over the if-else statement that it can be written inside another expression, like an assignment. Thus this is possible and sometimes used:</p>
<div class="alert alert-info" role="alert">
a = condition ? expression1 : expression2;
</div>
<h1>Relational and logical operators</h1>
<p style="text-indent: 40px">To write the conditions for the branching statement and the loops we will see in a short while, C and Objective-C offer the following relational operators:</p><br>
<br>
<table class="table">
<thead>
<tr>
<th>Operator</th>
<th>Description</th>
<th>Syntax</th>
</tr>
</thead>
<tbody>
<tr>
<td>==</td>