ios basicscocos2dbook.com/projects/strougo_iosintrotalk_wnotes.pdf · objective-c • c based...

22
IOS BASICS Session 1 of ... If this is your first day learning about or doing iOS development, this is the talk for you

Upload: others

Post on 24-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

IOS BASICSSession 1 of ...

If this is your first day learning about or doing iOS development, this is the talk for you

Page 2: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

AGENDA

• Intro to IOS Development

• Some Objective-C Basics

• A quick demo

•Where to go next and Q&A

Next month we will cover memory management and ARC

Page 3: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

WHO AM I?

• Founder of Prop Group www.prop.gr

• Background in enterprise software, now iPhone+iPad games!

• 2D physics game, Payload in the AppStore

Page 4: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

In Progress...

www.cocos2dbook.comThe book I wrote along with Ray Wenderlich. Covers Cocos2D Objective-C framework for games, along with Box2D and Chipmunk physics.

Page 5: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

SPACE VIKING GAMELearn how to build the Space Viking Game!

www.cocos2dbook.com

Page 6: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

HOW TO GET STARTED

• Step 1: Download Xcode from the Mac AppStore (it is free!)

• Step 2: Learn some basic Objc-C Syntax

• Step 3: Create a HelloWorld

• ....

• Profit?

Now that Xcode is free, there is no reason not to try your hand at iOS Development.

Page 7: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

XCODE AREAS

This is what Xcode IDE looks like. It supports multiple windows, but it is designed to have you work in one large window, with views that can slide in and out. A large resolution display helps.

Page 8: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

OBJECTIVE-C

• C based language with support for Object Oriented Programming (hint: It’s in the name)

•Message passing syntax instead of function syntax

• [object method]; or

• [object methodWithParameter:parameter];

• Your IDE will be Xcode (queue applause)

Think of Objective-C as a wrapper around the C language. It is not as hard as it seems though, Objective-C is a very verbose and easy to read language once you start to get accustomed to it.

Page 9: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

• Your source files are split into Header (.h) and Implementation (.m) files

• Header file contains the interface information

•What your class will do

• Instance variables, public methods*

• Implementation file contains the ...

• How your class will implement the interface

•Most of your code!

OBJECTIVE-C - PART 2

Implementation file contains the implementation of your methods, it is where you will write the code that determines how your classes do everything.The header files declare what is visible to external classes (methods for example). Sort of like a template where the implementation is in the details.

Page 10: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

• As a C function, with no parameters

• fireAllWeapons();

• or in some cases this.fireAllWeapons();

• In Objective-C

• [self fireAllWeapons];

MESSAGE PASSING SYNTAX

In most C based languages we say calling a method or function.In Objective-C we say sending a message.

I’m going to use the Penguin’s cartoon example of trying to implement a method to fire all of their weapons. No Penguins, other animals, or habitats were harmed during the making of this presentation.

Page 11: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

•What about if you want to tell it which weapon to fire

fireWeapon(weaponID); // Some identifier

fireWeapon(10);

• In Objective-C

[self fireWeaponWithID:weaponID];

[self fireWeaponWithID:10];

PASSING 1 PARAMETER

In JavaScript, C, or most other languages you call a method with the parameters between the parentheses.This can get complicated once you have multiple parameters such as:fireWeapon(10,2,’B’,true,false, true);

With Objective-C you have text before each parameter telling you what that parameter is. This is one of the biggest differences syntax wise (that and the square brackets), but it makes your code much more readable.

Page 12: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

fireWeapon(weaponID, weaponsOption);

fireWeapon(10,25);

• In Objective-C

[self fireWeaponWithID:weaponID andWeaponsOption:weaponsOption];

[self fireWeaponWithID:10 andWeaponsOption:20];

WHAT IF YOU HAVE MULTIPLE PARAMETERS?

Again, this is really useful in methods that have many parameters, where they become so easy to read. In Objective-C the method names are interleaved between the parameters.Having the verbose methods also helps when you are using code auto-complete (in Xcode for example).You can read more about the Objective-C language here:

https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html

Page 13: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

• In Objective-C

-(void)fireWeaponWithID:(int)weaponID andWeaponOptions:(int)weaponOptions { NSLog(@"Firing weapon with ID %d and option %d", weaponID, weaponOptions);}

• and to call this method:

[self fireWeaponWithID:10 andWeaponOptions:20];

HOW DO YOU DECLARE AND USE?

This section of the Objective-C Language Reference covers how to correctly and incorrectly name your methods:https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW1

Page 14: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

• In Objective-C

-(BOOL)areWeaponsArmed { // Check if they are armed return YES; }

• and to call this method:

if ([self areWeaponsArmed]) {

NSLog(@"Weapons are armed and ready captain!"); }

WHAT ABOUT RETURN A VALUE

Of course in the real world you would actually want to do a check to see if the weapons are armed, and not just return YES. The if statement here executes if the value is YES (or true). The areWeaponsArmed returns YES, so the NSLog line is executed.

Page 15: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

• You can mix C, C++, and Objective-C

• If your implementation file has C and Objective-C

•Nothing to change, keep the extension .m

• That was hard right?

• If your implementation file has C++ and Objective-C

• Change the extension to .mm

BUT WAIT, THERE IS MORE...

You will run into using C++ if you import a lot of game (for example Physics) libraries.Box2D is one such example, it is written in C++, and you need a few lines of Objective-C++ code to interact with it.

Page 16: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

• A class interface file of “YourClass” showing two instance variables

#import <UIKit/UIKit.h>

@interface YourClass : { NSString *someString; NSString *anotherString;}

@property (strong, nonatomic) NSString *someString;

@end

INSTANCE VARIABLES

The *someString and *anotherString are instance variables.The “*” character also denotes that they are pointers to objects.

If you have a primitive type (not an object), you leave out the “*”For example:float myFloat = 10.2f;int myInt = 8;

Lastly, if you need to store a primitive in an Object (to put it inside of a NSMutableArray for example), use the NSNumber object.NSNumber *myNumber = [NSNumber numberWithInt:myInt]; // Stores the int inside a NSNumber Object

Also see NSValue if you need to store CGPoints inside objects.

Page 17: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

STRONG AND WEAK

• Pointers to objects are strong by default

• Strong increases the pointed object’s retain count

•Weak establishes the link WITHOUT increasing the retain count

• Useful in Parent/Child relationships, and many other cases

You can also use the __strong and __weak keywords before pointer variables definitions to set them as either type.You will see more coverage of this next month when we cover Memory Management and ARC

Page 18: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

WHEN SHOULD YOU USETHE RETAIN COUNT?

•When should you use -retainCount ?

http://whentouseretaincount.com/

Avoid the temptation to use the -retainCount property of Objects.The site has some useful links on the topic

Page 19: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

DEMO TIME

•Demo of method calling

•Demo of a basic app (URL BELOW)

https://developer.apple.com/library/ios/#referencelibrary/GettingStarted/RoadMapiOS/JumpRightIn/iPhone101/Articles/00_Introduction.html#//apple_ref/doc/uid/TP40011343-TP40007514-CH1-SW1

Here is the URL: https://developer.apple.com/library/ios/#referencelibrary/GettingStarted/RoadMapiOS/JumpRightIn/iPhone101/Articles/00_Introduction.html#//apple_ref/doc/uid/TP40011343-TP40007514-CH1-SW1

It will take you 10-15 minutes, and you will have your first iOS App. Really, you can do it on your lunch break :-)

Page 20: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

WHERE TO GO FROM HERE

PlusiTunes UVideos

and more

Here are some of the books I found useful:Aaron Hillegass Objective-C Programming:http://www.amazon.com/gp/product/0321706285/ref=as_li_ss_tl?ie=UTF8&tag=cocos2dbookco-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0321706285

Beginning iOS 5 Development:http://www.amazon.com/gp/product/1430236051/ref=as_li_ss_tl?ie=UTF8&tag=cocos2dbookco-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=1430236051

Learning Objective-C 2.0:http://www.amazon.com/gp/product/0321711386/ref=as_li_ss_tl?ie=UTF8&tag=cocos2dbookco-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0321711386

Learning iPad Programming:http://www.amazon.com/gp/product/0321750403/ref=as_li_ss_tl?ie=UTF8&tag=cocos2dbookco-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0321750403

And here are some sites to brush up on Objective-C:http://cocoadevcentral.com/d/learn_objectivec/and the C language:http://cocoadevcentral.com/articles/000081.php

Page 21: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

THANK YOU

[email protected]

• twitter.com/rodstrougo

• www.prop.gr

• www.cocos2dbook.com

Thank you guys for allowing me to present.

Page 22: IOS BASICScocos2dbook.com/projects/Strougo_iOSIntroTalk_wNotes.pdf · OBJECTIVE-C • C based language with support for Object Oriented Programming (hint: It’s in the name) •

MORE DETAILS ON THE BLOG

http://cocos2dbook.com

This presentation and others will be there.