-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.m
1293 lines (1114 loc) · 54 KB
/
ViewController.m
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
//
// ViewController.m
// DataStructureTypes
//
// Created by HelloMihai on 9/10/14.
//
#import "ViewController.h"
@interface ViewController ()
@end
/** source http://rypress.com/tutorials/objective-c/data-types.html */
@implementation ViewController
- (void)dataTypes
{
/************************************ C Primitives Integer Types ************************************/
^{ // integer types
BOOL isBool = YES;
NSLog(@"%d", isBool);
NSLog(@"%@", isBool ? @"YES" : @"NO");
char aChar = 'a';
unsigned char anUnsignedChar = 255;
NSLog(@"The letter %c is ASCII number %hhd", aChar, aChar);
NSLog(@"%hhu", anUnsignedChar);
short aShort = -32768;
unsigned short anUnsignedShort = 65535;
NSLog(@"%hd", aShort);
NSLog(@"%hu", anUnsignedShort);
int anInt = -2147483648;
unsigned int anUnsignedInt = 4294967295;
NSLog(@"%d", anInt);
NSLog(@"%u", anUnsignedInt);
long aLong = -9223372036854775808;
unsigned long anUnsignedLong = 18446744073709551615;
NSLog(@"%ld", aLong);
NSLog(@"%lu", anUnsignedLong);
long long aLongLong = -9223372036854775808;
unsigned long long anUnsignedLongLong = 18446744073709551615;
NSLog(@"%lld", aLongLong);
NSLog(@"%llu", anUnsignedLongLong);
};
/************************************ Floating Point Types ************************************/
/** C Primitives Floating Point Types */
^{
// Single precision floating-point
float aFloat = -21.09f;
NSLog(@"%f", aFloat);
NSLog(@"%8.2f", aFloat);
// Double precision floating-point
double aDouble = -21.09;
NSLog(@"%8.2f", aDouble);
NSLog(@"%e", aDouble);
// Extended precision floating-point
long double aLongDouble = -21.09e8L;
NSLog(@"%Lf", aLongDouble);
NSLog(@"%Le", aLongDouble);
int integerResult = 5 / 4;
NSLog(@"Integer division: %d", integerResult); // 1
double doubleResult = 5.0 / 4;
NSLog(@"Floating-point division: %f", doubleResult); // 1.25
};
/************************************ Type Sizes ************************************/
/** C Primitives size of bytes used to represent each TYPE */
^{
NSLog(@"Size of char: %zu", sizeof(char)); // This will always be 1
NSLog(@"Size of short: %zu", sizeof(short));
NSLog(@"Size of int: %zu", sizeof(int));
NSLog(@"Size of long: %zu", sizeof(long));
NSLog(@"Size of long long: %zu", sizeof(long long));
NSLog(@"Size of float: %zu", sizeof(float));
NSLog(@"Size of double: %zu", sizeof(double));
NSLog(@"Size of size_t: %zu", sizeof(size_t));
/** Note that sizeof() can also be used with an array, in which case it returns the number of bytes used by the array. */
NSArray *anArray = @[@"", @"", @""];
size_t numberOfElements = sizeof(anArray)/sizeof(anArray[0]);
};
/************************************ Limit Macros ************************************/
/** min and max what each type can hold */
^{
NSLog(@"Smallest signed char: %d", SCHAR_MIN);
NSLog(@"Largest signed char: %d", SCHAR_MAX);
NSLog(@"Largest unsigned char: %u", UCHAR_MAX);
NSLog(@"Smallest signed short: %d", SHRT_MIN);
NSLog(@"Largest signed short: %d", SHRT_MAX);
NSLog(@"Largest unsigned short: %u", USHRT_MAX);
NSLog(@"Smallest signed int: %d", INT_MIN);
NSLog(@"Largest signed int: %d", INT_MAX);
NSLog(@"Largest unsigned int: %u", UINT_MAX);
NSLog(@"Smallest signed long: %ld", LONG_MIN);
NSLog(@"Largest signed long: %ld", LONG_MAX);
NSLog(@"Largest unsigned long: %lu", ULONG_MAX);
NSLog(@"Smallest signed long long: %lld", LLONG_MIN);
NSLog(@"Largest signed long long: %lld", LLONG_MAX);
NSLog(@"Largest unsigned long long: %llu", ULLONG_MAX);
NSLog(@"Smallest float: %e", FLT_MIN);
NSLog(@"Largest float: %e", FLT_MAX);
NSLog(@"Smallest double: %e", DBL_MIN);
NSLog(@"Largest double: %e", DBL_MAX);
NSLog(@"Largest possible array index: %u", SIZE_MAX);
};
/************************************ id ************************************/
^{
id mysteryObject = @"An NSString object";
NSLog(@"%@", [mysteryObject description]);
mysteryObject = @{@"model": @"Ford", @"year": @1967};
NSLog(@"%@", [mysteryObject description]);
};
/************************************ Class Type ************************************/
^{
Class targetClass = [NSString class];
id mysteryObject = @"An NSString object";
if ([mysteryObject isKindOfClass:targetClass]) {
NSLog(@"Yup! That's an instance of the target class");
}
};
/************************************ NSInteger ************************************/
/************************************ NSNumber ************************************/
^{
NSNumber *aBool = [NSNumber numberWithBool:NO];
NSNumber *aChar = [NSNumber numberWithChar:'z'];
NSNumber *aUChar = [NSNumber numberWithUnsignedChar:255];
NSNumber *aShort = [NSNumber numberWithShort:32767];
NSNumber *aUShort = [NSNumber numberWithUnsignedShort:65535];
NSNumber *annnInt = [NSNumber numberWithInt:2147483647];
NSNumber *aUInt = [NSNumber numberWithUnsignedInt:4294967295];
NSNumber *aLong = [NSNumber numberWithLong:9223372036854775807];
NSNumber *aULong = [NSNumber numberWithUnsignedLong:18446744073709551615];
NSNumber *aFloat = [NSNumber numberWithFloat:26.99f];
NSNumber *aDouble = [NSNumber numberWithDouble:26.99];
NSLog(@"%@", [aBool boolValue] ? @"YES" : @"NO");
NSLog(@"%c", [aChar charValue]);
NSLog(@"%hhu", [aUChar unsignedCharValue]);
NSLog(@"%hi", [aShort shortValue]);
NSLog(@"%hu", [aUShort unsignedShortValue]);
NSLog(@"%i", [annnInt intValue]);
NSLog(@"%u", [aUInt unsignedIntValue]);
NSLog(@"%li", [aLong longValue]);
NSLog(@"%lu", [aULong unsignedLongValue]);
NSLog(@"%f", [aFloat floatValue]);
NSLog(@"%f", [aDouble doubleValue]);
//or %@
NSLog(@"%@", aUChar);
NSLog(@"%@", annnInt);
NSLog(@"%@", aFloat);
float asFloat = [annnInt floatValue];
NSLog(@"%.2f", asFloat);
NSString* asString = [annnInt stringValue];
NSLog(@"%@", asString);
};
^{
// numeric literals
NSNumber* aaBool = @NO;
NSNumber* aaChar = @'z';
NSNumber* aanInt = @2147483647;
NSNumber* aaUInt = @4294967295U;
NSNumber* aaLong = @9223372036854775807L;
NSNumber* aaFloat = @26.99F;
NSNumber* aaDouble = @26.99;
};
^{
// entire operation in @(...)
double x = 24.0;
NSNumber* resultt = @(x * .15);
NSLog(@"%.2f", [resultt doubleValue]);
// comparing
NSNumber* annInt = @27;
NSNumber* sameInt = @27U;
// Pointer comparison (fails)
if (annInt == sameInt) {
NSLog(@"They are the same object");
}
// Value comparison (succeeds)
if ([annInt isEqualToNumber:sameInt]) {
NSLog(@"They are the same value");
}
};
^{
// comparing with enumarators
NSNumber* anInt = @27;
NSNumber* anotherInt = @42;
NSComparisonResult result = [anInt compare:anotherInt];
if (result == NSOrderedAscending) {
NSLog(@"27 < 42");
} else if (result == NSOrderedSame) {
NSLog(@"27 == 42");
} else if (result == NSOrderedDescending) {
NSLog(@"27 > 42");
}
};
/************************************ NSDecimalNumber ************************************/
^{ // declaring
NSDecimalNumber *price;
price = [NSDecimalNumber decimalNumberWithMantissa:1599
exponent:-2
isNegative:NO];
price = [NSDecimalNumber decimalNumberWithString:@"15.99"];
};
^{ // arithmetic
NSDecimalNumber *price1 = [NSDecimalNumber decimalNumberWithString:@"15.99"];
NSDecimalNumber *price2 = [NSDecimalNumber decimalNumberWithString:@"29.99"];
NSDecimalNumber *coupon = [NSDecimalNumber decimalNumberWithString:@"5.00"];
NSDecimalNumber *discount = [NSDecimalNumber decimalNumberWithString:@".90"];
NSDecimalNumber *numProducts = [NSDecimalNumber decimalNumberWithString:@"2.0"];
NSDecimalNumber *subtotal = [price1 decimalNumberByAdding:price2];
NSDecimalNumber *afterCoupon = [subtotal decimalNumberBySubtracting:coupon];
NSDecimalNumber *afterDiscount = [afterCoupon decimalNumberByMultiplyingBy:discount];
NSDecimalNumber *average = [afterDiscount decimalNumberByDividingBy:numProducts];
NSDecimalNumber *averageSquared = [average decimalNumberByRaisingToPower:2];
NSLog(@"Subtotal: %@", subtotal); // 45.98
NSLog(@"After coupon: %@", afterCoupon); // 40.98
NSLog((@"After discount: %@"), afterDiscount); // 36.882
NSLog(@"Average price per product: %@", average); // 18.441
NSLog(@"Average price squared: %@", averageSquared); // 340.070481
};
^{ // rounding
NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler
decimalNumberHandlerWithRoundingMode:NSRoundUp
scale:2
raiseOnExactness:NO
raiseOnOverflow:NO
raiseOnUnderflow:NO
raiseOnDivideByZero:YES];
NSDecimalNumber *subtotal = [NSDecimalNumber decimalNumberWithString:@"40.98"];
NSDecimalNumber *discount = [NSDecimalNumber decimalNumberWithString:@".90"];
NSDecimalNumber *total = [subtotal decimalNumberByMultiplyingBy:discount
withBehavior:roundUp];
NSLog(@"Rounded total: %@", total);
};
// comparing
^{
NSDecimalNumber *discount1 = [NSDecimalNumber decimalNumberWithString:@".85"];
NSDecimalNumber *discount2 = [NSDecimalNumber decimalNumberWithString:@".9"];
NSComparisonResult result = [discount1 compare:discount2];
if (result == NSOrderedAscending) {
NSLog(@"85%% < 90%%");
} else if (result == NSOrderedSame) {
NSLog(@"85%% == 90%%");
} else if (result == NSOrderedDescending) {
NSLog(@"85%% > 90%%");
}
};
/************************************ NSDecimal ************************************/
^{
NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithString:@"15.99"];
NSDecimal asStruct = [price decimalValue];
NSDecimalNumber *asNewObject = [NSDecimalNumber decimalNumberWithDecimal:asStruct];
};
^{ // arithmetic
NSDecimal price1 = [[NSDecimalNumber decimalNumberWithString:@"15.99"] decimalValue];
NSDecimal price2 = [[NSDecimalNumber decimalNumberWithString:@"29.99"] decimalValue];
NSDecimal coupon = [[NSDecimalNumber decimalNumberWithString:@"5.00"] decimalValue];
NSDecimal discount = [[NSDecimalNumber decimalNumberWithString:@".90"] decimalValue];
NSDecimal numProducts = [[NSDecimalNumber decimalNumberWithString:@"2.0"] decimalValue];
NSLocale *locale = [NSLocale currentLocale];
NSDecimal result;
NSDecimalAdd(&result, &price1, &price2, NSRoundUp);
NSLog(@"Subtotal: %@", NSDecimalString(&result, locale));
NSDecimalSubtract(&result, &result, &coupon, NSRoundUp);
NSLog(@"After coupon: %@", NSDecimalString(&result, locale));
NSDecimalMultiply(&result, &result, &discount, NSRoundUp);
NSLog(@"After discount: %@", NSDecimalString(&result, locale));
NSDecimalDivide(&result, &result, &numProducts, NSRoundUp);
NSLog(@"Average price per product: %@", NSDecimalString(&result, locale));
NSDecimalPower(&result, &result, 2, NSRoundUp);
NSLog(@"Average price squared: %@", NSDecimalString(&result, locale));
};
^{ // error checking
NSDecimal a = [[NSDecimalNumber decimalNumberWithString:@"1.0"] decimalValue];
NSDecimal b = [[NSDecimalNumber decimalNumberWithString:@"0.0"] decimalValue];
NSDecimal result;
NSCalculationError success = NSDecimalDivide(&result, &a, &b, NSRoundPlain);
switch (success) {
case NSCalculationNoError:
NSLog(@"Operation successful");
break;
case NSCalculationLossOfPrecision:
NSLog(@"Error: Operation resulted in loss of precision");
break;
case NSCalculationUnderflow:
NSLog(@"Error: Operation resulted in underflow");
break;
case NSCalculationOverflow:
NSLog(@"Error: Operation resulted in overflow");
break;
case NSCalculationDivideByZero:
NSLog(@"Error: Tried to divide by zero");
break;
default:
break;
}
};
^{ // comparing
NSDecimal discount1 = [[NSDecimalNumber decimalNumberWithString:@".85"] decimalValue];
NSDecimal discount2 = [[NSDecimalNumber decimalNumberWithString:@".9"] decimalValue];
NSComparisonResult result = NSDecimalCompare(&discount1, &discount2);
if (result == NSOrderedAscending) {
NSLog(@"85%% < 90%%");
} else if (result == NSOrderedSame) {
NSLog(@"85%% == 90%%");
} else if (result == NSOrderedDescending) {
NSLog(@"85%% > 90%%");
}
};
/************************************ NSString ************************************/
^{ // creating
NSString *make = @"Porsche";
NSString *model = @"911";
int year = 1968;
NSString *message = [NSString stringWithFormat:@"That's a %@ %@ from %d!",
make, model, year];
NSLog(@"%@", message);
};
^{ // unicode supported
NSString *make = @"Côte d'Ivoire";
};
^{ // enumerating
NSString *make = @"Porsche";
for (int i=0; i<[make length]; i++) {
unichar letter = [make characterAtIndex:i];
NSLog(@"%d: %hu", i, letter);
}
};
^{ // comparing
NSString *car = @"Porsche Boxster";
if ([car isEqualToString:@"Porsche Boxster"]) {
NSLog(@"That car is a Porsche Boxster");
}
if ([car hasPrefix:@"Porsche"]) {
NSLog(@"That car is a Porsche of some sort");
}
if ([car hasSuffix:@"Carrera"]) {
// This won't execute
NSLog(@"That car is a Carrera");
}
};
^{ // comparing order
NSString *car = @"Porsche Boxster";
NSString *otherCar = @"Ferrari";
NSComparisonResult result = [car compare:otherCar];
if (result == NSOrderedAscending) {
NSLog(@"The letter 'P' comes before 'F'");
} else if (result == NSOrderedSame) {
NSLog(@"We're comparing the same string");
} else if (result == NSOrderedDescending) {
NSLog(@"The letter 'P' comes after 'F'");
}
};
^{ // combining
NSString *make = @"Ferrari";
NSString *model = @"458 Spider";
NSString *car = [make stringByAppendingString:model];
NSLog(@"%@", car); // Ferrari458 Spider
car = [make stringByAppendingFormat:@" %@", model];
NSLog(@"%@", car); // Ferrari 458 Spider (note the space)
};
^{ // searching
NSString *car = @"Maserati GranCabrio";
NSRange searchResult = [car rangeOfString:@"Cabrio"];
if (searchResult.location == NSNotFound) {
NSLog(@"Search string was not found");
} else {
NSLog(@"'Cabrio' starts at index %lu and is %lu characters long",
(unsigned long)searchResult.location, // 13
(unsigned long)searchResult.length); // 6
}
};
^{ // subdividing
NSString *car = @"Maserati GranTurismo";
NSLog(@"%@", [car substringToIndex:8]); // Maserati
NSLog(@"%@", [car substringFromIndex:9]); // GranTurismo
NSRange range = NSMakeRange(9, 4);
NSLog(@"%@", [car substringWithRange:range]); // Gran
};
^{ // split into array
NSString *models = @"Porsche,Ferrari,Maserati";
NSArray *modelsAsArray = [models componentsSeparatedByString:@","];
NSLog(@"%@", [modelsAsArray objectAtIndex:1]); // Ferrari
};
^{ // replacing
NSString *elise = @"Lotus Elise";
NSRange range = NSMakeRange(6, 5);
NSString *exige = [elise stringByReplacingCharactersInRange:range
withString:@"Exige"];
NSLog(@"%@", exige); // Lotus Exige
NSString *evora = [exige stringByReplacingOccurrencesOfString:@"Exige"
withString:@"Evora"];
NSLog(@"%@", evora); // Lotus Evora
};
^{ // changing case
NSString *car = @"lotUs beSpoKE";
NSLog(@"%@", [car lowercaseString]); // lotus bespoke
NSLog(@"%@", [car uppercaseString]); // LOTUS BESPOKE
NSLog(@"%@", [car capitalizedString]); // Lotus Bespoke
};
^{ // numerical conversions
NSString *year = @"2012";
BOOL asBool = [year boolValue];
int asInt = [year intValue];
NSInteger asInteger = [year integerValue];
long long asLongLong = [year longLongValue];
float asFloat = [year floatValue];
double asDouble = [year doubleValue];
};
/************************************ NSMutableString ************************************/
^{ // creating and updating
NSMutableString *car = [NSMutableString stringWithString:@"Porsche 911"];
[car setString:@"Porsche Boxster"];
};
^{ // expanding
NSMutableString *car = [NSMutableString stringWithCapacity:20];
NSString *model = @"458 Spider";
[car setString:@"Ferrari"];
[car appendString:model];
NSLog(@"%@", car); // Ferrari458 Spider
[car setString:@"Ferrari"];
[car appendFormat:@" %@", model];
NSLog(@"%@", car); // Ferrari 458 Spider
[car setString:@"Ferrari Spider"];
[car insertString:@"458 " atIndex:8];
NSLog(@"%@", car); // Ferrari 458 Spider
};
^{ // replacing deleting substrings
NSMutableString *car = [NSMutableString stringWithCapacity:20];
[car setString:@"Lotus Elise"];
[car replaceCharactersInRange:NSMakeRange(6, 5)
withString:@"Exige"];
NSLog(@"%@", car); // Lotus Exige
[car deleteCharactersInRange:NSMakeRange(5, 6)];
NSLog(@"%@", car); // Lotus
};
^{ // DONT EVER DO THIS
NSString *indices = @"";
for (int i=0; i<1000; i++) {
indices = [indices stringByAppendingFormat:@"%d", i];
}
};
^{ // DO THIS INSTEAD OF ABOVE
NSMutableString *indices = [NSMutableString stringWithCapacity:1];
for (int i=0; i<1000; i++) {
[indices appendFormat:@"%d", i];
}
};
/************************************ NSSet ***********************************
unordered and unique
*/
^{ // creating
NSSet *americanMakes = [NSSet setWithObjects:@"Chrysler", @"Ford",
@"General Motors", nil];
NSLog(@"%@", americanMakes);
// from array
NSArray *japaneseMakes = @[@"Honda", @"Mazda",
@"Mitsubishi", @"Honda"];
NSSet *uniqueMakes = [NSSet setWithArray:japaneseMakes];
NSLog(@"%@", uniqueMakes); // Honda, Mazda, Mitsubishi
};
^{ // enumerating
NSSet *models = [NSSet setWithObjects:@"Civic", @"Accord",
@"Odyssey", @"Pilot", @"Fit", nil];
NSLog(@"The set has %li elements", (unsigned long)[models count]);
for (id item in models) {
NSLog(@"%@", item);
}
};
^{ // block based enumeration
NSSet *models = [NSSet setWithObjects:@"Civic", @"Accord",
@"Odyssey", @"Pilot", @"Fit", nil];
[models enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
NSLog(@"Current item: %@", obj);
if ([obj isEqualToString:@"Fit"]) {
NSLog(@"I was looking for a Honda Fit, and I found it!");
*stop = YES; // Stop enumerating items
}
}];
};
^{ // comparing
NSSet *japaneseMakes = [NSSet setWithObjects:@"Honda", @"Nissan",
@"Mitsubishi", @"Toyota", nil];
NSSet *johnsFavoriteMakes = [NSSet setWithObjects:@"Honda", nil];
NSSet *marysFavoriteMakes = [NSSet setWithObjects:@"Toyota",
@"Alfa Romeo", nil];
if ([johnsFavoriteMakes isEqualToSet:japaneseMakes]) {
NSLog(@"John likes all the Japanese auto makers and no others");
}
if ([johnsFavoriteMakes intersectsSet:japaneseMakes]) {
// You'll see this message
NSLog(@"John likes at least one Japanese auto maker");
}
if ([johnsFavoriteMakes isSubsetOfSet:japaneseMakes]) {
// And this one, too
NSLog(@"All of the auto makers that John likes are Japanese");
}
if ([marysFavoriteMakes isSubsetOfSet:japaneseMakes]) {
NSLog(@"All of the auto makers that Mary likes are Japanese");
}
};
^{ // membership checking
NSSet *selectedMakes = [NSSet setWithObjects:@"Maserati",
@"Porsche", nil];
// BOOL checking
if ([selectedMakes containsObject:@"Maserati"]) {
NSLog(@"The user seems to like expensive cars");
}
// nil checking
NSString *result = [selectedMakes member:@"Maserati"];
if (result != nil) {
NSLog(@"%@ is one of the selected makes", result);
}
};
^{ // filtering
NSSet *toyotaModels = [NSSet setWithObjects:@"Corolla", @"Sienna",
@"Camry", @"Prius",
@"Highlander", @"Sequoia", nil];
NSSet *cModels = [toyotaModels objectsPassingTest:^BOOL(id obj, BOOL *stop) {
if ([obj hasPrefix:@"C"]) {
return YES;
} else {
return NO;
}
}];
NSLog(@"%@", cModels); // Corolla, Camry
};
^{ // combining
NSSet *affordableMakes = [NSSet setWithObjects:@"Ford", @"Honda",
@"Nissan", @"Toyota", nil];
NSSet *fancyMakes = [NSSet setWithObjects:@"Ferrari", @"Maserati",
@"Porsche", nil];
NSSet *allMakes = [affordableMakes setByAddingObjectsFromSet:fancyMakes];
NSLog(@"%@", allMakes);
};
/************************************ NSMutableSet ************************************/
^{ // creating
NSMutableSet *brokenCars = [NSMutableSet setWithObjects:
@"Honda Civic", @"Nissan Versa", nil];
NSMutableSet *repairedCars = [NSMutableSet setWithCapacity:5];
};
^{ // adding removing
NSMutableSet *brokenCars = [NSMutableSet setWithObjects:
@"Honda Civic", @"Nissan Versa", nil];
NSMutableSet *repairedCars = [NSMutableSet setWithCapacity:5];
// "Fix" the Honda Civic
[brokenCars removeObject:@"Honda Civic"];
[repairedCars addObject:@"Honda Civic"];
NSLog(@"Broken cars: %@", brokenCars); // Nissan Versa
NSLog(@"Repaired cars: %@", repairedCars); // Honda Civic
};
^{ // filtering with predicates
NSMutableSet *toyotaModels = [NSMutableSet setWithObjects:@"Corolla", @"Sienna",
@"Camry", @"Prius",
@"Highlander", @"Sequoia", nil];
NSPredicate *startsWithC = [NSPredicate predicateWithBlock:
^BOOL(id evaluatedObject, NSDictionary *bindings) {
if ([evaluatedObject hasPrefix:@"C"]) {
return YES;
} else {
return NO;
}
}];
[toyotaModels filterUsingPredicate:startsWithC];
NSLog(@"%@", toyotaModels); // Corolla, Camry
};
^{ // set theory operations
NSSet *japaneseMakes = [NSSet setWithObjects:@"Honda", @"Nissan",
@"Mitsubishi", @"Toyota", nil];
NSSet *johnsFavoriteMakes = [NSSet setWithObjects:@"Honda", nil];
NSSet *marysFavoriteMakes = [NSSet setWithObjects:@"Toyota",
@"Alfa Romeo", nil];
NSMutableSet *result = [NSMutableSet setWithCapacity:5];
// Union
[result setSet:johnsFavoriteMakes];
[result unionSet:marysFavoriteMakes];
NSLog(@"Either John's or Mary's favorites: %@", result);
// Intersection
[result setSet:johnsFavoriteMakes];
[result intersectSet:japaneseMakes];
NSLog(@"John's favorite Japanese makes: %@", result);
// Relative Complement
[result setSet:japaneseMakes];
[result minusSet:johnsFavoriteMakes];
NSLog(@"Japanese makes that are not John's favorites: %@",
result);
};
^{ // enumaration and changing values DO NOT DO THIS. EVER.
NSMutableSet *makes = [NSMutableSet setWithObjects:@"Ford", @"Honda",
@"Nissan", @"Toyota", nil];
for (NSString *make in makes) {
NSLog(@"%@", make);
if ([make hasPrefix:@"T"]) {
// Throws an NSGenericException:
// "Collection was mutated while being enumerated"
[makes removeObject:@"Toyota"];
}
}
};
^{ // enumeration correctly
NSMutableSet *makes = [NSMutableSet setWithObjects:@"Ford", @"Honda",
@"Nissan", @"Toyota", nil];
NSArray *snapshot = [makes allObjects];
for (NSString *make in snapshot) {
NSLog(@"%@", make);
if ([make hasPrefix:@"T"]) {
[makes removeObject:@"Toyota"];
}
}
NSLog(@"%@", makes);
};
/************************************ NSCountedSet ************************************/
^{ //
NSCountedSet *inventory = [NSCountedSet setWithCapacity:5];
[inventory addObject:@"Honda Accord"];
[inventory addObject:@"Honda Accord"];
[inventory addObject:@"Nissan Altima"];
NSLog(@"There are %li Accords in stock and %li Altima",
(unsigned long)[inventory countForObject:@"Honda Accord"], // 2
(unsigned long)[inventory countForObject:@"Nissan Altima"]); // 1
};
/************************************ NSArray ************************************/
^{ // creating
NSArray *germanMakes = @[@"Mercedes-Benz", @"BMW", @"Porsche",
@"Opel", @"Volkswagen", @"Audi"];
NSArray *ukMakes = [NSArray arrayWithObjects:@"Aston Martin",
@"Lotus", @"Jaguar", @"Bentley", nil];
NSLog(@"First german make: %@", germanMakes[0]);
NSLog(@"First U.K. make: %@", [ukMakes objectAtIndex:0]);
};
^{ // enumerating
NSArray *germanMakes = @[@"Mercedes-Benz", @"BMW", @"Porsche",
@"Opel", @"Volkswagen", @"Audi"];
// With fast-enumeration
for (NSString *item in germanMakes) {
NSLog(@"%@", item);
}
// With a traditional for loop
for (int i=0; i<[germanMakes count]; i++) {
NSLog(@"%d: %@", i, germanMakes[i]);
}
[germanMakes enumerateObjectsUsingBlock:^(id obj,
NSUInteger idx,
BOOL *stop) {
NSLog(@"%ld: %@", (unsigned long)idx, obj);
}];
};
^{ // comparing
NSArray *germanMakes = @[@"Mercedes-Benz", @"BMW", @"Porsche",
@"Opel", @"Volkswagen", @"Audi"];
NSArray *sameGermanMakes = [NSArray arrayWithObjects:@"Mercedes-Benz",
@"BMW", @"Porsche", @"Opel",
@"Volkswagen", @"Audi", nil];
if ([germanMakes isEqualToArray:sameGermanMakes]) {
NSLog(@"Oh good, literal arrays are the same as NSArrays");
}
};
^{ // membership checking
NSArray *germanMakes = @[@"Mercedes-Benz", @"BMW", @"Porsche",
@"Opel", @"Volkswagen", @"Audi"];
// BOOL checking
if ([germanMakes containsObject:@"BMW"]) {
NSLog(@"BMW is a German auto maker");
}
// Index checking
NSUInteger index = [germanMakes indexOfObject:@"BMW"];
if (index == NSNotFound) {
NSLog(@"Well that's not quite right...");
} else {
NSLog(@"BMW is a German auto maker and is at index %ld", (unsigned long)index);
}
};
^{ // sorting
NSArray *germanMakes = @[@"Mercedes-Benz", @"BMW", @"Porsche",
@"Opel", @"Volkswagen", @"Audi"];
NSArray *sortedMakes = [germanMakes sortedArrayUsingComparator:
^NSComparisonResult(id obj1, id obj2) {
if ([obj1 length] < [obj2 length]) {
return NSOrderedAscending;
} else if ([obj1 length] > [obj2 length]) {
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
}];
NSLog(@"%@", sortedMakes);
};
^{ // filtering
NSArray *germanMakes = @[@"Mercedes-Benz", @"BMW", @"Porsche",
@"Opel", @"Volkswagen", @"Audi"];
NSPredicate *beforeL = [NSPredicate predicateWithBlock:
^BOOL(id evaluatedObject, NSDictionary *bindings) {
NSComparisonResult result = [@"L" compare:evaluatedObject];
if (result == NSOrderedDescending) {
return YES;
} else {
return NO;
}
}];
NSArray *makesBeforeL = [germanMakes
filteredArrayUsingPredicate:beforeL];
NSLog(@"%@", makesBeforeL); // BMW, Audi
};
^{ // subdividing
NSArray *germanMakes = @[@"Mercedes-Benz", @"BMW", @"Porsche",
@"Opel", @"Volkswagen", @"Audi"];
NSArray *lastTwo = [germanMakes subarrayWithRange:NSMakeRange(4, 2)];
NSLog(@"%@", lastTwo); // Volkswagen, Audi
};
^{ // combining
NSArray *germanMakes = @[@"Mercedes-Benz", @"BMW", @"Porsche",
@"Opel", @"Volkswagen", @"Audi"];
NSArray *ukMakes = @[@"Aston Martin", @"Lotus", @"Jaguar", @"Bentley"];
NSArray *allMakes = [germanMakes arrayByAddingObjectsFromArray:ukMakes];
NSLog(@"%@", allMakes);
};
^{ // string conversion
NSArray *ukMakes = @[@"Aston Martin", @"Lotus", @"Jaguar", @"Bentley"];
NSLog(@"%@", [ukMakes componentsJoinedByString:@", "]);
};
^{ // reversing order
NSArray *anArray = @[@"Mercedes-Benz", @"BMW", @"Porsche",
@"Opel", @"Volkswagen", @"Audi"];
NSArray* reversedArray = [[anArray reverseObjectEnumerator] allObjects];
};
/************************************ NSMutableArray ************************************/
^{ // creating
NSMutableArray *newList = [NSMutableArray new];
NSMutableArray *brokenCars = [NSMutableArray arrayWithObjects: @"Audi A6", @"BMW Z3", nil];
NSMutableArray *newListA = [@[@"infiniti", @"g37s"] mutableCopy];
};
^{ // adding removing
NSMutableArray *brokenCars = [NSMutableArray arrayWithObjects: @"Audi A6", @"BMW Z3", nil];
[brokenCars addObject:@"BMW F25"];
NSLog(@"%@", brokenCars); // BMW F25 added to end
[brokenCars removeLastObject];
NSLog(@"%@", brokenCars); // BMW F25 removed from end
// Add BMW F25 to front
[brokenCars insertObject:@"BMW F25" atIndex:0];
// Remove BMW F25 from front
[brokenCars removeObjectAtIndex:0];
// Remove Audi Quattro
[brokenCars removeObject:@"Audi Quattro"];
// Change second item to Audi Q5
[brokenCars replaceObjectAtIndex:1 withObject:@"Audi Q5"];
};
^{ // Sorting With Descriptors
NSDictionary *car1 = @{
@"make": @"Volkswagen",
@"model": @"Golf",
@"price": [NSDecimalNumber decimalNumberWithString:@"18750.00"]
};
NSDictionary *car2 = @{
@"make": @"Volkswagen",
@"model": @"Eos",
@"price": [NSDecimalNumber decimalNumberWithString:@"35820.00"]
};
NSDictionary *car3 = @{
@"make": @"Volkswagen",
@"model": @"Jetta A5",
@"price": [NSDecimalNumber decimalNumberWithString:@"16675.00"]
};
NSDictionary *car4 = @{
@"make": @"Volkswagen",
@"model": @"Jetta A4",
@"price": [NSDecimalNumber decimalNumberWithString:@"16675.00"]
};
NSMutableArray *cars = [NSMutableArray arrayWithObjects:
car1, car2, car3, car4, nil];
NSSortDescriptor *priceDescriptor = [NSSortDescriptor
sortDescriptorWithKey:@"price"
ascending:YES
selector:@selector(compare:)];
NSSortDescriptor *modelDescriptor = [NSSortDescriptor
sortDescriptorWithKey:@"model"
ascending:YES
selector:@selector(caseInsensitiveCompare:)];
NSArray *descriptors = @[priceDescriptor, modelDescriptor];
[cars sortUsingDescriptors:descriptors];
NSLog(@"%@", cars); // car4, car3, car1, car2
};
/************************************ NSDictionary ************************************/
^{ // creating
// Literal syntax
NSDictionary *inventory = @{
@"Mercedes-Benz SLK250" : [NSNumber numberWithInt:13],
@"Mercedes-Benz E350" : [NSNumber numberWithInt:22],
@"BMW M3 Coupe" : [NSNumber numberWithInt:19],
@"BMW X6" : [NSNumber numberWithInt:16],
};
// Values and keys as arguments
inventory = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:13], @"Mercedes-Benz SLK250",
[NSNumber numberWithInt:22], @"Mercedes-Benz E350",
[NSNumber numberWithInt:19], @"BMW M3 Coupe",
[NSNumber numberWithInt:16], @"BMW X6", nil];
// Values and keys as arrays
NSArray *models = @[@"Mercedes-Benz SLK250", @"Mercedes-Benz E350",
@"BMW M3 Coupe", @"BMW X6"];
NSArray *stock = @[[NSNumber numberWithInt:13],
[NSNumber numberWithInt:22],
[NSNumber numberWithInt:19],
[NSNumber numberWithInt:16]];
inventory = [NSDictionary dictionaryWithObjects:stock forKeys:models];
NSLog(@"%@", inventory);
};
^{ // accessing values and keys
NSDictionary *inventory = @{
@"Mercedes-Benz SLK250" : [NSNumber numberWithInt:13],
@"Mercedes-Benz E350" : [NSNumber numberWithInt:22],
@"BMW M3 Coupe" : [NSNumber numberWithInt:19],
@"BMW X6" : [NSNumber numberWithInt:16],
};
NSLog(@"There are %@ X6's in stock", inventory[@"BMW X6"]);
NSLog(@"There are %@ E350's in stock",
[inventory objectForKey:@"Mercedes-Benz E350"]);
};
^{ // accessing values and keys reverse lookup
NSDictionary *inventory = @{
@"Mercedes-Benz SLK250" : [NSNumber numberWithInt:0],
@"Mercedes-Benz E350" : [NSNumber numberWithInt:0],
@"BMW M3 Coupe" : [NSNumber numberWithInt:19],
@"BMW X6" : [NSNumber numberWithInt:16],
};
NSArray *outOfStock = [inventory allKeysForObject:
[NSNumber numberWithInt:0]];
NSLog(@"The following cars are currently out of stock: %@",
[outOfStock componentsJoinedByString:@", "]);
// join dictionary with deliminators
NSMutableString *resultString = [NSMutableString string];
for (NSString* key in [inventory allKeys]){
if ([resultString length]>0)
[resultString appendString:@"&"];
[resultString appendFormat:@"%@=%@", key, [inventory objectForKey:key]];
}
};
^{ // enumerating
NSDictionary *inventory = @{
@"Mercedes-Benz SLK250" : [NSNumber numberWithInt:13],
@"Mercedes-Benz E350" : [NSNumber numberWithInt:22],
@"BMW M3 Coupe" : [NSNumber numberWithInt:19],
@"BMW X6" : [NSNumber numberWithInt:16],
};
NSLog(@"We currently have %ld models available", (unsigned long)[inventory count]);
for (id key in inventory) {
NSLog(@"There are %@ %@'s in stock", inventory[key], key);
}
NSLog(@"Models: %@", [inventory allKeys]);
NSLog(@"Stock: %@", [inventory allValues]);
[inventory enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"There are %@ %@'s in stock", obj, key);
}];
};
^{ // comparing
NSDictionary *warehouseInventory = @{
@"Mercedes-Benz SLK250" : [NSNumber numberWithInt:13],
@"Mercedes-Benz E350" : [NSNumber numberWithInt:22],
@"BMW M3 Coupe" : [NSNumber numberWithInt:19],
@"BMW X6" : [NSNumber numberWithInt:16],
};
NSDictionary *showroomInventory = @{
@"Mercedes-Benz SLK250" : [NSNumber numberWithInt:13],
@"Mercedes-Benz E350" : [NSNumber numberWithInt:22],
@"BMW M3 Coupe" : [NSNumber numberWithInt:19],
@"BMW X6" : [NSNumber numberWithInt:16],
};
if ([warehouseInventory isEqualToDictionary:showroomInventory]) {
NSLog(@"Why are we storing so many cars in our showroom?");
}
};
^{ // sorting
NSDictionary *prices = @{
@"Mercedes-Benz SLK250" : [NSDecimalNumber decimalNumberWithString:@"42900.00"],
@"Mercedes-Benz E350" : [NSDecimalNumber decimalNumberWithString:@"51000.00"],
@"BMW M3 Coupe" : [NSDecimalNumber decimalNumberWithString:@"62000.00"],
@"BMW X6" : [NSDecimalNumber decimalNumberWithString:@"55015.00"]