Snipt.net is closing on December 31st, 2016

For more information, please visit this blog post.

snipt

Ctrl+h for KB shortcuts

Objective-C

Set error

1
2
3
4
5
6
7
#define SetNSError(ERROR_VAR, FORMAT,...)	\
	if (ERROR_VAR) {	\
		NSString *errStr = [@"+[NSObject(JRSwizzle) jr_swizzleMethod:withMethod:error:]: " stringByAppendingFormat:FORMAT,##__VA_ARGS__];	\
		*ERROR_VAR = [NSError errorWithDomain:@"NSCocoaErrorDomain" \
										 code:-1	\
									 userInfo:[NSDictionary dictionaryWithObject:errStr forKey:NSLocalizedDescriptionKey]]; \
	}
https://snipt.net/embed/b51edec9094ca9e3fa408f8718b46824/
https://snipt.net/raw/b51edec9094ca9e3fa408f8718b46824/
b51edec9094ca9e3fa408f8718b46824
objective-c
Objective-C
7
2016-11-27T02:12:35
True
False
False
/api/public/snipt/21888/
set-error
<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> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">#define SetNSError(ERROR_VAR, FORMAT,...) \</span> </span><span id="L-2"><a name="L-2"></a><span class="cp"> if (ERROR_VAR) { \</span> </span><span id="L-3"><a name="L-3"></a><span class="cp"> NSString *errStr = [@&quot;+[NSObject(JRSwizzle) jr_swizzleMethod:withMethod:error:]: &quot; stringByAppendingFormat:FORMAT,##__VA_ARGS__]; \</span> </span><span id="L-4"><a name="L-4"></a><span class="cp"> *ERROR_VAR = [NSError errorWithDomain:@&quot;NSCocoaErrorDomain&quot; \</span> </span><span id="L-5"><a name="L-5"></a><span class="cp"> code:-1 \</span> </span><span id="L-6"><a name="L-6"></a><span class="cp"> userInfo:[NSDictionary dictionaryWithObject:errStr forKey:NSLocalizedDescriptionKey]]; \</span> </span><span id="L-7"><a name="L-7"></a><span class="cp"> }</span> </span></pre></div> </td></tr></table>
cocoa

Objective-C

view fade animation

1
2
3
  CATransition* animation = [CATransition animation];
	[animation setType:kCATransitionFade];
	[[aSuperview layer] addAnimation:animation forKey:@"layerAnimation"];
https://snipt.net/embed/83316235271e076fb72cfb872d29e160/
https://snipt.net/raw/83316235271e076fb72cfb872d29e160/
83316235271e076fb72cfb872d29e160
objective-c
Objective-C
4
2016-12-02T17:32:00
True
False
False
/api/public/snipt/8785/
view-fade-animation
<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">CATransition</span><span class="o">*</span> <span class="n">animation</span> <span class="o">=</span> <span class="p">[</span><span class="bp">CATransition</span> <span class="n">animation</span><span class="p">];</span> </span><span id="L-2"><a name="L-2"></a> <span class="p">[</span><span class="n">animation</span> <span class="nl">setType</span><span class="p">:</span><span class="n">kCATransitionFade</span><span class="p">];</span> </span><span id="L-3"><a name="L-3"></a> <span class="p">[[</span><span class="n">aSuperview</span> <span class="n">layer</span><span class="p">]</span> <span class="nl">addAnimation</span><span class="p">:</span><span class="n">animation</span> <span class="nl">forKey</span><span class="p">:</span><span class="s">@&quot;layerAnimation&quot;</span><span class="p">];</span> </span></pre></div> </td></tr></table>
animation, cocoa, core-animation, fade, view

SQL

uiview to uiimage

1
2
3
4
UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
https://snipt.net/embed/6c7cc283d3e46ef55b657af92d6f9ae8/
https://snipt.net/raw/6c7cc283d3e46ef55b657af92d6f9ae8/
6c7cc283d3e46ef55b657af92d6f9ae8
sql
SQL
4
2016-12-02T17:32:09
True
False
False
/api/public/snipt/8654/
uiview-to-uiimage
<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></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="n">UIGraphicsBeginImageContext</span><span class="p">(</span><span class="n">myView</span><span class="p">.</span><span class="n">bounds</span><span class="p">.</span><span class="k">size</span><span class="p">);</span> </span><span id="L-2"><a name="L-2"></a><span class="p">[</span><span class="n">myView</span><span class="p">.</span><span class="n">layer</span> <span class="n">renderInContext</span><span class="p">:</span><span class="n">UIGraphicsGetCurrentContext</span><span class="p">()];</span> </span><span id="L-3"><a name="L-3"></a><span class="n">UIImage</span> <span class="o">*</span><span class="n">viewImage</span> <span class="o">=</span> <span class="n">UIGraphicsGetImageFromCurrentImageContext</span><span class="p">();</span> </span><span id="L-4"><a name="L-4"></a><span class="n">UIGraphicsEndImageContext</span><span class="p">();</span> </span></pre></div> </td></tr></table>
cocoa, to, uiimage, uiview

Ruby

Cocoa-ing Rails

1
2
3
4
5
# Include your application configuration below - at the end of environment.rb
# May Cocoa be with us
require 'osx/cocoa'
include OSX
OSX.require_framework 'any_extra_framework'
https://snipt.net/embed/01dbe8ab34a825279152f0bab351684d/
https://snipt.net/raw/01dbe8ab34a825279152f0bab351684d/
01dbe8ab34a825279152f0bab351684d
rb
Ruby
5
2016-12-02T10:53:05
True
False
False
/api/public/snipt/2743/
cocoa-ing-rails
<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="c1"># Include your application configuration below - at the end of environment.rb</span> </span><span id="L-2"><a name="L-2"></a><span class="c1"># May Cocoa be with us</span> </span><span id="L-3"><a name="L-3"></a><span class="nb">require</span> <span class="s1">&#39;osx/cocoa&#39;</span> </span><span id="L-4"><a name="L-4"></a><span class="kp">include</span> <span class="no">OSX</span> </span><span id="L-5"><a name="L-5"></a><span class="no">OSX</span><span class="o">.</span><span class="n">require_framework</span> <span class="s1">&#39;any_extra_framework&#39;</span> </span></pre></div> </td></tr></table>
"ruby on rails", cocoa, rails, ruby, rubycocoa

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/
https://snipt.net/raw/52db1a34c41233bf780f975ee0722098/
52db1a34c41233bf780f975ee0722098
objective-c
Objective-C
6
2016-11-25T12:07:24
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

Objective-C

RaiseMan Init Method

1
2
3
4
5
6
-(id)init{
	[super init];
	expectedRaise=5.0;
	personName=@"New Person";
	return self;
}
https://snipt.net/embed/918b704c7e391246a8b754d4fc6f9147/
https://snipt.net/raw/918b704c7e391246a8b754d4fc6f9147/
918b704c7e391246a8b754d4fc6f9147
objective-c
Objective-C
6
2016-12-02T23:33:24
True
False
False
/api/public/snipt/775/
raiseman-init-method
<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="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="p">[</span><span class="nb">super</span> <span class="n">init</span><span class="p">];</span> </span><span id="L-3"><a name="L-3"></a> <span class="n">expectedRaise</span><span class="o">=</span><span class="mf">5.0</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a> <span class="n">personName</span><span class="o">=</span><span class="s">@&quot;New Person&quot;</span><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
Copyrighted, illegal, or inappropriate content? Email [email protected].