-
Notifications
You must be signed in to change notification settings - Fork 21
/
406 Integrating Swift With Objective C [English].srt
executable file
·4364 lines (3427 loc) · 81.3 KB
/
406 Integrating Swift With Objective C [English].srt
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
1
00:00:13,065 --> 00:00:16,102
morning
2
00:00:17,002 --> 00:00:22,048
session 406 integrating swept for the
objective-c
3
00:00:22,048 --> 00:00:26,060
now many of us with demos here at the
conference start by creating either a
4
00:00:26,006 --> 00:00:28,595
brand new project from want to expose
project templates
5
00:00:29,189 --> 00:00:33,980
or a playground and that's great
playgrounds are a great way to explore
6
00:00:33,098 --> 00:00:34,105
this with language
7
00:00:35,005 --> 00:00:38,078
but many have you already have Mac or
Iowa saps
8
00:00:38,078 --> 00:00:41,317
and some areas but years writing
objective-c code
9
00:00:42,019 --> 00:00:45,030
here written a carefully you're the bug
bit
10
00:00:45,003 --> 00:00:48,042
polished it optimize it is just perfect
11
00:00:48,042 --> 00:00:52,061
from and we certainly don't want you to
have to
12
00:00:52,061 --> 00:00:55,118
through any better way or rewrite
anything that you don't want to
13
00:00:56,018 --> 00:01:01,037
also someone you had really a lot of
objective-c code that shouldn't may be
14
00:01:01,037 --> 00:01:02,092
spent a decade writing and maybe it's
15
00:01:02,092 --> 00:01:05,431
not so perfect it's not not as clean and
shiny
16
00:01:06,259 --> 00:01:10,287
so on you we're gonna be using
objective-c code for
17
00:01:10,539 --> 00:01:13,710
a while you might want to rewrite its
but it's not gonna happen this time
18
00:01:13,071 --> 00:01:14,730
around because you're gonna have to ship
19
00:01:15,369 --> 00:01:19,405
et cetera so objective-c is not going
away
20
00:01:19,729 --> 00:01:23,110
boats with an objective-c our first
class citizens
21
00:01:23,011 --> 00:01:27,047
for doing cocoa and Cocoa Touch
development and in fact
22
00:01:27,047 --> 00:01:30,726
we don't want to to spend any time that
you don't have to win when you stop
23
00:01:31,149 --> 00:01:34,227
refining objective-c code we want you to
be able to integrate swift
24
00:01:34,929 --> 00:01:39,640
as you want to and we do think that as
you implement new features
25
00:01:39,064 --> 00:01:42,293
as you finally get around to replacing
crafty
26
00:01:42,869 --> 00:01:46,892
subsystems that you want to use with for
that because if it's better
27
00:01:47,099 --> 00:01:51,121
a type safety its expressiveness
28
00:01:51,319 --> 00:01:54,338
its performance now
29
00:01:54,509 --> 00:01:58,531
many viewers also gained a lot of
knowledge and experience
30
00:01:58,729 --> 00:02:03,330
thing cocoa and Cocoa Touch two years
objective-c development and
31
00:02:03,033 --> 00:02:06,094
that applies a swift as well you use the
same frameworks
32
00:02:06,094 --> 00:02:10,139
the same classes the same API's with
some syntactic differences
33
00:02:11,039 --> 00:02:14,042
in fact the same design patterns such as
delegation
34
00:02:14,069 --> 00:02:18,086
all that knowledge applies and so we
expect that a lot of view
35
00:02:18,086 --> 00:02:21,141
are going to end up with projects that
contain both
36
00:02:22,041 --> 00:02:26,730
swift and objective-c and we worked hard
to make sure that Xcode lets you freely
37
00:02:27,099 --> 00:02:28,610
mix with an objective-c
38
00:02:28,061 --> 00:02:32,114
so this session over the next hour we're
gonna be talking to a bit more
39
00:02:33,014 --> 00:02:37,037
detail than what you've probably seen so
far about exactly how that works in
40
00:02:37,037 --> 00:02:37,103
practice
41
00:02:38,003 --> 00:02:41,021
we're gonna start simple by just
introducing some sweat code into an
42
00:02:41,021 --> 00:02:41,064
exact
43
00:02:41,064 --> 00:02:46,135
existing objective-c ap they will get a
little more interesting we're gonna
44
00:02:47,035 --> 00:02:50,056
start to add some swift code to a model
level framework
45
00:02:50,056 --> 00:02:53,137
which had is then embedded in
objective-c ap thank all that project to
46
00:02:54,037 --> 00:02:54,083
see
47
00:02:54,083 --> 00:02:58,091
will talk about how to start accessing a
pure swift framework
48
00:02:59,063 --> 00:03:03,074
which there'll probably be start showing
up up from an objective-c ap
49
00:03:03,074 --> 00:03:07,152
and then we'll talk as we go along about
how to adopt some other commonwealths
50
00:03:08,052 --> 00:03:09,055
tablished coca patterns
51
00:03:09,082 --> 00:03:13,117
been swept and again Swift is designed
from the ground up to be a great cocoa
52
00:03:14,017 --> 00:03:14,115
Cocoa Touch
53
00:03:15,015 --> 00:03:18,082
development language and so it has
language support
54
00:03:18,082 --> 00:03:23,090
for some other Coco idioms that we got
used to it so I could go on here for
55
00:03:23,009 --> 00:03:26,013
you know two dozen slides and talk in
detail about how this works but
56
00:03:26,094 --> 00:03:29,149
a rather just get a demo because that's
a usually been more clear
57
00:03:30,049 --> 00:03:34,050
alright
58
00:03:34,005 --> 00:03:37,040
so got a project here
59
00:03:37,085 --> 00:03:40,304
that some Sep I was ap
60
00:03:41,069 --> 00:03:44,960
and it has a better framework so it's
decently design here it's gotta
61
00:03:44,096 --> 00:03:47,130
the top layer which is the actual app
with the view and controller layer
62
00:03:48,003 --> 00:03:51,065
and our NBC design there is area
63
00:03:51,092 --> 00:03:54,170
framework all mister kit down here which
contains our model objects and all this
64
00:03:55,007 --> 00:03:56,073
is currently written in objective-c
65
00:03:57,036 --> 00:04:00,087
just get a sense of what this is
66
00:04:00,087 --> 00:04:04,104
let's go to run in the simulator and
gonna go ahead here and bring up the
67
00:04:05,004 --> 00:04:05,067
simulator
68
00:04:05,067 --> 00:04:08,148
we see that yet so typical is taking
application that can go and
69
00:04:09,048 --> 00:04:12,113
have color list here I can check off
items at cetera
70
00:04:13,013 --> 00:04:16,332
edit them when I can't do right now is
to add
71
00:04:16,449 --> 00:04:20,930
a new list this happens to be and I
cloud app so I could do that in my Mac
72
00:04:20,093 --> 00:04:24,105
happen it would show up here but for
this example we're going to use with to
73
00:04:25,005 --> 00:04:26,097
add the ability to add a new list
74
00:04:26,097 --> 00:04:30,226
in pilots facts I'm gonna back here
75
00:04:31,099 --> 00:04:34,134
and %uh the first thing I'm gonna do is
looked out here to the group you
76
00:04:34,449 --> 00:04:34,521
controllers
77
00:04:35,169 --> 00:04:38,760
and here we have some %uh view
controllers already written
78
00:04:38,076 --> 00:04:42,083
and objective-c kind Jake objective-c I
go to File
79
00:04:43,046 --> 00:04:47,005
credit file and in this case I was gonna
create an mp3 file cuz I wanna show you
80
00:04:47,419 --> 00:04:48,650
how to get started from scratch
81
00:04:48,065 --> 00:04:51,070
Xcode has great file templates and can
fill an order code for you
82
00:04:52,015 --> 00:04:55,101
we wanna do it on a basic way here show
how it's going to work
83
00:04:56,001 --> 00:04:59,023
I'm gonna call it new list controller
84
00:04:59,023 --> 00:05:03,100
and I'm gonna make sure that it is I
gonna coen go into the application
85
00:05:04,000 --> 00:05:04,088
target here
86
00:05:04,088 --> 00:05:07,159
we also see that I have mister kit which
is my framework and what are the new
87
00:05:08,059 --> 00:05:10,153
extensions here for showing India
88
00:05:11,053 --> 00:05:14,097
a the status of your so news controller
89
00:05:14,097 --> 00:05:18,109
now as I create this my first with file
90
00:05:19,009 --> 00:05:22,103
in a cure objective-c project expo will
offer to create for me was called the
91
00:05:23,003 --> 00:05:24,051
bridging header
92
00:05:24,051 --> 00:05:27,052
so abridging header we'll talk about
that more detail later
93
00:05:27,061 --> 00:05:30,078
but basically what it is is a way for
you to expose some with your objective
94
00:05:30,078 --> 00:05:30,144
c-
95
00:05:31,044 --> 00:05:34,052
API's to swept
96
00:05:35,024 --> 00:05:40,024
gonna go ahead and create that so I get
that by default
97
00:05:40,024 --> 00:05:44,038
now import foundation actually I'm gonna
be needing some you like it
98
00:05:44,038 --> 00:05:47,055
if you're here so I go ahead and change
that you like it
99
00:05:47,055 --> 00:05:51,127
and I know that I will need to import my
own model framework because we wanna
100
00:05:52,027 --> 00:05:54,080
obviously crater model objects from
within
101
00:05:54,008 --> 00:05:58,082
that you control and I'm just gonna go
ahead here and say
102
00:05:59,054 --> 00:06:02,119
new list controller
103
00:06:03,019 --> 00:06:08,031
and now when I complete we see that
because I said import
104
00:06:08,031 --> 00:06:12,170
you like it there swift balls in the
objective-c framework you like it
105
00:06:12,449 --> 00:06:16,210
and gives me a swift you all those
symbols
106
00:06:16,021 --> 00:06:19,060
something a subclass from your
controller and just leave it like that
107
00:06:19,006 --> 00:06:21,039
for now
108
00:06:21,093 --> 00:06:25,101
so we are just a basic empty class this
with class subclassing in Objective C
109
00:06:26,001 --> 00:06:26,065
Class
110
00:06:26,065 --> 00:06:29,072
that we get from you I from Cocoa Touch
111
00:06:30,035 --> 00:06:33,087
now we could sit here and I got
rectangles and create an API
112
00:06:33,087 --> 00:06:36,151
credit you I for this but fortunately it
happened already have one
113
00:06:37,051 --> 00:06:42,083
I how lucky it isn't wired up though so
we're gonna do that right now
114
00:06:42,083 --> 00:06:46,392
the first thing gonna do here is just
like with objective-c
115
00:06:47,139 --> 00:06:50,770
I need to set what the class above your
controllers
116
00:06:50,077 --> 00:06:54,078
sign up here and now we see that the new
list controller that I just created
117
00:06:54,087 --> 00:06:57,116
is shown here among all the ones
implement an objective-c so when you
118
00:06:58,016 --> 00:07:00,032
access your classes from sweat from
119
00:07:00,032 --> 00:07:03,097
Interface Builder the swift Objective C
Class A show up next to each other you
120
00:07:03,097 --> 00:07:04,098
can work with them
121
00:07:05,007 --> 00:07:10,099
as equals alright so now the next thing
I want to do after setting that
122
00:07:10,099 --> 00:07:13,160
I'm gonna go ahead and create a bar
button item here
123
00:07:14,006 --> 00:07:17,084
gonna drag that up into the toolbar up
here
124
00:07:18,038 --> 00:07:21,066
and I'm gonna change said in a fire to
be
125
00:07:21,066 --> 00:07:24,074
added that also changes the visual
appearance to give it a standard Cocoa
126
00:07:25,046 --> 00:07:25,090
Touch
127
00:07:25,009 --> 00:07:29,022
Add button block all credits segue
128
00:07:30,003 --> 00:07:34,142
it's gonna be a mobile segue this is
after all storyboard
129
00:07:34,169 --> 00:07:38,930
and or change the name that segue to be
new list
130
00:07:38,093 --> 00:07:41,127
now that's gonna the important a bit
later on so that we can access it from
131
00:07:42,027 --> 00:07:44,068
within our cut
132
00:07:44,068 --> 00:07:49,156
okay so now you may have seen and what
are the other demos how we can drag from
133
00:07:50,056 --> 00:07:51,140
swift code and connector objects
134
00:07:52,004 --> 00:07:56,022
inside as I P we can do the same thing
the other direction as well
135
00:07:56,058 --> 00:08:00,129
if I option click here on my swift class
136
00:08:01,029 --> 00:08:06,043
my view controller class implement in
swift now I can drag the other direction
137
00:08:06,043 --> 00:08:10,116
for sale gonna do is I'm gonna go ahead
and dragged from outlets
138
00:08:11,016 --> 00:08:15,585
and and certainly outlet here and I'm
going to call this
139
00:08:15,729 --> 00:08:19,310
name feel and we see that the UN text
field
140
00:08:19,031 --> 00:08:23,056
and a storage is weak because we have a
controller class
141
00:08:23,056 --> 00:08:27,060
two superb you are the view our key owns
the button and the name in the fields
142
00:08:27,006 --> 00:08:27,825
and the buttons
143
00:08:28,419 --> 00:08:31,450
in here and so we just won a weak
reference to it
144
00:08:31,729 --> 00:08:34,760
and in fact for me insert this
145
00:08:35,039 --> 00:08:39,061
Interface Builder knows it is what file
so uses the swift syntax
146
00:08:39,259 --> 00:08:42,275
just as it would have for objective-c
147
00:08:42,419 --> 00:08:47,180
and uses the ID outlet property
148
00:08:47,018 --> 00:08:50,063
for the stored property a sorry the idea
of what attribute for the stored
149
00:08:50,063 --> 00:08:51,202
property here
150
00:08:51,769 --> 00:08:54,806
and this is implicitly week and also
optional
151
00:08:55,139 --> 00:09:00,230
because not all the outlets may be
connected
152
00:09:00,023 --> 00:09:03,111
get myself some space there gonna go
ahead and do the same thing
153
00:09:04,011 --> 00:09:07,780
here for the buttons I'm just gonna
154
00:09:07,879 --> 00:09:11,290
control drag these and just gonna
155
00:09:11,029 --> 00:09:15,030
Phillies and the same way now this is a
sample app
156
00:09:15,003 --> 00:09:18,462
Megan just the space heater this is a
sample app
157
00:09:18,759 --> 00:09:22,660
and so it's not risk able to just have
sex partners and six outlets but
158
00:09:22,066 --> 00:09:26,295
I know that your design better a make
more maintainable code and this
159
00:09:26,889 --> 00:09:31,600
this is for illustrative purposes only I
you might want something I where you can
160
00:09:31,006 --> 00:09:32,615
load colors from a table you
161
00:09:33,209 --> 00:09:37,300
from a popular Stern Show the minute
able to you or something like that
162
00:09:38,119 --> 00:09:41,157
I'm gonna go ahead and finish connecting
these
163
00:09:41,499 --> 00:09:45,543
alright so now we have our outlets to
the UI components
164
00:09:45,939 --> 00:09:50,490
can also create actions in this way if I
choose the cancel button down here and I
165
00:09:50,049 --> 00:09:51,078
drag n
166
00:09:51,519 --> 00:09:55,536
that I can create an action up at the
top here just for as for objective-c
167
00:09:55,689 --> 00:09:58,778
a change the connection type 2 action
I'll take a council
168
00:09:59,579 --> 00:10:02,586
and we can just leave the typeof the
center as any object here we don't
169
00:10:02,649 --> 00:10:03,300
actually
170
00:10:03,003 --> 00:10:07,003
care because we're not going to be
messaging app
171
00:10:07,003 --> 00:10:12,072
what we will do however is to invoke
some Cocoa Touch API
172
00:10:12,099 --> 00:10:16,998
give myself some more room and i cant
say so
173
00:10:17,889 --> 00:10:20,897
dismiss and then code completion gives
me access to
174
00:10:21,689 --> 00:10:25,694
the Cocoa Touch API that we already know
I can complete here
175
00:10:26,189 --> 00:10:29,195
I'm not gonna need hey I wanted to be
animated site but true
176
00:10:29,249 --> 00:10:32,920
I don't need a completion blocks I get
it now now
177
00:10:32,092 --> 00:10:36,681
in objective-c we always have to message
cell if we want to talk to self
178
00:10:37,509 --> 00:10:40,522
Swift knows from context when you are
messaging yourself
179
00:10:40,639 --> 00:10:47,639
so we can actually leave this out make
this a bit more readable
180
00:10:48,959 --> 00:10:55,959
excellence alright so now we will
connect the buttons here as well
181
00:10:56,011 --> 00:10:59,054
and I'm going to be calling this pic
color
182
00:10:59,054 --> 00:11:02,073
now in this case i jus you I button
183
00:11:02,559 --> 00:11:06,480
I'm sorry I may need to make an outlook
an action
184
00:11:06,048 --> 00:11:09,637
archer's you I button down here because
in this case we will actually want to
185
00:11:10,069 --> 00:11:12,113
talk to the button or compare the button
186
00:11:12,509 --> 00:11:16,584
a so we want to have that type and as
you can see
187
00:11:17,259 --> 00:11:22,670
Interface Builder generate the code and
makes the center be a white button
188
00:11:22,067 --> 00:11:25,456
before forget I'm also going to wire up
the other
189
00:11:26,059 --> 00:11:30,102
actions here as well you see here
190
00:11:30,489 --> 00:11:34,505
to connect to this one
191
00:11:34,649 --> 00:11:41,649
for myself will be dispersed there
192
00:11:42,019 --> 00:11:45,670
and now they're all wired up to the same
action for all wired up to the same
193
00:11:45,067 --> 00:11:47,160
action the center will be different
depending on which button we touch
194
00:11:48,006 --> 00:11:54,006
now I wanna show the summer the power of
the swift SELECT statement here
195
00:11:54,006 --> 00:12:00,050
had a switch statement unlike
objective-c rather see based languages
196
00:12:01,004 --> 00:12:05,063
I can actually switch on I a wide
variety of different kinds of patterns
197
00:12:05,063 --> 00:12:07,097
in this case I'm just switching out what
the sundress
198
00:12:07,097 --> 00:12:10,466
but this is a very rich constructs
199
00:12:11,339 --> 00:12:15,970
and this win this with language and I
encourage you to catch one of the later
200
00:12:15,097 --> 00:12:18,193
talks about advance weft to the really
get into the depths of
201
00:12:19,093 --> 00:12:22,118
what you can use this for for right now
202
00:12:23,018 --> 00:12:27,063
to keep it simple I'm just going to use
it to match on a button and what I will
203
00:12:27,063 --> 00:12:28,072
say is
204
00:12:28,072 --> 00:12:32,001
okay now I need to assign something here
so I'm gonna need to have a store
205
00:12:32,649 --> 00:12:33,320
property
206
00:12:33,032 --> 00:12:37,491
credits toward property called selected
label
207
00:12:37,779 --> 00:12:41,980
because we're actually choosing labels
here represented as colors on the screen
208
00:12:41,098 --> 00:12:46,327
and then what I can do here as I can say
this is an AAPL
209
00:12:47,209 --> 00:12:51,410
list label so this again is an
objective-c
210
00:12:51,041 --> 00:12:54,056
type that has been imported
211
00:12:54,056 --> 00:12:59,117
through my import a blister kept up
there I'm gonna start out by saying that
212
00:13:00,017 --> 00:13:01,101
that is an AP list label
213
00:13:02,001 --> 00:13:06,080
stack great that's the default value
other