This repository was archived by the owner on Jan 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.xml
1208 lines (904 loc) · 91.6 KB
/
atom.xml
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
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
<title type="text">De Re mulle-objc</title>
<generator uri="https://github.com/jekyll/jekyll">Jekyll</generator>
<link rel="self" type="application/atom+xml" href="https://www.mulle-kybernetik.com/atom.xml" />
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com" />
<updated>2025-01-26T22:37:34+01:00</updated>
<id>https://www.mulle-kybernetik.com/</id>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com/</uri>
</author>
<entry>
<title type="html"><![CDATA[Tooling: Code reduction]]></title>
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com/reduction.html" />
<id>https://www.mulle-kybernetik.com/reduction.html</id>
<published>2024-06-29T00:00:00+02:00</published>
<updated>2024-06-29T00:00:00+02:00</updated>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com</uri>
</author>
<content type="html"><h2 id="reduction">Reduction</h2>
<p>With the coverage information gained in the previous chapter, we can now
extract the required <code class="language-plaintext highlighter-rouge">.o</code> files from the static libraries and create a
custom static library, that contains only the necessary files.</p>
<h2 id="todo">TODO</h2>
<p>Explain how this is done with the cmake <code class="language-plaintext highlighter-rouge">OBJC_COVERAGE_OPTIMIZED_LIBS</code>.</p>
<blockquote>
<p>I have this already written somewhere…</p>
</blockquote>
</content>
</entry>
<entry>
<title type="html"><![CDATA[Tooling: Coverage information]]></title>
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com/coverage.html" />
<id>https://www.mulle-kybernetik.com/coverage.html</id>
<published>2024-06-24T00:00:00+02:00</published>
<updated>2024-06-24T00:00:00+02:00</updated>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com</uri>
</author>
<content type="html"><h2 id="coverage">Coverage</h2>
<p>You can create Objective-C coverage information for any mulle-objc program.</p>
<p>Coverage files will be are generated when you run the program with
with the environment variables <code class="language-plaintext highlighter-rouge">MULLE_OBJC_PEDANTIC_EXIT</code> and
<code class="language-plaintext highlighter-rouge">MULLE_OBJC_COVERAGE</code> set to YES:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">MULLE_OBJC_PEDANTIC_EXIT</span><span class="o">=</span>YES <span class="nv">MULLE_OBJC_COVERAGE</span><span class="o">=</span>YES myexe
</code></pre></div></div>
<p>This will produce two coverage files <code class="language-plaintext highlighter-rouge">class-coverage.csv</code> and
<code class="language-plaintext highlighter-rouge">method-coverage.csv</code>.</p>
<div class="alert alert-info" role="alert"><i class="fa fa-info-circle"></i> <b>Note:</b> Your executable needs to properly terminate, to generate
coverage information. If that isn’t possible, call
<code class="language-plaintext highlighter-rouge">mulle_objc_csvdump_methodcoverage()</code> and <code class="language-plaintext highlighter-rouge">mulle_objc_csvdump_classcoverage()</code>
yourself, when you know the runtime system is quiescent.</div>
<h2 id="extending-coverage-files">Extending coverage files</h2>
<p>A second “coverage” run will append to previously existing coverage files.
But you can also concatenate coverage files from various source with <code class="language-plaintext highlighter-rouge">cat</code>.</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">cat </span>coverage<span class="k">*</span>.txt | <span class="nb">sort</span> <span class="nt">-u</span> <span class="o">&gt;</span> class-coverage.csv
</code></pre></div></div>
</content>
</entry>
<entry>
<title type="html"><![CDATA[Tooling: mulle-objc Tools]]></title>
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com/tools.html" />
<id>https://www.mulle-kybernetik.com/tools.html</id>
<published>2024-06-20T00:00:00+02:00</published>
<updated>2024-06-20T00:00:00+02:00</updated>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com</uri>
</author>
<content type="html"><h2 id="mulle-objc-listmulle-objc-lista">mulle-objc-list/mulle-objc-lista</h2>
<p><strong>mulle-objc-list</strong> generates CSV style information from dynamic libraries,
that contain mulle-objc code. You can list the contained classes, methods and
properties. It’s the backbone of the mulle-objc tool set.</p>
<p><strong>mulle-objc-lista</strong> is the variant that handles static libraries.</p>
<p>See <a href="//github.com/mulle-objc/mulle-objc-runtime">mulle-objc-runtime</a> for more
details.</p>
<h2 id="mulle-objc-uniqueid">mulle-objc-uniqueid</h2>
<p>Generates the <code class="language-plaintext highlighter-rouge">@selector()</code> hash value from a string. This can be useful when
writing C code, that calls Objective-C.</p>
<div class="language-console highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="gp">$</span><span class="w"> </span>mulle-objc-uniqueid alloc
<span class="go">ab1bb16b
</span></code></pre></div></div>
<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">mulle_objc_object_call</span><span class="p">(</span> <span class="n">self</span><span class="p">,</span> <span class="mh">0xab1bb16b</span><span class="p">,</span> <span class="n">nil</span><span class="p">);</span>
</code></pre></div></div>
<p>See <a href="//github.com/mulle-objc/mulle-objc-runtime">mulle-objc-runtime</a> for more
details.</p>
<h2 id="mulle-objc-loader-tool">mulle-objc-loader-tool</h2>
<p>Generates the required <code class="language-plaintext highlighter-rouge">MulleObjCLoader</code> category files for a library.
This tool is used in the <a href="modern">modern workflow</a> to generate the
proper dependency information.</p>
<p>See <a href="//github.com/mulle-objc/mulle-objc-list">mulle-objc-list</a> for more
details.</p>
<h2 id="mulle-objc-signature">mulle-objc-signature</h2>
<p>Separates an ‘@encode()’ Objective-C type into constituents, separated by ‘;’.
This can be useful for creating inspection tools.</p>
<div class="language-console highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="gp">$</span><span class="w"> </span>mulle-objc-uniqueid alloc ^v@:@<span class="s2">"NSString"</span>
<span class="gp">^v;</span>@<span class="p">;</span>:<span class="p">;</span>@<span class="s2">"NSString"</span>
</code></pre></div></div>
<p>See <a href="//github.com/mulle-objc/mulle-objc-runtime">mulle-objc-runtime</a> for more
details.</p>
<h2 id="mulle-objc-printline">mulle-objc-printline</h2>
<p>A little utility to fake a “class-coverage.csv” entry.</p>
<div class="language-console highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="gp">$</span><span class="w"> </span>mulle-objc-printline <span class="nt">--method</span> <span class="nt">-foo</span> <span class="nt">--category</span> Foo Bar
<span class="gp">bbc7dbad;</span>Bar<span class="p">;</span>c7e16770<span class="p">;</span>Foo<span class="p">;</span>9f37ed7a<span class="p">;</span><span class="nt">-foo</span>
</code></pre></div></div>
<p>See <a href="coverage.html">Coverage</a> for more details about mulle-objc
coverage information.</p>
<h2 id="mulle-objc-searchid">mulle-objc-searchid</h2>
<p>Grep through libraries to find the matching string for a selector, classid,
protocol et.c. This can be useful when debugging optimized code.</p>
<div class="language-console highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="gp">$</span><span class="w"> </span>mulle-objc-searchid ab1bb16b
<span class="go">@selector( alloc) is ab1bb16b
</span></code></pre></div></div>
<p>See <a href="//github.com/mulle-objc/mulle-objc-list">mulle-objc-list</a> for more
details.</p>
<h2 id="mulle-objc-unarchive">mulle-objc-unarchive</h2>
<p>Used by the library optimization process, that unpacks and repacks static
libraries to only contain required classes and categories.</p>
<p>See <a href="//github.com/mulle-objc/mulle-objc-list">mulle-objc-list</a> for more
details.</p>
<h2 id="mulle-objc-uncovered-methods">mulle-objc-uncovered-methods</h2>
<p>Creates a list of methods not being messaged during a programs run. This can
be useful for finding missing tests.</p>
<p>See <a href="coverage.html">Coverage</a> for more details about mulle-objc coverage information.</p>
</content>
</entry>
<entry>
<title type="html"><![CDATA[Porting: Xcode projects]]></title>
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com/xcodeproj.html" />
<id>https://www.mulle-kybernetik.com/xcodeproj.html</id>
<published>2024-06-19T00:00:00+02:00</published>
<updated>2024-06-19T00:00:00+02:00</updated>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com</uri>
</author>
<content type="html"><p>As <strong>mulle-objc</strong> evolves, more and more Xcode projects will be portable
without effort. For now Foundation based Tool and Library targets are
the candidates for porting.</p>
<h2 id="mulle-xcode-to-cmake">mulle-xcode-to-cmake</h2>
<p>Getting an existing Xcode project to run with <strong>mulle-objc</strong> can be very easy
with <a href="//github.com/mulle-nat/mulle-xcode-to-cmake">mulle-xcode-to-cmake</a>.
Assuming that your Xcode project file is named “project.xcodeproj”, these
steps may already be sufficient:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>mulle-xcode-to-cmake <span class="nb">export </span>project.xcodeproj <span class="o">&gt;</span> CMakeLists.txt
mulle-sde init <span class="nt">-m</span> foundation/objc-porter executable
mulle-sde craft
</code></pre></div></div>
<p>To install:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>mulle-sde craft craftorder
mulle-sde run mulle-make <span class="nb">install</span> <span class="nt">--prefix</span> /tmp/whereever
</code></pre></div></div>
<h2 id="converting-to-the-modern-workflow">Converting to the modern workflow</h2>
<p>If your project contains one or many libraries, it is best to split them
up into multiple projects.</p>
<p>Replace the <em>objc-porter</em> environment with the <em>objc-developer</em> environment:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">rm</span> <span class="nt">-rf</span> .mulle
mulle-sde init <span class="nt">-m</span> foundation/objc-developer executable
</code></pre></div></div>
<p>And then you are ready to setup a <a href="modern.html">project</a>.</p>
</content>
</entry>
<entry>
<title type="html"><![CDATA[Porting: Objective-C++]]></title>
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com/cpp.html" />
<id>https://www.mulle-kybernetik.com/cpp.html</id>
<published>2024-06-14T00:00:00+02:00</published>
<updated>2024-06-14T00:00:00+02:00</updated>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com</uri>
</author>
<content type="html"><div class="alert alert-info" role="alert"><i class="fa fa-info-circle"></i> <b>Note:</b> C++ is already the most complex language in the
world and adding Objective-C on top of it, is like the worst of both worlds.</div>
<h2 id="use-c-from-objective-c">Use C++ from Objective-C</h2>
<p>Create a C code wrapper to call the C++ functions. Then call the C code from
Objective-C.</p>
<p>C++</p>
<p><code class="language-plaintext highlighter-rouge">cpp.h:</code></p>
<div class="language-c++ highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#ifdef __cplusplus
</span><span class="k">extern</span> <span class="s">"C"</span>
<span class="p">{</span>
<span class="cp">#endif
</span> <span class="kt">void</span> <span class="n">call_cpp1</span><span class="p">(</span> <span class="kt">char</span> <span class="o">*</span><span class="p">);</span>
<span class="kt">char</span> <span class="o">*</span><span class="n">call_cpp2</span><span class="p">(</span> <span class="kt">void</span><span class="p">);</span>
<span class="cp">#ifdef __cplusplus
</span><span class="p">};</span>
<span class="cp">#endif
</span></code></pre></div></div>
<p>Objective-C</p>
<p><code class="language-plaintext highlighter-rouge">foo.m</code></p>
<div class="language-objc highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;cpp.h&gt;</span><span class="cp">
</span>
<span class="k">@implementation</span> <span class="nc">Foo</span>
<span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="nf">callCPlusPlus1</span><span class="p">:(</span><span class="kt">char</span> <span class="o">*</span><span class="p">)</span> <span class="n">s</span>
<span class="p">{</span>
<span class="n">call_cpp1</span><span class="p">(</span> <span class="n">s</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">-</span> <span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="p">)</span> <span class="n">callCPlusPlus2</span>
<span class="p">{</span>
<span class="k">return</span><span class="p">(</span> <span class="n">call_cpp2</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">@end</span>
</code></pre></div></div>
<h2 id="use-objective-c-from-c">Use Objective-C from C++</h2>
<p>This is possible too, but you need to link against the <code class="language-plaintext highlighter-rouge">mulle-objc-runtime.h</code>
only:</p>
<blockquote>
<p>TODO: test this does this work with mulle-c11 ?</p>
</blockquote>
<div class="language-objc highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#ifdef __cplusplus
</span><span class="k">extern</span> <span class="s">"C"</span>
<span class="p">{</span>
<span class="cp">#include</span> <span class="cpf">&lt;mulle-objc/mulle-objc-runtime.h&gt;</span><span class="cp">
</span><span class="p">};</span>
<span class="cp">#endif
</span></code></pre></div></div>
<p>Now you can use the runtime functions to create instances and call them.
It’s very cumbersome though.</p>
</content>
</entry>
<entry>
<title type="html"><![CDATA[Porting: unichar]]></title>
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com/unichar.html" />
<id>https://www.mulle-kybernetik.com/unichar.html</id>
<published>2024-06-11T00:00:00+02:00</published>
<updated>2024-06-11T00:00:00+02:00</updated>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com</uri>
</author>
<content type="html"><p>Apple Foundation uses UTF-16 as <code class="language-plaintext highlighter-rouge">unichar</code>, whereas the mulle-objc Foundation used
UTF-32 as <code class="language-plaintext highlighter-rouge">unichar</code>. As long as your code is not assuming 16-bit for its size,
there should be no problem.</p>
<p>When accessing string contents as <code class="language-plaintext highlighter-rouge">unichar *</code> with <code class="language-plaintext highlighter-rouge">dataUsingEncoding:</code> use the
generic <code class="language-plaintext highlighter-rouge">NSUnicodeStringEncoding</code> instead of <code class="language-plaintext highlighter-rouge">NSUTF32StringEncoding</code>/<code class="language-plaintext highlighter-rouge">NSUTF16StringEncoding</code>.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> data = [s dataUsingEncoding:NSUnicodeStringEncoding];
here_some_unichars( (unichar *) [data bytes], [data length] / sizeof( unichar));
</code></pre></div></div>
<p>TODO: How about <code class="language-plaintext highlighter-rouge">printf</code> with <code class="language-plaintext highlighter-rouge">%S</code> ?</p>
</content>
</entry>
<entry>
<title type="html"><![CDATA[Porting: NSZone]]></title>
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com/nszone.html" />
<id>https://www.mulle-kybernetik.com/nszone.html</id>
<published>2024-06-09T00:00:00+02:00</published>
<updated>2024-06-09T00:00:00+02:00</updated>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com</uri>
</author>
<content type="html"><p>Zones are dead. Do not use the <code class="language-plaintext highlighter-rouge">withZone:</code> methods anymore.</p>
<p>mulle-objc will work well enough if you use them, but they are just superflous.
Incidentally I don’t think Apple Objective-C uses zones either anymore.</p>
<h3 id="compiler-transforms--zone-calls">Compiler transforms -zone calls</h3>
<p>With that being said, the <strong>mulle-clang</strong> compiler will transform any
call to <code class="language-plaintext highlighter-rouge">-zone</code> into <code class="language-plaintext highlighter-rouge">NULL</code>.</p>
</content>
</entry>
<entry>
<title type="html"><![CDATA[Porting: Variable Arguments in Methods]]></title>
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com/varargs.html" />
<id>https://www.mulle-kybernetik.com/varargs.html</id>
<published>2024-06-04T00:00:00+02:00</published>
<updated>2024-06-04T00:00:00+02:00</updated>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com</uri>
</author>
<content type="html"><h2 id="intro">Intro</h2>
<p>Variable arguments in methods follow the Mulle MetaABI and are incompatible
with <code class="language-plaintext highlighter-rouge">va_list</code>. C functions continue to use <code class="language-plaintext highlighter-rouge">va_list</code> though. So mulle-objc
will support both formats.</p>
<h3 id="a-typical-variable-argument-method">A typical variable argument method</h3>
<h4 id="va_list">va_list</h4>
<p>This is the <code class="language-plaintext highlighter-rouge">+[NSString stringWithFormat:]</code> method as presumably coded in the
Apple Foundation. Conventionally the <code class="language-plaintext highlighter-rouge">va_list</code> parameter in Apple Foundation
methods is called “arguments:”:</p>
<div class="language-objc highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">+</span> <span class="p">(</span><span class="n">instancetype</span><span class="p">)</span> <span class="nf">stringWithFormat</span><span class="p">:(</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span> <span class="n">format</span><span class="p">,</span> <span class="p">...</span>
<span class="p">{</span>
<span class="n">NSString</span> <span class="o">*</span><span class="n">s</span><span class="p">;</span>
<span class="kt">va_list</span> <span class="n">args</span><span class="p">;</span>
<span class="n">va_start</span><span class="p">(</span> <span class="n">args</span><span class="p">,</span> <span class="n">format</span><span class="p">);</span>
<span class="n">s</span> <span class="o">=</span> <span class="p">[</span><span class="n">self</span> <span class="nf">stringWithFormat</span><span class="p">:</span><span class="n">format</span>
<span class="nl">arguments:</span><span class="n">args</span><span class="p">];</span>
<span class="n">va_end</span><span class="p">(</span> <span class="n">args</span><span class="p">);</span>
<span class="k">return</span><span class="p">(</span> <span class="n">s</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div></div>
<h4 id="mulle_vararg_list">mulle_vararg_list</h4>
<p>In mulle-objc the type is <code class="language-plaintext highlighter-rouge">mulle_vararg_list</code>. And if it is used as a
parameter its called “mulleVarargList:” by convention. <code class="language-plaintext highlighter-rouge">va_list</code> which is still
a possibility type due to C code (e.g. <code class="language-plaintext highlighter-rouge">NSLog</code>), is called <code class="language-plaintext highlighter-rouge">varargList:</code>
instead for discrimination.</p>
<p>This is how <code class="language-plaintext highlighter-rouge">+[NSString stringWithFormat:]</code> is actually coded in
MulleFoundation:</p>
<div class="language-objc highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">+</span> <span class="p">(</span><span class="n">instancetype</span><span class="p">)</span> <span class="nf">stringWithFormat</span><span class="p">:(</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span> <span class="n">format</span><span class="p">,</span> <span class="p">...</span>
<span class="p">{</span>
<span class="n">NSString</span> <span class="o">*</span><span class="n">s</span><span class="p">;</span>
<span class="n">mulle_vararg_list</span> <span class="n">args</span><span class="p">;</span>
<span class="n">mulle_vararg_start</span><span class="p">(</span> <span class="n">args</span><span class="p">,</span> <span class="n">format</span><span class="p">);</span>
<span class="n">s</span> <span class="o">=</span> <span class="p">[</span><span class="n">self</span> <span class="nf">stringWithFormat</span><span class="p">:</span><span class="n">format</span>
<span class="nl">mulleVarargList:</span><span class="n">args</span><span class="p">];</span>
<span class="n">mulle_vararg_end</span><span class="p">(</span> <span class="n">args</span><span class="p">);</span>
<span class="k">return</span><span class="p">(</span> <span class="n">s</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div></div>
<p>So that’s pretty similar.</p>
<h4 id="mulle-objc-supports-both">mulle-objc supports both</h4>
<p>It’s not an either or scenarion, as mulle-objc supports both:</p>
<div class="language-objc highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">+</span> <span class="p">(</span><span class="n">instancetype</span><span class="p">)</span> <span class="nf">stringWithFormat</span><span class="p">:(</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span> <span class="n">format</span>
<span class="n">mulleVarargList</span><span class="o">:</span><span class="p">(</span><span class="n">mulle_vararg_list</span><span class="p">)</span> <span class="n">arguments</span>
<span class="p">{</span>
<span class="k">return</span><span class="p">(</span> <span class="p">[[[</span><span class="n">self</span> <span class="nf">alloc</span><span class="p">]</span> <span class="nf">initWithFormat</span><span class="p">:</span><span class="n">format</span>
<span class="nl">mulleVarargList:</span><span class="n">arguments</span><span class="p">]</span> <span class="nf">autorelease</span><span class="p">]);</span>
<span class="p">}</span>
<span class="k">+</span> <span class="p">(</span><span class="n">instancetype</span><span class="p">)</span> <span class="nf">stringWithFormat</span><span class="p">:(</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span> <span class="n">format</span>
<span class="n">varargList</span><span class="o">:</span><span class="p">(</span><span class="kt">va_list</span><span class="p">)</span> <span class="n">args</span>
<span class="p">{</span>
<span class="k">return</span><span class="p">(</span> <span class="p">[[[</span><span class="n">self</span> <span class="nf">alloc</span><span class="p">]</span> <span class="nf">initWithFormat</span><span class="p">:</span><span class="n">format</span>
<span class="nl">varargList:</span><span class="n">args</span><span class="p">]</span> <span class="nf">autorelease</span><span class="p">]);</span>
<span class="p">}</span>
</code></pre></div></div>
<h2 id="accessing-variable-arguments">Accessing variable arguments</h2>
<p>The actual access of variable arguments of <code class="language-plaintext highlighter-rouge">mulle_vararg_list</code> is very
different though.</p>
<p>See <a href="https://github.com/MulleFoundation/objc-compat">objc-compat</a> for some
details on how to achieve this portably.</p>
</content>
</entry>
<entry>
<title type="html"><![CDATA[Porting: Synchronized is gone]]></title>
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com/synchronized.html" />
<id>https://www.mulle-kybernetik.com/synchronized.html</id>
<published>2024-05-30T00:00:00+02:00</published>
<updated>2024-05-30T00:00:00+02:00</updated>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com</uri>
</author>
<content type="html"><div class="alert alert-info" role="alert"><i class="fa fa-info-circle"></i> <b>Note:</b> See <a href="https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html#//apple_ref/doc/uid/10000057i-CH8-SW16">Threading Programming Guide</a> for more information about <tt>@synchronized</tt>.</div>
<h2 id="use-mulle-thread-for-least-hassle">Use mulle-thread for least hassle.</h2>
<p><a href="//github.com/mulle-concurrent/mulle-thread">mulle-thread</a>
is available on all platforms, that run Objective-C.</p>
<p>Use <code class="language-plaintext highlighter-rouge">mulle_thread_mutex_t</code> to transform</p>
<div class="language-objc highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">myFunction</span>
<span class="p">{</span>
<span class="k">@synchronized</span><span class="p">()</span>
<span class="p">{</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>
<p>to</p>
<div class="language-objc highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">static</span> <span class="n">mulle_thread_mutex_t</span> <span class="n">lock</span><span class="p">;</span>
<span class="k">+</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">load</span>
<span class="p">{</span>
<span class="n">mulle_thread_mutex_init</span><span class="p">(</span> <span class="o">&amp;</span><span class="n">lock</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">+</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">unload</span>
<span class="p">{</span>
<span class="n">mulle_thread_mutex_done</span><span class="p">(</span> <span class="o">&amp;</span><span class="n">lock</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">myFunction</span>
<span class="p">{</span>
<span class="n">mulle_thread_mutex_lock</span><span class="p">(</span> <span class="o">&amp;</span><span class="n">lock</span><span class="p">);</span>
<span class="p">{</span>
<span class="p">}</span>
<span class="n">mulle_thread_mutex_unlock</span><span class="p">(</span> <span class="o">&amp;</span><span class="n">lock</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div></div>
<h2 id="use-nslock-instead">Use NSLock instead</h2>
<div class="language-objc highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">static</span> <span class="n">NSLock</span> <span class="n">lock</span><span class="p">;</span>
<span class="k">+</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">initialize</span>
<span class="p">{</span>
<span class="k">if</span><span class="p">(</span> <span class="o">!</span> <span class="n">lock</span><span class="p">)</span>
<span class="n">lock</span> <span class="o">=</span> <span class="p">[</span><span class="n">NSLock</span> <span class="nf">new</span><span class="p">];</span>
<span class="p">}</span>
<span class="k">+</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">deinitialize</span>
<span class="p">{</span>
<span class="k">if</span><span class="p">(</span> <span class="n">lock</span><span class="p">)</span>
<span class="p">{</span>
<span class="p">[</span><span class="n">lock</span> <span class="nf">release</span><span class="p">];</span>
<span class="n">lock</span> <span class="o">=</span> <span class="nb">nil</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">myFunction</span>
<span class="p">{</span>
<span class="p">[</span><span class="n">lock</span> <span class="nf">lock</span><span class="p">];</span>
<span class="p">{</span>
<span class="p">}</span>
<span class="p">[</span><span class="n">lock</span> <span class="nf">unlock</span><span class="p">];</span>
<span class="p">}</span>
</code></pre></div></div>
<h3 id="good-points">Good points</h3>
<ul>
<li>code works in all runtimes without another dependency</li>
<li><code class="language-plaintext highlighter-rouge">+deinitialize</code> will not be called by other runtimes, it’s a harmless addition</li>
</ul>
<h3 id="bad-points">Bad points</h3>
<ul>
<li>A <strong>NSLock</strong> is slower than a <code class="language-plaintext highlighter-rouge">mulle_thread_mutex_t</code></li>
<li>The lock has not become a proper mulle-objc root object, so this code will leak in tests.</li>
</ul>
<p>You could fix this with deleting <code class="language-plaintext highlighter-rouge">+deinitialize</code> and rewriting <code class="language-plaintext highlighter-rouge">+initialize</code> as:</p>
<p>``` objc</p>
<ul>
<li>(void) initialize
{
if( ! lock)
{
lock = [NSLock new];
#ifdef <strong>MULLE_OBJC</strong>
[lock _becomeRootObject];
[lock release;]
#endif
}
}</li>
</ul>
<p>// + (void) deinitialize clashes with _becomeRootObject and must be removed</p>
</content>
</entry>
<entry>
<title type="html"><![CDATA[Porting: @synthesize is useless now]]></title>
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com/synthesize.html" />
<id>https://www.mulle-kybernetik.com/synthesize.html</id>
<published>2024-05-25T00:00:00+02:00</published>
<updated>2024-05-25T00:00:00+02:00</updated>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com</uri>
</author>
<content type="html"><p>If <code class="language-plaintext highlighter-rouge">@synthesize</code> renames the instance variable, you will unfortunately have to
actually rename the instance variable to the name of the property prefixed
with an underscore, otherwise it won’t work. Do best is to just delete the
<code class="language-plaintext highlighter-rouge">@synthesize</code> and fix the errors.</p>
</content>
</entry>
<entry>
<title type="html"><![CDATA[Porting @property]]></title>
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com/property.html" />
<id>https://www.mulle-kybernetik.com/property.html</id>
<published>2024-05-20T00:00:00+02:00</published>
<updated>2024-05-20T00:00:00+02:00</updated>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com</uri>
</author>
<content type="html"><p>Porting properties in a fashion that works in ARC code and in mulle-objc is
tricky. It is best if you can restrict yourself to <strong>assign</strong>, <strong>copy</strong> and <strong>retain</strong>.</p>
<h2 id="property-deallocation">Property deallocation</h2>
<div class="alert alert-info" role="alert"><i class="fa fa-info-circle"></i> <b>Note:</b> This is a rare case, where mulle-objc is compatible with ARC, but incompatible with MMR.</div>
<p>In Apple ARC, properties are automatically cleared during <code class="language-plaintext highlighter-rouge">-dealloc</code>. In Apple Manual Retain-Release Mode (MRR)
you have to do it yourself during <code class="language-plaintext highlighter-rouge">-dealloc</code>.</p>
<p>In mulle-objc all properties that reference objects or pointers are cleared during <code class="language-plaintext highlighter-rouge">-finalize</code> by
setting them to <strong>0</strong>.</p>
<p><strong>readonly</strong> properties - they have no setter - are not cleared. But in mulle-objc <em>they are backed by an <strong>ivar</strong></em>.
It is open to discussion if you want to release them in <code class="language-plaintext highlighter-rouge">-dealloc</code> for compatibility or use <code class="language-plaintext highlighter-rouge">-finalize</code> to break
possible retain cycles.</p>
<p>Here is how to write <code class="language-plaintext highlighter-rouge">-dealloc</code> for compatiblity with MMR (also see <a href="arc.html">ARC Porting tips</a>):</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#if __has_feature(objc_arc) || defined( __MULLE_OBJC__)
# define PROPERTY_RELEASE( p)
#else
# define PROPERTY_RELEASE( p) [_p release]
#endif
#if __has_feature(objc_arc)
# define SUPER_DEALLOC()
#else
# define SUPER_DEALLOC( p) [super dealloc]
#endif
- (void) dealloc
{
PROPERTY_RELEASE( a)
PROPERTY_RELEASE( b)
PROPERTY_RELEASE( c)
SUPER_DEALLOC()
}
</code></pre></div></div>
<h2 id="missing-attributes">Missing Attributes</h2>
<h3 id="atomic">atomic</h3>
<p>Yup it’s gone. Use locking or the atomic operations provided by
<a href="//github.com/mulle-concurrent/mulle-thread">mulle-thread</a>.</p>
<h3 id="weak">weak</h3>
<p>Use <strong>assign</strong> instead.</p>
<div class="alert alert-info" role="alert"><i class="fa fa-info-circle"></i> <b>Note:</b> Use C containers to manage weak references
instead. They won’t <em>magically</em> cleanup though.</div>
<h3 id="strong">strong</h3>
<p>When declaring a property use <strong>copy</strong> or <strong>retain</strong>. You usually
use <strong>copy</strong> for <code class="language-plaintext highlighter-rouge">NSValue</code>, <code class="language-plaintext highlighter-rouge">NSDate</code>, <code class="language-plaintext highlighter-rouge">NSNumber</code> and <code class="language-plaintext highlighter-rouge">NSString</code> arguments,
and <strong>retain</strong> for everything else.</p>
<h3 id="nullable">nullable</h3>
<p>One of the strong points of Objective-C is its gracious handling of
<code class="language-plaintext highlighter-rouge">nil</code> values, which simplifies coding a lot. Remember that messaging <code class="language-plaintext highlighter-rouge">nil</code>
also produces <code class="language-plaintext highlighter-rouge">nil</code>. With the introduction of <code class="language-plaintext highlighter-rouge">nonnull</code> <code class="language-plaintext highlighter-rouge">nullable</code> was
also introduced. It is superflous.</p>
<p>You can easily get rid of <code class="language-plaintext highlighter-rouge">nullable</code> compile errors with:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#define nullable
</code></pre></div></div>
<div class="alert alert-info" role="alert"><i class="fa fa-info-circle"></i> <b>Note:</b> Tedious checks for <code class="language-plaintext highlighter-rouge">nil</code> means you are optimizing
your code for the error case. Use <strong>nonnull</strong> sparingly. If a <code class="language-plaintext highlighter-rouge">nil</code> parameter
has no ill effect, don’t mark the code <strong>nonnull</strong>.</div>
<h3 id="unsafe_unretained">unsafe_unretained</h3>
<p>Use <strong>assign</strong> instead.</p>
<h3 id="class">class</h3>
<p>Remove the property. Use <code class="language-plaintext highlighter-rouge">static</code> variables in your <code class="language-plaintext highlighter-rouge">@implementation</code>
then write and declare <code class="language-plaintext highlighter-rouge">+</code> accessors for them.</p>
<div class="alert alert-info" role="alert"><i class="fa fa-info-circle"></i> <b>Note:</b> <code class="language-plaintext highlighter-rouge">class</code> is likely to make a comeback in a future version.</div>
</content>
</entry>
<entry>
<title type="html"><![CDATA[Porting: Protocols are a kind of @selector]]></title>
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com/protocol.html" />
<id>https://www.mulle-kybernetik.com/protocol.html</id>
<published>2024-05-15T00:00:00+02:00</published>
<updated>2024-05-15T00:00:00+02:00</updated>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com</uri>
</author>
<content type="html"><p>This will be tricky, but it is a very rare occurence. In Objective-C you
can actually treat a <strong>@protocol</strong> as an object and assign it to <code class="language-plaintext highlighter-rouge">id</code>.
You can not in mulle-objc.</p>
<p>There are no good tips for this yet.</p>
</content>
</entry>
<entry>
<title type="html"><![CDATA[Porting: @package]]></title>
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com/package.html" />
<id>https://www.mulle-kybernetik.com/package.html</id>
<published>2024-05-10T00:00:00+02:00</published>
<updated>2024-05-10T00:00:00+02:00</updated>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com</uri>
</author>
<content type="html"><p>Replace with <code class="language-plaintext highlighter-rouge">@public</code>.</p>
</content>
</entry>
<entry>
<title type="html"><![CDATA[Porting: @import]]></title>
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com/modules.html" />
<id>https://www.mulle-kybernetik.com/modules.html</id>
<published>2024-05-05T00:00:00+02:00</published>
<updated>2024-05-05T00:00:00+02:00</updated>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com</uri>
</author>
<content type="html"><p>You will have to use <code class="language-plaintext highlighter-rouge">#import</code> instead.</p>
</content>
</entry>
<entry>
<title type="html"><![CDATA[Porting: generics]]></title>
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com/generics.html" />
<id>https://www.mulle-kybernetik.com/generics.html</id>
<published>2024-05-02T00:00:00+02:00</published>
<updated>2024-05-02T00:00:00+02:00</updated>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com</uri>
</author>
<content type="html"><h2 id="rewrite-by-removing-generics-typing">Rewrite by removing generics typing</h2>
<p>If your parameter is specified as <code class="language-plaintext highlighter-rouge">NSArray&lt;NSNumber *&gt;</code> reduce the type
to <code class="language-plaintext highlighter-rouge">NSArray *</code>.</p>
<h2 id="validate-content-at-runtime">Validate content at runtime</h2>
<p>The way to validate array content in Objective-C is at runtime, when you
are inserting a value:</p>
<div class="language-objc highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">NSParameterAssert</span><span class="p">(</span> <span class="p">[</span><span class="n">obj</span> <span class="nf">isKindOfClass</span><span class="p">:[</span><span class="n">NSNumber</span> <span class="nf">class</span><span class="p">]]);</span>
<span class="p">[</span><span class="n">array</span> <span class="nf">addObject</span><span class="p">:</span><span class="n">obj</span><span class="p">];</span>
</code></pre></div></div>
<h2 id="use-id-for-truely-generic-algorithms">Use <code class="language-plaintext highlighter-rouge">id</code> for truely generic algorithms</h2>
<p>If you want true generic algorithms, consider replacing your type with <code class="language-plaintext highlighter-rouge">id</code>.
You can also then specify the methods your algorithm requires using a
<code class="language-plaintext highlighter-rouge">@protocol</code> and then type your methods with <code class="language-plaintext highlighter-rouge">id &lt;protocol&gt;</code>.
This keeps the algorithm the most resusable.</p>
</content>
</entry>
<entry>
<title type="html"><![CDATA[Porting: . syntax for properties]]></title>
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com/dotsyntax.html" />
<id>https://www.mulle-kybernetik.com/dotsyntax.html</id>
<published>2024-04-30T00:00:00+02:00</published>
<updated>2024-04-30T00:00:00+02:00</updated>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com</uri>
</author>
<content type="html"><p>Hopefully there will be a code-conversion tool in the future, but for now
translate dot syntax to Objective-C calls.</p>
<p>e.g.</p>
<div class="language-objc highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">setup</span>
<span class="p">{</span>
<span class="n">self</span><span class="p">.</span><span class="n">propertyA</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="n">self</span><span class="p">.</span><span class="n">propertyB</span><span class="p">.</span><span class="n">numberC</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="n">self</span><span class="p">.</span><span class="n">propertyB</span><span class="p">.</span><span class="n">numberD</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>
<div class="language-objc highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">setup</span>
<span class="p">{</span>
<span class="n">id</span> <span class="n">propertyB</span><span class="p">;</span>
<span class="p">[</span><span class="n">self</span> <span class="nf">setPropertyA</span><span class="p">:</span><span class="mi">0</span><span class="p">];</span>
<span class="n">propertyB</span> <span class="o">=</span> <span class="p">[</span><span class="n">self</span> <span class="nf">propertyB</span><span class="p">];</span>
<span class="p">[</span><span class="n">propertyB</span> <span class="nf">setNumberC</span><span class="p">:</span><span class="mi">1</span><span class="p">];</span>
<span class="p">[</span><span class="n">propertyB</span> <span class="nf">setNumberD</span><span class="p">:</span><span class="mi">2</span><span class="p">];</span>
<span class="p">}</span>
</code></pre></div></div>
<p>This is also better code.</p>
</content>
</entry>
<entry>
<title type="html"><![CDATA[Porting: ^blocks]]></title>
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com/block.html" />
<id>https://www.mulle-kybernetik.com/block.html</id>
<published>2024-04-25T00:00:00+02:00</published>
<updated>2024-04-25T00:00:00+02:00</updated>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com</uri>
</author>
<content type="html"><h2 id="disable-blocks">Disable blocks</h2>
<p>A good first step is to wrap all method declarations and definitions with</p>
<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#ifdef __has_extension(blocks)
#endif
</span></code></pre></div></div>
<p>If you are lucky, blocks are just a non-integral,value-added feature
in the ported library.</p>
<h2 id="rewrite">Rewrite</h2>
<p>Generally at this point, you should check <em>how much</em> blocks code there is.
If it is used only a few places, here are some ideas how to convert the code
for mulle-objc.</p>
<h3 id="transform-to-nsinvocation">Transform to NSInvocation</h3>
<p>Blocks that are stored for later execution, are basically a form of
<code class="language-plaintext highlighter-rouge">NSInvocation</code>. Transform your blocks code into a method and create an
<code class="language-plaintext highlighter-rouge">NSInvocation</code> for it.</p>
<h3 id="transform-to-c-function">Transform to C function</h3>
<p>If the block is used immediately, perhaps to map it to an NSArray,
extract the code into a C function. Encapsulate the parameters into a C
struct.</p>
</content>
</entry>
<entry>
<title type="html"><![CDATA[Porting: ARC]]></title>
<link rel="alternate" type="text/html" href="https://www.mulle-kybernetik.com/arc.html" />
<id>https://www.mulle-kybernetik.com/arc.html</id>
<published>2024-04-20T00:00:00+02:00</published>
<updated>2024-04-20T00:00:00+02:00</updated>
<author>
<name>Nat!</name>
<uri>https://www.mulle-kybernetik.com</uri>
</author>
<content type="html"><p>ARC as a technology is not available in <strong>mulle-objc</strong> and never will be.</p>
<p>Ideally though, code should remain functional in ARC but work flawlessly in
mulle-objc.</p>
<h2 id="use-convenience-constructors">Use convenience constructors</h2>
<p>Outside of <code class="language-plaintext highlighter-rouge">-init</code> and <code class="language-plaintext highlighter-rouge">-dealloc</code>, replace <code class="language-plaintext highlighter-rouge">[[obj alloc] init]</code> calls with
convenience constructors like <code class="language-plaintext highlighter-rouge">+[NSArray array]</code>, if available.</p>
<h3 id="create-your-own-convenience-constructors">Create your own convenience constructors</h3>
<p>If a convenience constructor is not available, it might be useful to
create your own with a category. Consider this if there is a lot of calls
for the same class/method combination.</p>
<p>This is the code to replace a <code class="language-plaintext highlighter-rouge">[[Foo alloc] initWithRandomNumber]</code> with
<code class="language-plaintext highlighter-rouge">[Foo fooWithRandomNumber]</code>:</p>
<div class="language-objc highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">@interface</span> <span class="nc">Foo</span><span class="p">(</span> <span class="nl">Convenience</span><span class="p">)</span>
<span class="k">+</span> <span class="p">(</span><span class="n">instancetype</span><span class="p">)</span> <span class="n">fooWithRandomNumber</span><span class="p">;</span>
<span class="k">@end</span>
<span class="k">@implementation</span> <span class="nc">Foo</span><span class="p">(</span> <span class="nl">Convenience</span><span class="p">)</span>
<span class="k">+</span> <span class="p">(</span><span class="n">instancetype</span><span class="p">)</span> <span class="n">fooWithRandomNumber</span>
<span class="p">{</span>
<span class="n">id</span> <span class="n">obj</span><span class="p">;</span>
<span class="n">obj</span> <span class="o">=</span> <span class="p">[[</span><span class="n">Foo</span> <span class="nf">alloc</span><span class="p">]</span> <span class="nf">initWithRandomNumber</span><span class="p">:</span><span class="n">rand</span><span class="p">()];</span>
<span class="cp">#if ! __has_feature(objc_arc)
</span> <span class="n">obj</span> <span class="o">=</span> <span class="p">[</span><span class="n">obj</span> <span class="nf">autorelease</span><span class="p">];</span>
<span class="cp">#endif
</span> <span class="k">return</span><span class="p">(</span> <span class="n">obj</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">@end</span>
</code></pre></div></div>
<h3 id="wrap-allocinit-calls">Wrap alloc/init calls</h3>
<p>You could also use this idea to wrap your <code class="language-plaintext highlighter-rouge">[[obj alloc] init]</code> code</p>
<div class="language-objc highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#if __has_feature(objc_arc)
# define AUTORELEASE( x) x
#else
# define AUTORELEASE( x) NSAutoreleaseObject( x)
#endif
</span></code></pre></div></div>
<p>So you can simplify the above written <code class="language-plaintext highlighter-rouge">+fooWithRandomNumber</code> like this:</p>
<div class="language-objc highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">+</span> <span class="p">(</span><span class="n">instancetype</span><span class="p">)</span> <span class="n">fooWithRandomNumber</span>
<span class="p">{</span>
<span class="k">return</span><span class="p">(</span> <span class="n">AUTORELEASE</span><span class="p">(</span> <span class="p">[[</span><span class="n">Foo</span> <span class="nf">alloc</span><span class="p">]</span> <span class="nf">initWithRandomNumber</span><span class="p">:</span><span class="n">rand</span><span class="p">()]</span> <span class="p">));</span>
<span class="p">}</span>
</code></pre></div></div>
<h3 id="add-super-dealloc-to--dealloc">Add <code class="language-plaintext highlighter-rouge">[super dealloc]</code> to <code class="language-plaintext highlighter-rouge">-dealloc</code></h3>
<p>You could use this idea to modify your <code class="language-plaintext highlighter-rouge">-dealloc</code> code</p>
<div class="language-objc highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#if __has_feature(objc_arc)
# define SUPER_DEALLOC()
#else
# define SUPER_DEALLOC() [super dealloc]
#endif
</span></code></pre></div></div>
<div class="language-objc highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">dealloc</span>
<span class="p">{</span>
<span class="n">SUPER_DEALLOC</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div></div>
<h2 id="fix-convenience-constructors-in--init">Fix convenience constructors in -init</h2>
<div class="language-objc highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">-</span> <span class="p">(</span><span class="n">id</span><span class="p">)</span> <span class="n">init</span>
<span class="p">{</span>
<span class="n">self</span> <span class="o">=</span> <span class="p">[</span><span class="n">super</span> <span class="nf">init</span><span class="p">];</span>
<span class="k">if</span><span class="p">(</span> <span class="n">self</span><span class="p">)</span>
<span class="n">_foo</span> <span class="o">=</span> <span class="p">[</span><span class="n">NSArray</span> <span class="nf">array</span><span class="p">];</span>
<span class="k">return</span><span class="p">(</span> <span class="n">self</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div></div>
<p>Here an instance variable is initialized with an autoreleased <code class="language-plaintext highlighter-rouge">NSArray</code>, which
will soon be unavailable.</p>
<p>Write <code class="language-plaintext highlighter-rouge">_foo = [[NSArray alloc] init];</code> to make your code ARC and mulle-objc
compatible.</p>
<h2 id="release-instance-variables-manually">Release instance variables manually</h2>
<p>There is often no <code class="language-plaintext highlighter-rouge">-dealloc</code> method in ARC code. That is fine if the
class has only properties. Then mulle-objc will clean up automatically.
If your class has non-property instance variables, they must be released in
<code class="language-plaintext highlighter-rouge">-dealloc</code> or <code class="language-plaintext highlighter-rouge">-finalize</code>.</p>
<p>Since <code class="language-plaintext highlighter-rouge">-finalize</code> isn’t used in ARC code, it can be a good place to do it.
Othewise you could use <code class="language-plaintext highlighter-rouge">#if __has_feature( objc_arc)</code> in <code class="language-plaintext highlighter-rouge">-dealloc</code>.</p>
<div class="language-objc highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#ifdef __MULLE_OBJC__
</span><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="n">finalize</span>
<span class="p">{</span>
<span class="p">[</span><span class="n">_foo</span> <span class="nf">autorelease</span><span class="p">];</span>
<span class="n">_foo</span> <span class="o">=</span> <span class="nb">nil</span><span class="p">;</span>
<span class="p">[</span><span class="n">super</span> <span class="nf">finalize</span><span class="p">];</span>
<span class="p">}</span>
<span class="cp">#endif