10 useful snippets for Apple iPhone

Run a method after n seconds

The whole family performSelector is really interesting and can be useful in a multitude of cases. Its application simplest and most common is the following:

1
2
3
4
5
@selector ( myMethod ) withObject : nil afterDelay : 3 ] ; [Self performSelector: @ selector (myMethod) withObject: nil afterDelay: 3];
/ /
void ) myMethod { - (Void) myMethod {
"Hello World!" ) ; NSLog (@ "Hello World!");
}

However, consider that the "timer" is not accurate. This procedure, therefore, should be used when it is not prompted for a "significant" temporal precision.

Retrieve the version of the application

1
2
version = [ [ [ NSBundle mainBundle ] infoDictionary ] objectForKey : @ "CFBundleVersion" ] ; NSString * version = [[[ NSBundle mainBundle] infoDictionary] objectForKey: @ "CFBundleVersion"];
"versione = %@" , version ) ; NSLog (@ "version =% @", version);

YES, TRUE or false?

e TRUE sono in pratica la stessa edentica cosa: Going to comb through the Kernel Apple iPhone you can realize that YES , true and TRUE edentica are basically the same thing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/ / Definition of the YES
# Define YES (BOOL) 1
# Define NO (BOOL) 0

/ / Definition of true
# Define true 1
# Define false 0

/ / Definition of TRUE
# If! Defined (TRUE)
# Define TRUE 1
# Endif

# If! Defined (FALSE)
# Define FALSE 0
# Endif

At least for now ...

Chatter

1
2
3
# Import <AudioToolbox/AudioToolbox.h>
/ /
; AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);

Pointer CGImageRef from a UIImage

1
2
3
4
[ UIImage imageNamed : @ "LittleHeart.png" ] ; UIImage * heart = [UIImage imageNamed: @ "LittleHeart.png"];
heart CGImage ] ; CGImageRef image = [heart CGImage];
/ / The image can now be "rasterized" on a CGContextRef
CGRect ) { 0 , 0 , 100 , 100 } , image ) ; CGContextDrawImage (c, (CGRect) {0, 0, 100, 100}, image);

Animations

1
2
3
4
5
nil context : NULL ] ; [UIView beginAnimations: nil context: NULL];
1.5 ] ; [UIView setAnimationDuration: 1.5];
UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
/ / ...
; [UIView commitAnimations];

NSLog

1
2
3
"NSString object %@ " , myString ) ; NSLog (@ "NSString object% @", myString);
"Float: %f " , myFloat ) ; NSLog (@ "float:% f", myFloat);
"Integer: %i " , myInt ) ; NSLog (@ "Integer:% i", myInt);

Converter RGB to UIColor

1
# Define RGBA (r, g, b, a) [UIColor colorWithRed: r/255.0 green: blue g/255.0: b/255.0 alpha: a]

Passing parameters to a NSTimer

Taking advantage of the parameter userInfo you can send a pointer to an object to the method invoked by our timer.

1
2
3
4
5
6
7
8
9
10
11
12
13
scheduledTimerWithTimeInterval : 1 target : self selector : @selector ( timerMethod ) userInfo : objectPointer repeats : YES ] ; [ NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @ selector (timerMethod) userInfo: objectPointer repeats: YES];

/ / ...

void ) timerMethod : ( NSTimer * ) timer { - (Void) timerMethod: ( NSTimer *) timer {
/ / Retrieve the pointer to my object
timer userInfo ] ; objectPointer = [timer userInfo];
/ / Or
myMethod ] ; [[Timer userInfo] myMethod];
[ [ timer userInfo ] myProperty ] ; int a = [[timer userInfo] myProperty];
/ / Which is the same
[ objectPointer myProperty ] ; int a = [objectPointer myProperty];
}

Execution time

Here's a simple way to calculate short time needed to check the speed of execution of the code:

1
2
3
4
; CFAbsoluteTime initialTime CFAbsoluteTimeGetCurrent = ();
/ / ... code
; CFAbsoluteTime finalTime CFAbsoluteTimeGetCurrent = ();
"Tempo trascorso %f" , finalTime - initialTime ) ; NSLog (@ "Elapsed time% f", finalTime - initialTime);

There are no comments for this post

Leave a comment

TAG XHTML PERMITS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> CODE ENTRY:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL