snipt

Ctrl+h for KB shortcuts

Objective-C

Dynamically instanting classes in Objective-C

id obj = [[NSClassFromString(@"MySpecialClass") alloc] init];
https://snipt.net/embed/017e19107befb46e3e45ce38a56c6df0/
/raw/017e19107befb46e3e45ce38a56c6df0/
017e19107befb46e3e45ce38a56c6df0
objective-c
Objective-C
1
2019-07-15T18:18:05
True
False
False
/api/public/snipt/21186/
dynamically-instanting-classes-in-objective-c
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="kt">id</span> <span class="n">obj</span> <span class="o">=</span> <span class="p">[[</span><span class="n">NSClassFromString</span><span class="p">(</span><span class="s">@&quot;MySpecialClass&quot;</span><span class="p">)</span> <span class="n">alloc</span><span class="p">]</span> <span class="n">init</span><span class="p">];</span> </span></pre></div> </td></tr></table>
obj-c, objc

Objective-C

NSString and NSData conversion

1
2
3
4
5
NSString* aStr;
aStr = [[NSString alloc] initWithData:aData encoding:NSASCIIStringEncoding];

NSData* aData;
aData = [aStr dataUsingEncoding: NSASCIIStringEncoding];
https://snipt.net/embed/19435eaabcc91e1595ffeac965f6c89c/
/raw/19435eaabcc91e1595ffeac965f6c89c/
19435eaabcc91e1595ffeac965f6c89c
objective-c
Objective-C
5
2019-07-15T12:38:30
True
False
False
/api/public/snipt/3708/
nsstring-and-nsdata-conversion
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="bp">NSString</span><span class="o">*</span> <span class="n">aStr</span><span class="p">;</span> </span><span id="L-2"><a name="L-2"></a><span class="n">aStr</span> <span class="o">=</span> <span class="p">[[</span><span class="bp">NSString</span> <span class="n">alloc</span><span class="p">]</span> <span class="nl">initWithData</span><span class="p">:</span><span class="n">aData</span> <span class="nl">encoding</span><span class="p">:</span><span class="n">NSASCIIStringEncoding</span><span class="p">];</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="bp">NSData</span><span class="o">*</span> <span class="n">aData</span><span class="p">;</span> </span><span id="L-5"><a name="L-5"></a><span class="n">aData</span> <span class="o">=</span> <span class="p">[</span><span class="n">aStr</span> <span class="nl">dataUsingEncoding</span><span class="p">:</span> <span class="n">NSASCIIStringEncoding</span><span class="p">];</span> </span></pre></div> </td></tr></table>
obj-c

Objective-C

NSString to int or double

1
2
3
NSString    *theString = @"512";
int        theInteger = [theString intValue];
double      theDouble =  [theString doubleValue];
https://snipt.net/embed/9f94aa37fee5f01685bdcee947dfbf22/
/raw/9f94aa37fee5f01685bdcee947dfbf22/
9f94aa37fee5f01685bdcee947dfbf22
objective-c
Objective-C
3
2019-07-15T09:17:36
True
False
False
/api/public/snipt/3707/
nsstring-to-int-or-double
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="bp">NSString</span> <span class="o">*</span><span class="n">theString</span> <span class="o">=</span> <span class="s">@&quot;512&quot;</span><span class="p">;</span> </span><span id="L-2"><a name="L-2"></a><span class="kt">int</span> <span class="n">theInteger</span> <span class="o">=</span> <span class="p">[</span><span class="n">theString</span> <span class="n">intValue</span><span class="p">];</span> </span><span id="L-3"><a name="L-3"></a><span class="kt">double</span> <span class="n">theDouble</span> <span class="o">=</span> <span class="p">[</span><span class="n">theString</span> <span class="n">doubleValue</span><span class="p">];</span> </span></pre></div> </td></tr></table>
obj-c

Objective-C

proper way to write an Objective-C initializer

1
2
3
4
5
6
- (id)init {
    if ((self = [super init])) {
        // initialization code
    }
    return self;
}
https://snipt.net/embed/52db1a34c41233bf780f975ee0722098/
/raw/52db1a34c41233bf780f975ee0722098/
52db1a34c41233bf780f975ee0722098
objective-c
Objective-C
6
2019-07-15T09:17:40
True
False
False
/api/public/snipt/795/
correct-way-to-write-a-cocoa-initializer
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="p">-</span> <span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nf">init</span> <span class="p">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="k">if</span> <span class="p">((</span><span class="nb">self</span> <span class="o">=</span> <span class="p">[</span><span class="nb">super</span> <span class="n">init</span><span class="p">]))</span> <span class="p">{</span> </span><span id="L-3"><a name="L-3"></a> <span class="c1">// initialization code</span> </span><span id="L-4"><a name="L-4"></a> <span class="p">}</span> </span><span id="L-5"><a name="L-5"></a> <span class="k">return</span> <span class="nb">self</span><span class="p">;</span> </span><span id="L-6"><a name="L-6"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
cocoa, init, initializer, obj-c, objective-c