Objective-C. Stanford CS193p Fall 2013

Similar documents
Stanford CS193p. Developing Applications for ios Fall Stanford CS193p. Fall 2013

Review (Basic Objective-C)

Objective-C. Deck.m. Deck.h. Let s look at another class. This one represents a deck of cards. #import <Foundation/Foundation.h> #import "Deck.

CS193p Spring 2010 Thursday, April 29, 2010

ITP 342 Mobile App Dev. Fundamentals

Mobile Application Development

Developing Applications for ios

Introductory ios Development

Stanford CS193p. Developing Applications for ios. Spring Stanford CS193p. Spring 2012

JAVA An overview for C++ programmers

ios: Objective-C Primer

ITP 342 Advanced Mobile App Dev. Memory

Mobile Application Programming. Objective-C Classes

Review. iphone Application Programming Lecture 2: Objective-C, Cocoa. History. Objective-C. Device restrictions. Interaction paradigm changes

ITP 342 Mobile App Dev. Fundamentals

Mobile Application Development

Stanford CS193p. Developing Applications for ios. Spring CS193p. Spring 2016

Objective-C and COCOA Applications

Assignment II: Foundation Calculator

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Type Checking and Type Equality

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

Objective-C ICT/7421ICTNathan. René Hexel. School of Information and Communication Technology Griffith University.

Chapter 1 Getting Started

Intro. Scheme Basics. scm> 5 5. scm>

Stanford CS193p. Developing Applications for iphone 4, ipod Touch, & ipad Spring Stanford CS193p Spring 2011


Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Dynamic Memory Allocation

CSE 333 Midterm Exam 7/29/13

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

IPHONE DEVELOPMENT. Getting Started with the iphone SDK

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Review. iphone Application Programming Lecture 2: Objective-C, Cocoa. History. Objective-C. Device restrictions. Interaction paradigm changes

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

CS 47. Beginning iphone Application Development

Rules and syntax for inheritance. The boring stuff

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Binghamton University. CS-211 Fall Object Orientation

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #49. Structures in C -1

6. Pointers, Structs, and Arrays. 1. Juli 2011

Introductory ios Development

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

Modular Programming. Prof. Clarkson Fall Today s music: "Giorgio By Moroder" by Daft Punk

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

377 Student Guide to C++

Thursday, February 16, More C++ and root

The Dynamic Typing Interlude

Lecture 2, September 4

Questions. Exams: no. Get by without own Mac? Why ios? ios vs Android restrictions. Selling in App store how hard to publish? Future of Objective-C?

Design Phase. Create a class Person. Determine the superclass. NSObject (in this case)

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

Project. C++: Inheritance III. Plan. Project. Before we begin. The final exam. Advanced Topics. Project. This week in the home stretch

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Introduce C# as Object Oriented programming language. Explain, tokens,

CS Summer 2013

Shorthand for values: variables

iphone Programming Patrick H. Madden SUNY Binghamton Computer Science Department

Static Methods. Why use methods?

Operator overloading

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

Motivation was to facilitate development of systems software, especially OS development.

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

CS193P - Lecture 3. iphone Application Development. Custom Classes Object Lifecycle Autorelease Properties

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

1.The basics 5 Comments 5 Variables and basic types 6 Operators 8 Object variables 10 A note on prefixes and namespaces 11

CS 11 Haskell track: lecture 1

CS1004: Intro to CS in Java, Spring 2005

Project 1: Scheme Pretty-Printer

C++ basics Getting started with, and Data Types.

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Lab # 02. Basic Elements of C++ _ Part1

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

CS558 Programming Languages


Objective-C Part 2 (ECS189H) Ken Joy Serban Porumbescu

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

OVERVIEW. Why learn ios programming? Share first-hand experience. Identify platform differences. Identify similarities with.net

ITP 342 Mobile App Dev. Code

Operational Semantics. One-Slide Summary. Lecture Outline

Principles of Programming Languages. Objective-C. Joris Kluivers

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Stanford CS193p. Developing Applications for ios. Winter CS193p. Winter 2017

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Variables and Functions. ROBOTC Software

Week 8: Operator overloading

CS193E Lecture #3 Categories and Protocols Cocoa Memory Management

Topic Notes: Bits and Bytes and Numbers

Lambdas and Generics (Intro)

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Java Threads and intrinsic locks

Transcription:

New language to learn! Strict superset of C Adds syntax for classes, methods, etc. A few things to think differently about (e.g. properties, dynamic binding) Most important concept to understand today: Properties Usually we do not access instance variables directly in. Instead, we use properties. A property is just the combination of a getter method and a setter method in a class. The getter (usually) has the name of the property (e.g. myvalue ) The setter s name is set plus capitalized property name (e.g. setmyvalue: ) (To make this look nice, we always use a lowercase letter as the first letter of a property name.) We just call the setter to store the value we want and the getter to get it. Simple. This is just your first glimpse of this language! We ll go much more into the details next week. Don t get too freaked out by the syntax at this point.

Public Declarations Private Implementation

Its superclass. The name of this class. NSObject is the root class from which pretty much all ios classes inherit (including the classes you author yourself). Don t forget this!

Note, superclass is not specified here.

#import <Foundation/NSObject.h> Superclass s header file.

If the superclass is in ios itself, we import the entire framework that includes the superclass. In this case, Foundation, which contains basic non-ui objects like NSObject.

@import Foundation; In fact, in ios 7 (only), there is special syntax for importing an entire framework called @import.

However, the old framework importing syntax is backwards-compatible in ios 7.

Our own header file must be imported into our implementation file.

Private declarations can go here.

@property (strong) NSString *contents; In ios, we don t access instance variables directly. Instead, we use an @property which declares two methods: a setter and a getter. It is with those two methods that the @property s instance variable is accessed (both publicly and privately). This particular @property is a pointer. Specifically, a pointer to an object whose class is (or inherits from) NSString. ALL objects live in the heap (i.e. are pointed to) in! Thus you would never have a property of type NSString (rather, NSString * ). Because this @property is in this class s header file, it is public. Its setter and getter can be called from outside this class s @implementation block.

@property (strong) NSString *contents; strong means: keep the object that this property points to in memory until I set this property to nil (zero) (and it will stay in memory until everyone who has a strong pointer to it sets their property to nil too) weak would mean: if no one else has a strong pointer to this object, then you can throw it out of memory and set this property to nil (this can happen at any time)

@property (strong, nonatomic) NSString *contents; nonatomic means: access to this property is not thread-safe. We will always specify this for object pointers in this course. If you do not, then the compiler will generate locking code that will complicate your code elsewhere.

This @synthesize is the line of code that actually creates the backing instance variable that is set and gotten. Notice that by default the backing variable s name is the same as the property s name but with an underbar in front. @property (strong, nonatomic ) NSString *contents; @synthesize contents = _contents; This is the @property implementation that the compiler generates automatically for you (behind the scenes). You are welcome to write the setter or getter yourself, but this would only be necessary if you needed to do something in addition to simply setting or getting the value of the property. - (NSString *)contents return _contents; - (void)setcontents:(nsstring *)contents _contents = contents;

@property (strong, nonatomic) NSString *contents; Because the compiler takes care of everything you need to implement a property, it s usually only one line of code (the @property declaration) to add one to your class.

Notice no strong or weak here. Primitive types are not stored in the heap, so there s no need to specify how the storage for them in the heap is treated. @property (strong, nonatomic) NSString *contents; @property (nonatomic) BOOL chosen; @property (nonatomic) BOOL matched; Properties can be any C type. That includes int, float, etc., even C structs. C does not define a boolean type. This BOOL is an typedef. It s values are YES or NO. Let s look at some more properties. These are not pointers. They are simple BOOLs.

@property (strong, nonatomic) NSString *contents; @property (nonatomic) BOOL chosen; @property (nonatomic) BOOL matched; @synthesize chosen = _chosen; @synthesize matched = _matched; - (BOOL) chosen return _chosen; - (void)setchosen:(bool)chosen _chosen = chosen; - (BOOL) matched return _matched; - (void)setmatched:(bool)matched _matched = matched; Here s what the compiler is doing behind the scenes for these two properties.

It is actually possible to change the name of the getter that is generated. The only time you ll ever see that done in this class (or anywhere probably) is boolean getters. @property (strong, nonatomic) NSString *contents; @property (nonatomic, getter=ischosen) BOOL chosen; @property (nonatomic, getter=ismatched) BOOL matched; This is done simply to make the code read a little bit nicer. You ll see this in action later. @synthesize chosen = _chosen; @synthesize matched = _matched; - (BOOL) ischosen return _chosen; - (void)setchosen:(bool)chosen _chosen = chosen; - (BOOL) ismatched return _matched; - (void)setmatched:(bool)matched _matched = matched; Note change in getter method. Note change in getter method.

@property (strong, nonatomic) NSString *contents; @property (nonatomic, getter=ischosen) BOOL chosen; @property (nonatomic, getter=ismatched) BOOL matched; Remember, unless you need to do something besides setting or getting when a property is being set or gotten, the implementation side of this will all happen automatically for you.

@property (strong, nonatomic) NSString *contents; Enough properties for now. Let s take a look at defining methods. @property (nonatomic, getter=ischosen) BOOL chosen; @property (nonatomic, getter=ismatched) BOOL matched; - (int)match:(card *)card; Here s the declaration of a public method called match: which takes one argument (a pointer to a Card) and returns an integer. What makes this method public? Because we ve declared it in the header file.

@property (strong, nonatomic) NSString *contents; @property (nonatomic, getter=ischosen) BOOL chosen; @property (nonatomic, getter=ismatched) BOOL matched; - (int)match:(card *)card; - (int)match:(card *)card int score = 0; Here s the declaration of a public method called match: which takes one argument (a pointer to a Card) and returns an integer. match: is going to return a score which says how good a match the passed card is to the Card that is receiving this message. 0 means no match, higher numbers mean a better match. return score;

@property (strong, nonatomic) NSString *contents; @property (nonatomic, getter=ischosen) BOOL chosen; @property (nonatomic, getter=ismatched) BOOL matched; There s a lot going on here! For the first time, we are seeing the calling side of properties (and methods). - (int)match:(card *)card; - (int)match:(card *)card int score = 0; if ([card.contents isequaltostring:self.contents]) score = 1; return score; For this example, we ll return 1 if the passed card has the same contents as we do or 0 otherwise (you could imagine more complex scoring).

@property (strong, nonatomic) NSString *contents; @property (nonatomic, getter=ischosen) BOOL chosen; @property (nonatomic, getter=ismatched) BOOL matched; Notice that we are calling the getter for the contents @property (both on our self and on the passed card). This calling syntax is called dot notation. It s only for setters and getters. - (int)match:(card *)card; - (int)match:(card *)card int score = 0; if ([card.contents isequaltostring:self.contents]) score = 1; return score;

Recall that the contents property is an NSString. @property (strong, nonatomic) NSString *contents; @property (nonatomic, getter=ischosen) BOOL chosen; @property (nonatomic, getter=ismatched) BOOL matched; - (int)match:(card *)card; - (int)match:(card *)card int score = 0; isequaltostring: is an NSString method which takes another NSString as an argument and returns a BOOL (YES if the 2 strings are the same). if ([card.contents isequaltostring:self.contents]) score = 1; Also, we see the square bracket notation we use to send a message to an object. In this case, the message isequaltostring: is being sent to the NSString returned by the contents getter. return score;

@property (strong, nonatomic) NSString *contents; @property (nonatomic, getter=ischosen) BOOL chosen; @property (nonatomic, getter=ismatched) BOOL matched; - (int)match:(nsarray *)othercards; - (int)match:( NSArray *)othercards int score = 0; We could make match: even more powerful by allowing it to match against multiple cards by passing an array of cards using the NSArray class in Foundation. if ([card.contents isequaltostring:self.contents]) score = 1; return score;

@property (strong, nonatomic) NSString *contents; @property (nonatomic, getter=ischosen) BOOL chosen; @property (nonatomic, getter=ismatched) BOOL matched; We ll implement a very simple match scoring system here which is to score 1 point if ANY of the passed othercards contents match the receiving Card s contents. (You could imagine giving more points if multiple cards match.) - (int)match:(nsarray *)othercards; - (int)match:( NSArray *)othercards int score = 0; for (Card *card in othercards) if ([card.contents isequaltostring:self.contents]) score = 1; return score; Note the for-in looping syntax here. This is called fast enumeration. It works on arrays, dictionaries, etc.