SlideShare a Scribd company logo
1 of 21
Download to read offline
My Favourite Ten Xcode Things


Tuesday, October 11, 11

Some of my favourite Xcode and Objective-C type things.
(Or: Please tell me a better way of
                                  doing these things)


Tuesday, October 11, 11

Three phases of iPhone development:

1. Argh! Square brackets!
2. Ok, how do I do this?
3. Ok, what’s the BEST way to do this?

The common theme is that each stage is full of puzzles, and puzzles are attractive to certain
types of brains. You think, you solve, you get a little rush. Addictive!
1. Xcode Keyboard Shortcuts

                          Flip between .h and .m

                             Control + Command + Arrow up

                          Switch between open tabs

                             Shift + Command + [ or ]

                          Find anywhere in project

                             Shift + Command + F

Tuesday, October 11, 11

I spend many hours using Xcode every day. The transition to Xcode 4 wasn’t too bad,
although it took a while to get used to the way it splits views. I’m happy enough now though.
At least it’s not Eclipse, and when I go back to Visual Studio it irritates me that I need to
double-click on things a lot.
1b. Xcode Keyboard Shortcuts



                          Toggle Debug Console

                             Shift + Command + Y

                          Open a file

                             Shift + Command + O



Tuesday, October 11, 11

Open a file is fantastic. Use it.
2. Drag from the NIB editor into .h files




                          Hold down Control and drag a control into
                          your .h file, to create actions and properties
                          (and the associated methods in .m).




Tuesday, October 11, 11

I love this.
3. Useful NSString initialization



                          -(NSString *)getAxis
                {
                ! return [NSString stringWithFormat:
                       @"X:%0.2f Y:%0.2f Z:%0.2f",
                       _accelerometer[0],
                       _accelerometer[1],
                       _accelerometer[2]];
                }




Tuesday, October 11, 11

Very nice - and because there is no ALLOC, it’s an autorelease object.
Just don’t put a BOOL in there.
3b. Useful NSString initialization




                          BOOL myStatus = TRUE;

                  -(NSString *)getStatus
                  {
                !   return [NSString stringWithFormat:
                       @"%@",(myStatus ? @”YES” : @”NO”)];
                  }




Tuesday, October 11, 11

Handy for NSLog too.
4. Useful NSString comparisons

                          Don’t check to see if a string is empty like
                          this:
                          if (myString == nil)   { NSLog(@"empty!"); }



                          Use this:

                          if ([myString isEqualToString:@""]) { NSLog(@"empty!"); }


                          Or this:
                          if ([myString length] == 0) { NSLog(@"empty!"); }




Tuesday, October 11, 11

But if it’s just whitespace, that might not work..
4b. Useful NSString comparisons



                          //	
  Mr.	
  Will	
  Shipley
                static	
  inline	
  BOOL	
  isEmpty(id	
  thing)	
  {
                	
  	
  	
  	
  return	
  thing	
  ==	
  nil
                	
  	
  	
  	
  ||	
  [thing	
  isKindOfClass:[NSNull	
  class]]
                	
  	
  	
  	
  ||	
  ([thing	
  respondsToSelector:@selector(length)]
                	
  	
  	
  	
  	
  	
  	
  	
  &&	
  [(NSData	
  *)thing	
  length]	
  ==	
  0)
                	
  	
  	
  	
  ||	
  ([thing	
  respondsToSelector:@selector(count)]
                	
  	
  	
  	
  	
  	
  	
  	
  &&	
  [(NSArray	
  *)thing	
  count]	
  ==	
  0);
                }




Tuesday, October 11, 11

Here’s the 100% best way.
5. Testing to see if a file exists



                          There is no file system. Except there is.



                    if ([[NSFileManager defaultManager]
                         fileExistsAtPath:[[NSBundle mainBundle]
                         pathForResource:@"picture" ofType:@"jpg"]])

                             return YES;




Tuesday, October 11, 11

Also, are you using large image files? Tempted to stick with PNG? Try JPG instead.
Same quality in photograph images, but can be 1/10th of the size.
6. Time delay before method call

                          -(void) myMethod
                {

                }

                [self performSelector:@selector(myMethod)
                        withObject: nil
                        afterDelay: 1.0];




                [NSObject
                cancelPreviousPerformRequestsWithTarget              :self
                        selector :@selector(myMethod)
                        object :nil];


Tuesday, October 11, 11

Multithreading for dummies - a great way to get animation working when the main thread
would otherwise be blocked. WARNING! Remember to cancel it if you don’t use it before the
reference goes away!
7. Delegate callback to parent AppDelegate

                   // In AppDelegate
                -(void)callMe
                {


                }



                // In some other class
                - (void)viewDidLoad
                {
                    [super viewDidLoad];

                          [(AppDelegate*)
                           [[UIApplication sharedApplication] delegate] callMe];

                }




Tuesday, October 11, 11

Just need to add callMe to .h in AppDelegate, and #import that into the other class.
However, think about why you are doing this: it’s very likely that a Singleton will be a better
idea.
8. Using delegates to perform actions when
                modal dialog closed.
                          // In the Modal Dialog UIViewController Class, .h
                          file

                @interface CityPickerViewController : UIViewController
                <UIPickerViewDelegate>

                {
                !
                ! id delegate;
                !
                }

                - (id)delegate;
                - (void)setDelegate:(id)newDelegate;

                @end




Tuesday, October 11, 11

So you want to trigger something when a modal dialog is closed. Use this to allow the Modal
dialog UIViewController class to call a method back in the class that called it.
8b. Using delegates to perform actions
                when modal dialog closed.
                          // In the Modal Dialog UIViewController Class, .m file

                - (id)delegate {
                    return delegate;
                }

                - (void)setDelegate:(id)newDelegate {
                    delegate = newDelegate;
                }

                -(void) updateCity: (id)sender
                {
                ! // replaced by delegated call
                !
                }

                -(void)callWhenDialogClosing
                {
                ! [[self delegate] updateCity:self];!
                }




Tuesday, October 11, 11

So you want to trigger something when a modal dialog is closed. Use this to allow the Modal
dialog UIViewController class to call a method back in the class that called it.
8b. Using delegates to perform actions
                when modal dialog closed.
                          // In the class that is calling the modal dialog

                -(void) updateCity: (id)sender
                {
                ! [self drawLocation];!
                  [myPickerView dismissModalViewControllerAnimated:YES];
                }


                -(IBAction) clickNearestCity: (id)sender
                {
                     myPickerView = [[CityPickerViewController alloc]
                initWithNibName:@"CityPickerViewController" bundle:nil];


                ! myPickerView.delegate = self;
                     ....




Tuesday, October 11, 11

You can’t keep the pointer to the view nice and local now, as you’ll need to close it yourself in
the delegate. Yes, it smells a bit. Suggestions welcome.
9. Two-part animation block



                [UIView beginAnimations:@"clickBounce" context:tempButton];

                [UIView setAnimationDelegate:self];

                [UIView setAnimationDuration:0.2]; !

                [UIView setAnimationDidStopSelector:@selector(part2:finished:context:)];

                [myButton setTransform:CGAffineTransformMakeScale(.9,.9)];

                [UIView commitAnimations];




Tuesday, October 11, 11

Blocks have a terribly ugly syntax, but if you can get over it, they are useful. And increasing
in importance. If you wanted an animation to have multiple parts, you needed to use the
DidStopSelector, and it was a pain.
9b. Two-part animation block


                [UIView animateWithDuration:1.0

                          animations:^
                          {
                               myButton.alpha = 1.0;
                               myButton.transform = CGAffineTransformMakeScale(1.5, 1.5);
                          }

                          completion:^(BOOL completed)
                          {
                               myButton.alpha = 0.8;
                               myButton.transform = CGAffineTransformMakeScale(1, 1);
                          }
                ];




Tuesday, October 11, 11

Much neater, no extra methods, easier to pass values between different components of the
animation etc etc
10. UICollections, Tags and iOS 5



                    Declare a Collection, and then link all your
                    buttons to it..
                          IBOutletCollection(UIButton) NSArray *menuButtonsCollection;




Tuesday, October 11, 11

Piece of cake in NIB editor.
10b. UICollections, Tags and iOS 5


                    Now you can iterate over them all..
              for (UIButton *button in menuButtonsCollection)
               {
                     [button setImage:[UIImage imageNamed:@"pic.png"]
                             forState:UIControlStateNormal];
               }




Tuesday, October 11, 11

Handy to programmatically fade out all buttons for example.
Remember you can also set TAGS in the NIB editor, and then act on those in the loop.
You don’t need to only use one type of object, use (id) for any control.
10b. UICollections, Tags and iOS 5



                  If you need to change them all at once..
                  [[UISwitch appearance] setOnTintColor: [UIColor redColor]];




Tuesday, October 11, 11

Note: need to re-open the view to see the changes.
johntkennedy@gmail.com


Tuesday, October 11, 11

More Related Content

What's hot

Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascriptDoeun KOCH
 
5 Tips for Better JavaScript
5 Tips for Better JavaScript5 Tips for Better JavaScript
5 Tips for Better JavaScriptTodd Anglin
 
React Native Evening
React Native EveningReact Native Evening
React Native EveningTroy Miles
 
Aplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & JetpackAplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & JetpackNelson Glauber Leal
 
Javascript basics for automation testing
Javascript  basics for automation testingJavascript  basics for automation testing
Javascript basics for automation testingVikas Thange
 
Little Helpers for Android Development with Kotlin
Little Helpers for Android Development with KotlinLittle Helpers for Android Development with Kotlin
Little Helpers for Android Development with KotlinKai Koenig
 
Kotlin Delegates in practice - Kotlin community conf
Kotlin Delegates in practice - Kotlin community confKotlin Delegates in practice - Kotlin community conf
Kotlin Delegates in practice - Kotlin community confFabio Collini
 
Reactive Programming with JavaScript
Reactive Programming with JavaScriptReactive Programming with JavaScript
Reactive Programming with JavaScriptCodemotion
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJWORKS powered by Ordina
 
Mastering Kotlin Standard Library
Mastering Kotlin Standard LibraryMastering Kotlin Standard Library
Mastering Kotlin Standard LibraryNelson Glauber Leal
 
Solid Software Design Principles
Solid Software Design PrinciplesSolid Software Design Principles
Solid Software Design PrinciplesJon Kruger
 
Powerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesPowerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesDragos Ionita
 
Node Boot Camp
Node Boot CampNode Boot Camp
Node Boot CampTroy Miles
 
Aplicações Assíncronas no Android com Coroutines e Jetpack
Aplicações Assíncronas no Android com Coroutines e JetpackAplicações Assíncronas no Android com Coroutines e Jetpack
Aplicações Assíncronas no Android com Coroutines e JetpackNelson Glauber Leal
 
Automatic Reference Counting @ Pragma Night
Automatic Reference Counting @ Pragma NightAutomatic Reference Counting @ Pragma Night
Automatic Reference Counting @ Pragma NightGiuseppe Arici
 
運用Closure Compiler 打造高品質的JavaScript
運用Closure Compiler 打造高品質的JavaScript運用Closure Compiler 打造高品質的JavaScript
運用Closure Compiler 打造高品質的JavaScripttaobao.com
 
Kotlin delegates in practice - Kotlin Everywhere Stockholm
Kotlin delegates in practice - Kotlin Everywhere StockholmKotlin delegates in practice - Kotlin Everywhere Stockholm
Kotlin delegates in practice - Kotlin Everywhere StockholmFabio Collini
 
Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6Ray Ploski
 

What's hot (20)

Let's JavaScript
Let's JavaScriptLet's JavaScript
Let's JavaScript
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascript
 
5 Tips for Better JavaScript
5 Tips for Better JavaScript5 Tips for Better JavaScript
5 Tips for Better JavaScript
 
Objective-C for Beginners
Objective-C for BeginnersObjective-C for Beginners
Objective-C for Beginners
 
React Native Evening
React Native EveningReact Native Evening
React Native Evening
 
Aplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & JetpackAplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & Jetpack
 
Javascript basics for automation testing
Javascript  basics for automation testingJavascript  basics for automation testing
Javascript basics for automation testing
 
Little Helpers for Android Development with Kotlin
Little Helpers for Android Development with KotlinLittle Helpers for Android Development with Kotlin
Little Helpers for Android Development with Kotlin
 
Kotlin Delegates in practice - Kotlin community conf
Kotlin Delegates in practice - Kotlin community confKotlin Delegates in practice - Kotlin community conf
Kotlin Delegates in practice - Kotlin community conf
 
Reactive Programming with JavaScript
Reactive Programming with JavaScriptReactive Programming with JavaScript
Reactive Programming with JavaScript
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
 
Mastering Kotlin Standard Library
Mastering Kotlin Standard LibraryMastering Kotlin Standard Library
Mastering Kotlin Standard Library
 
Solid Software Design Principles
Solid Software Design PrinciplesSolid Software Design Principles
Solid Software Design Principles
 
Powerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesPowerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best Practices
 
Node Boot Camp
Node Boot CampNode Boot Camp
Node Boot Camp
 
Aplicações Assíncronas no Android com Coroutines e Jetpack
Aplicações Assíncronas no Android com Coroutines e JetpackAplicações Assíncronas no Android com Coroutines e Jetpack
Aplicações Assíncronas no Android com Coroutines e Jetpack
 
Automatic Reference Counting @ Pragma Night
Automatic Reference Counting @ Pragma NightAutomatic Reference Counting @ Pragma Night
Automatic Reference Counting @ Pragma Night
 
運用Closure Compiler 打造高品質的JavaScript
運用Closure Compiler 打造高品質的JavaScript運用Closure Compiler 打造高品質的JavaScript
運用Closure Compiler 打造高品質的JavaScript
 
Kotlin delegates in practice - Kotlin Everywhere Stockholm
Kotlin delegates in practice - Kotlin Everywhere StockholmKotlin delegates in practice - Kotlin Everywhere Stockholm
Kotlin delegates in practice - Kotlin Everywhere Stockholm
 
Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6
 

Similar to My Favourite 10 Things about Xcode/ObjectiveC

Five class-based views everyone has written by now
Five class-based views everyone has written by nowFive class-based views everyone has written by now
Five class-based views everyone has written by nowJames Aylett
 
Grand Central Dispatch Design Patterns
Grand Central Dispatch Design PatternsGrand Central Dispatch Design Patterns
Grand Central Dispatch Design PatternsRobert Brown
 
Learning iOS and hunting NSZombies in 3 weeks
Learning iOS and hunting NSZombies in 3 weeksLearning iOS and hunting NSZombies in 3 weeks
Learning iOS and hunting NSZombies in 3 weeksCalvin Cheng
 
Scalable JavaScript Design Patterns
Scalable JavaScript Design PatternsScalable JavaScript Design Patterns
Scalable JavaScript Design PatternsAddy Osmani
 
JavaScript 101
JavaScript 101JavaScript 101
JavaScript 101ygv2000
 
Jquery Plugin
Jquery PluginJquery Plugin
Jquery PluginRavi Mone
 
Ios development
Ios developmentIos development
Ios developmentelnaqah
 
So, you think you know widgets.
So, you think you know widgets.So, you think you know widgets.
So, you think you know widgets.danielericlee
 
The Ring programming language version 1.2 book - Part 5 of 84
The Ring programming language version 1.2 book - Part 5 of 84The Ring programming language version 1.2 book - Part 5 of 84
The Ring programming language version 1.2 book - Part 5 of 84Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 14 of 185
The Ring programming language version 1.5.4 book - Part 14 of 185The Ring programming language version 1.5.4 book - Part 14 of 185
The Ring programming language version 1.5.4 book - Part 14 of 185Mahmoud Samir Fayed
 
Practical tips for building apps with kotlin
Practical tips for building apps with kotlinPractical tips for building apps with kotlin
Practical tips for building apps with kotlinAdit Lal
 
Save time with kotlin in android development
Save time with kotlin in android developmentSave time with kotlin in android development
Save time with kotlin in android developmentAdit Lal
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)Piyush Katariya
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends旻琦 潘
 
Automation with Ansible and Containers
Automation with Ansible and ContainersAutomation with Ansible and Containers
Automation with Ansible and ContainersRodolfo Carvalho
 
iOS Selectors Blocks and Delegation
iOS Selectors Blocks and DelegationiOS Selectors Blocks and Delegation
iOS Selectors Blocks and DelegationJussi Pohjolainen
 

Similar to My Favourite 10 Things about Xcode/ObjectiveC (20)

Five class-based views everyone has written by now
Five class-based views everyone has written by nowFive class-based views everyone has written by now
Five class-based views everyone has written by now
 
Grand Central Dispatch Design Patterns
Grand Central Dispatch Design PatternsGrand Central Dispatch Design Patterns
Grand Central Dispatch Design Patterns
 
Learning iOS and hunting NSZombies in 3 weeks
Learning iOS and hunting NSZombies in 3 weeksLearning iOS and hunting NSZombies in 3 weeks
Learning iOS and hunting NSZombies in 3 weeks
 
Scalable JavaScript Design Patterns
Scalable JavaScript Design PatternsScalable JavaScript Design Patterns
Scalable JavaScript Design Patterns
 
JavaScript 101
JavaScript 101JavaScript 101
JavaScript 101
 
Jquery Plugin
Jquery PluginJquery Plugin
Jquery Plugin
 
Ios development
Ios developmentIos development
Ios development
 
Thinking In Swift
Thinking In SwiftThinking In Swift
Thinking In Swift
 
So, you think you know widgets.
So, you think you know widgets.So, you think you know widgets.
So, you think you know widgets.
 
The Ring programming language version 1.2 book - Part 5 of 84
The Ring programming language version 1.2 book - Part 5 of 84The Ring programming language version 1.2 book - Part 5 of 84
The Ring programming language version 1.2 book - Part 5 of 84
 
03 objective-c session 3
03  objective-c session 303  objective-c session 3
03 objective-c session 3
 
The Ring programming language version 1.5.4 book - Part 14 of 185
The Ring programming language version 1.5.4 book - Part 14 of 185The Ring programming language version 1.5.4 book - Part 14 of 185
The Ring programming language version 1.5.4 book - Part 14 of 185
 
Rubymotion talk
Rubymotion talkRubymotion talk
Rubymotion talk
 
Practical tips for building apps with kotlin
Practical tips for building apps with kotlinPractical tips for building apps with kotlin
Practical tips for building apps with kotlin
 
Save time with kotlin in android development
Save time with kotlin in android developmentSave time with kotlin in android development
Save time with kotlin in android development
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends
 
Automation with Ansible and Containers
Automation with Ansible and ContainersAutomation with Ansible and Containers
Automation with Ansible and Containers
 
iOS Selectors Blocks and Delegation
iOS Selectors Blocks and DelegationiOS Selectors Blocks and Delegation
iOS Selectors Blocks and Delegation
 
Testable Javascript
Testable JavascriptTestable Javascript
Testable Javascript
 

Recently uploaded

ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsManeerUddin
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 

Recently uploaded (20)

ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture hons
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 

My Favourite 10 Things about Xcode/ObjectiveC

  • 1. My Favourite Ten Xcode Things Tuesday, October 11, 11 Some of my favourite Xcode and Objective-C type things.
  • 2. (Or: Please tell me a better way of doing these things) Tuesday, October 11, 11 Three phases of iPhone development: 1. Argh! Square brackets! 2. Ok, how do I do this? 3. Ok, what’s the BEST way to do this? The common theme is that each stage is full of puzzles, and puzzles are attractive to certain types of brains. You think, you solve, you get a little rush. Addictive!
  • 3. 1. Xcode Keyboard Shortcuts Flip between .h and .m Control + Command + Arrow up Switch between open tabs Shift + Command + [ or ] Find anywhere in project Shift + Command + F Tuesday, October 11, 11 I spend many hours using Xcode every day. The transition to Xcode 4 wasn’t too bad, although it took a while to get used to the way it splits views. I’m happy enough now though. At least it’s not Eclipse, and when I go back to Visual Studio it irritates me that I need to double-click on things a lot.
  • 4. 1b. Xcode Keyboard Shortcuts Toggle Debug Console Shift + Command + Y Open a file Shift + Command + O Tuesday, October 11, 11 Open a file is fantastic. Use it.
  • 5. 2. Drag from the NIB editor into .h files Hold down Control and drag a control into your .h file, to create actions and properties (and the associated methods in .m). Tuesday, October 11, 11 I love this.
  • 6. 3. Useful NSString initialization -(NSString *)getAxis { ! return [NSString stringWithFormat: @"X:%0.2f Y:%0.2f Z:%0.2f", _accelerometer[0], _accelerometer[1], _accelerometer[2]]; } Tuesday, October 11, 11 Very nice - and because there is no ALLOC, it’s an autorelease object. Just don’t put a BOOL in there.
  • 7. 3b. Useful NSString initialization BOOL myStatus = TRUE; -(NSString *)getStatus { ! return [NSString stringWithFormat: @"%@",(myStatus ? @”YES” : @”NO”)]; } Tuesday, October 11, 11 Handy for NSLog too.
  • 8. 4. Useful NSString comparisons Don’t check to see if a string is empty like this: if (myString == nil) { NSLog(@"empty!"); } Use this: if ([myString isEqualToString:@""]) { NSLog(@"empty!"); } Or this: if ([myString length] == 0) { NSLog(@"empty!"); } Tuesday, October 11, 11 But if it’s just whitespace, that might not work..
  • 9. 4b. Useful NSString comparisons //  Mr.  Will  Shipley static  inline  BOOL  isEmpty(id  thing)  {        return  thing  ==  nil        ||  [thing  isKindOfClass:[NSNull  class]]        ||  ([thing  respondsToSelector:@selector(length)]                &&  [(NSData  *)thing  length]  ==  0)        ||  ([thing  respondsToSelector:@selector(count)]                &&  [(NSArray  *)thing  count]  ==  0); } Tuesday, October 11, 11 Here’s the 100% best way.
  • 10. 5. Testing to see if a file exists There is no file system. Except there is. if ([[NSFileManager defaultManager] fileExistsAtPath:[[NSBundle mainBundle] pathForResource:@"picture" ofType:@"jpg"]]) return YES; Tuesday, October 11, 11 Also, are you using large image files? Tempted to stick with PNG? Try JPG instead. Same quality in photograph images, but can be 1/10th of the size.
  • 11. 6. Time delay before method call -(void) myMethod { } [self performSelector:@selector(myMethod) withObject: nil afterDelay: 1.0]; [NSObject cancelPreviousPerformRequestsWithTarget :self selector :@selector(myMethod) object :nil]; Tuesday, October 11, 11 Multithreading for dummies - a great way to get animation working when the main thread would otherwise be blocked. WARNING! Remember to cancel it if you don’t use it before the reference goes away!
  • 12. 7. Delegate callback to parent AppDelegate // In AppDelegate -(void)callMe { } // In some other class - (void)viewDidLoad { [super viewDidLoad]; [(AppDelegate*) [[UIApplication sharedApplication] delegate] callMe]; } Tuesday, October 11, 11 Just need to add callMe to .h in AppDelegate, and #import that into the other class. However, think about why you are doing this: it’s very likely that a Singleton will be a better idea.
  • 13. 8. Using delegates to perform actions when modal dialog closed. // In the Modal Dialog UIViewController Class, .h file @interface CityPickerViewController : UIViewController <UIPickerViewDelegate> { ! ! id delegate; ! } - (id)delegate; - (void)setDelegate:(id)newDelegate; @end Tuesday, October 11, 11 So you want to trigger something when a modal dialog is closed. Use this to allow the Modal dialog UIViewController class to call a method back in the class that called it.
  • 14. 8b. Using delegates to perform actions when modal dialog closed. // In the Modal Dialog UIViewController Class, .m file - (id)delegate { return delegate; } - (void)setDelegate:(id)newDelegate { delegate = newDelegate; } -(void) updateCity: (id)sender { ! // replaced by delegated call ! } -(void)callWhenDialogClosing { ! [[self delegate] updateCity:self];! } Tuesday, October 11, 11 So you want to trigger something when a modal dialog is closed. Use this to allow the Modal dialog UIViewController class to call a method back in the class that called it.
  • 15. 8b. Using delegates to perform actions when modal dialog closed. // In the class that is calling the modal dialog -(void) updateCity: (id)sender { ! [self drawLocation];! [myPickerView dismissModalViewControllerAnimated:YES]; } -(IBAction) clickNearestCity: (id)sender { myPickerView = [[CityPickerViewController alloc] initWithNibName:@"CityPickerViewController" bundle:nil]; ! myPickerView.delegate = self; .... Tuesday, October 11, 11 You can’t keep the pointer to the view nice and local now, as you’ll need to close it yourself in the delegate. Yes, it smells a bit. Suggestions welcome.
  • 16. 9. Two-part animation block [UIView beginAnimations:@"clickBounce" context:tempButton]; [UIView setAnimationDelegate:self]; [UIView setAnimationDuration:0.2]; ! [UIView setAnimationDidStopSelector:@selector(part2:finished:context:)]; [myButton setTransform:CGAffineTransformMakeScale(.9,.9)]; [UIView commitAnimations]; Tuesday, October 11, 11 Blocks have a terribly ugly syntax, but if you can get over it, they are useful. And increasing in importance. If you wanted an animation to have multiple parts, you needed to use the DidStopSelector, and it was a pain.
  • 17. 9b. Two-part animation block [UIView animateWithDuration:1.0 animations:^ { myButton.alpha = 1.0; myButton.transform = CGAffineTransformMakeScale(1.5, 1.5); } completion:^(BOOL completed) { myButton.alpha = 0.8; myButton.transform = CGAffineTransformMakeScale(1, 1); } ]; Tuesday, October 11, 11 Much neater, no extra methods, easier to pass values between different components of the animation etc etc
  • 18. 10. UICollections, Tags and iOS 5 Declare a Collection, and then link all your buttons to it.. IBOutletCollection(UIButton) NSArray *menuButtonsCollection; Tuesday, October 11, 11 Piece of cake in NIB editor.
  • 19. 10b. UICollections, Tags and iOS 5 Now you can iterate over them all.. for (UIButton *button in menuButtonsCollection) { [button setImage:[UIImage imageNamed:@"pic.png"] forState:UIControlStateNormal]; } Tuesday, October 11, 11 Handy to programmatically fade out all buttons for example. Remember you can also set TAGS in the NIB editor, and then act on those in the loop. You don’t need to only use one type of object, use (id) for any control.
  • 20. 10b. UICollections, Tags and iOS 5 If you need to change them all at once.. [[UISwitch appearance] setOnTintColor: [UIColor redColor]]; Tuesday, October 11, 11 Note: need to re-open the view to see the changes.