Archive

Archive for the ‘2011 Fall DPS913’ Category

Learning Objective-C – the top ten list

December 21, 2011 Leave a comment

When we begin to learn a new computer programming language, we often try to get acquainted with the language by applying our current knowledge to its characteristics. This “top ten” list captures this process.

.

As a third- or fourth-year student, you have some experience with a number of programming languages. Let’s begin to learn about Objective-C by asking questions in the form of a “top ten” list. (Note that you can ask these questions about ANY language.)

Note: Objective-C is the name of the programming language. It adds a small number of object-oriented extensions to the standard C language.

Cocoa is the name of the the class library and runtime. The classes are organized into “frameworks”. Your app will link to one or more frameworks.

So, in summary, to create apps for iOS and Mac OS X, you write code using the Objective-C language, and use Cocoa classes.

.

The “top ten” list of questions

Here is the “top ten” list of questions to ask when learning Objective-C:

1. How do I create/edit/save the source code? What editor do I use? Where do I store the source code? What is the source code file naming convention?

2. How do I build/compile and then execute/run my program? Is there a compiler? Is there a host execution (aka “runtime”) environment?

3. What is the program’s entry point? Syntactically, what coding convention must I follow?

4. What data types can I use? Are they categorized (e.g. value/stack, reference/heap), and if so, what do I need to know about their characteristics (e.g. size, initial value, precision, convertibility, etc.)?

5. How do I declare new variables/fields of a data type? How do I instantiate them? How do I refer to and use these variables/fields? (Note that a type could be a class declaration for an object-oriented language and platform.)

6. What operators are available to me in this language? (How do they compare to other languages that I’m familiar with?)

7. What is the syntax of a statement, and of an expression? What delimiters are used? How is scope and hierarchy expressed?

8. What syntax enables me to process a decision (e.g. if-else, or switch-case)?

9. What syntax enables me to process repeatedly (e.g. for, while)?

10. How are functions (methods) declared? What rules are there for argument/parameter declaration? How are functions used/called? Are the arguments passed by value, or by reference?

Bonus:

11. Where do I get learning/tutorial information? Where do I get reference information? Where do I look for good-quality online information?

.

The answers

Here are the answers. We cover them in class, and we go through some examples.

1. How do I create/edit/save the source code? What editor do I use? Where do I store the source code? What is the source code file naming convention?

You write source code with Xcode, Apple’s integrated development environment (IDE).

An app is typically organized as a “project”, which appears as a folder in the file system. In that folder, your source code is stored in text files, and you will also find “resources” for your app, such as images, configuration settings, and so on.

Objective-C is a header-based language. You define a class’s interface in a text file that has a .h extension (e.g. Person.h). The class’s implementation code is located in a text file that has a .m extension (e.g. Person.m).

Most often, you create classes for your app. A class can be used to perform tasks (e.g. a “controller”), or it can be used to represent real-world objects (e.g. a “model”).

.

2. How do I build/compile and then execute/run my program? Is there a compiler? Is there a host execution (aka “runtime”) environment? Does it control the application lifecycle?

In Xcode, you can use a “build” command (or one of its variants) to build/compile your program. Its command key shortcut is Command+B.

To run your program, you first select an execution target (e.g. a simulator, or a device), and then you use a “run” command. Its shortcut is Command+R.

Cocoa is the execution runtime environment. Your app runs under the control of Cocoa. Cocoa sends your app messages when something happens in the environment (e.g. a user touches/taps the screen, or data arrives from the network.

In summary, Cocoa is in control.

.

3. What is the program’s entry point? Syntactically, what coding convention must I follow?

As a C-based language, the main() function is your app’s entry point. When a user starts/launches your app, the main() function runs.

For a typical iOS or Mac OS X app, main() calls a function that loads the app into the Cocoa runtime environment. At that point in time, as noted above, the Cocoa runtime is in control.

The Xcode project templates all include the main.m source code module, which has the main() function. Typically, you do not ever have to edit main.m.

.

4. What data types can I use? Are they categorized (e.g. value/stack, reference/heap), and if so, what do I need to know about their characteristics (e.g. size, initial value, precision, convertibility, etc.)?

Your apps can use C language types, and Cocoa types, in any combination.

Most of the time, you will prefer to use Cocoa types. For example, NSString offers rich functionality for strings. NSArray is a collection class, for elements/members of any Cocoa type.

C language types, including structs, are value (scalar) types.

Cocoa type instances are reference types. The instance name is a pointer to the memory that holds the instance’s data.

The initial value of a type instance varies by type. Sometimes, you get “nil”, or nothing. Other times, you get a working object, with empty or nil fields (properties).

As you would expect, most convertible types have methods which enable type conversion. For example, NSString has an intValue method, which attempts to return an integer of the string instance.

You can create your own types. Objective-C is a header-based language:

  • The public interface (methods and properties) is defined in a source code file which has a .h extension. The definitions are placed between an @interface statement and an @end statement.
  • The implementation code is located in a source code file which has a .m extension. The code is placed between an @implementation statement and an @end statement.

.

5. How do I declare new variables/fields of a data type? How do I instantiate them? How do I refer to and use these variables/fields? (Note that a type could be a class declaration for an object-oriented language and platform.)

For C language types, simply use the C language semantics. For example:

int i = 5;

For Objective-C (Cocoa) types, you must create an instance of the desired type. There are two ways to initialize a new object:

  1. Use the type’s designated initializer
  2. Use a “class method”

.

Using an initializer:

Most Objective-C types include one or more initializer methods. (In C# and Java, you know these methods as “constructors”.)

Almost every type supports the base initializer, called “init”.

A type designer can create more initializers, which can accept arguments. If there’s more than one initializer, the type designer will make one of them the “designated initializer”, which is the one that performs the initialization. (If the developer uses another initializer, that initializer will ultimately call the designated initializer.) Initializers should always begin with “init”.

The following shows two examples that use an initializer:

NSMutableString *s = [[NSMutableString alloc] init];

Person *p = [[Person alloc] initWithLastname:@”McIntyre” andFirstName:@”Peter”];

.

Using a class method:

Many Objective-C types offer one or more class methods that initialize a new object. (In C# and Java, these are known as “static methods”.)

They are also known as “convenience” (or “factory”) methods. They make it really easy to create an object. Initializer class methods may or may not accept arguments, it depends upon their design.

The following shows two examples that use class methods. Assume that “lname” and “fname” are existing NSString objects:

NSString *s = [NSString stringWithFormat:@"My name is %@ %@", fname, lname];

Person *p = [Person personWithFirstName:fname andLastName:lname];

.

As you would expect (in other languages), you refer to the new objects by using their instance name. You would simply use “s” to refer to the NSString object above. Or, “p” to refer to the Person object above.

As you would do in C, if you wanted the memory address of “s” or “p”, use the “&” symbol. So, the address of s would be “&s”.

As you would do in C, if you wanted the pointer to “s”, use the “*” symbol. However, as it turns out, this is infrequently used.

.

6. What operators are available to me in this language? (How do they compare to other languages that I’m familiar with?)

The standard C operators are available.

When working with objects, you will typically call an object’s methods, to perform operations, or access state through getters and setters. Some objects have properties, which you can access using a dot operator.

.

7. What is the syntax of a statement, and of an expression? What delimiters are used? How is scope and hierarchy expressed?

Standard C language rules apply for statements, expression, delimiters, scope, and hierarchy.

However, when calling a method on an Objective-C object, a square-bracket syntax is used. The syntax is obviously quite different from C and other C-family object-oriented languages.

In the example that follows, we “convert” a string, which is holding an integer, into a C scalar, by calling the string’s “intValue” method. (The string comes from a “Label” control – which is an object – in the user interface).

int n = [lblAge.Text intValue];

.

8. What syntax enables me to process a decision (e.g. if-else, or switch-case)?

Objective-C is a C-family language, so its statement syntax is similar. if-else (including the ? : syntax) and switch-case are available.

.

9. What syntax enables me to process repeatedly (e.g. for, while)?

Objective-C is a C-family language, so its statement syntax is similar. for and while are available. There is also a foreach enumerator form of the for statement.

.

10. How are functions (methods) declared? What rules are there for argument/parameter declaration? How are functions used/called? Are the arguments passed by value, or by reference?

Objective-C is a C-family language, so you declare and call functions in a similar way.

In an Objective-C class, you define (then implement) its public interface with methods and properties. There are two kinds of methods: 1) Class methods, and 2) Instance methods:

  1. A class method can be called directly, without creating an instance of the class.
  2. An instance method can be called on an instance of the class.

.

The definition of a method is similar to C, but there are two important differences:

  1. The method definition (and implementation) is preceded by a + (plus sign) for a class method, and by a – (minus sign) for an instance method.
  2. The method parameters (arguments) are named.

.

Here are a few examples, using the “Person” examples from above (in question 5).

// Instance method definition/declaration
– (NSString *)newPhoneNumber:(NSString *)phoneNumber forPhoneNumberType:(NSString *)phoneNumberType;

// Class method definition/declaration
+ (Person *)personWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName;

.

The name of the instance method (above) is:

newPhoneNumber:forPhoneNumberType:

Notice that the name includes each argument, and the colons.

.

To call a method, use the syntax that you saw in question 5 above:

// Calling an instance method
Person *p = [[Person alloc] initWithLastname:@”McIntyre” andFirstName:@”Peter”];

// Calling a class method
Person *p = [Person personWithFirstName:fname andLastName:lname];

.

In Objective-C, the phrase “calling a method” is not used as a first choice. Instead, we use the phrase “sending a message (to an object)”, or more simply, “messaging”.

.

Parameters (arguments) are passed by value (like in C). However, please note that you often pass a pointer, so you are effectively passing a reference.

.

11. Where do I get learning/tutorial information? Where do I get reference information? Where do I look for good-quality online information? 

Your professor recommends the Apple developer documentation.

To get started, and learn effectively through discussion and demonstration, you must attend all lectures. Then, you can augment your learning with three kinds of Apple documentation:

1) Competencies Documents

Apple publishes two sets of well-written technology introductions. One set covers both iOS and Mac OS X, and is available here:

Cocoa Core Competencies

The other set covers topics that are specific to iOS:

Cocoa Application Competencies for iOS

Each document is a brief introduction to a topic, and includes links to a “definitive discussion” document, which is usually a developer guide.

.

2) Developer Guides 

Apple publishes a large number of developer guides. These documents range in size from tens of pages, to hundreds of pages. They include discussion, best-practice advice, code samples, and other information that’s valuable to the developer.

Developer guides are organized on a per-topic basis. You can see the complete list of guides by viewing the iOS Developer Library (after you login to the iOS Dev Center). On the left side, under “Resource Types”, click “Guides”.

A student should look for a developer guide after looking for and viewing a “competencies” document.

.

3) Reference Documents

Objective-C and Cocoa classes are well-documented in “reference” documentation. For a class reference (e.g. UITextField Class Reference), you will get overview information, as well as detailed syntax and usage for methods and properties.

.

The online iOS Dev Center is also your source for videos, code samples, Xcode information, and so on.

.

For non-Apple resources, the StackOverflow web site is acceptable. In addition, a few other web properties are useful from time to time, and they will be referenced in the notes during the semester.

Note: Your professor’s opinion is that you should avoid getting answers and code samples from search engine results. Typically, the information and code is low quality, and will distract you from learning the material. (Your professor is trying to be as kind as he can be with his phrasing here.)

.


.

.

.

Introduction to Cocoa programming on Mac OS X for iOS programmers

December 6, 2011 Leave a comment

This post will help the iOS programmer get started with Cocoa programming on Mac OS X.

This document was most recently updated in December 2011.

.

We have the following goals for this part of the course:

  • Orient the iOS developer to Mac OS development, specifically Cocoa
  • Leverage your existing knowledge
  • Think about making a Mac OS companion for your iOS app
  • Highlight a few interesting features of Cocoa for Mac OS apps

.

Getting started with Mac OS programming

The iOS developer can take advantage of what they know to get started with Mac OS programming. The language (Objective-C) is the same, and many of the Cocoa frameworks are the same.

The way in which data is persisted is the same. You can use a plist file (for arrays and dictionaries), archiving (for any type of object), and Core Data. The application also has access to data stored at network locations.

On iOS, the developer has UIKit, a framework with user interface classes that were designed for the iOS. On Mac OS, the developer uses AppKit, a richer set of user interface classes. The iOS offers the developer and user a single window, and a screen-at-a-time way of working with the application. The Mac OS enables the developer to use a larger screen, which allows the use of multiple windows.

Mac OS apps can be built as “document-based” applications, which means that the user is able to create, work with, and save document-oriented data. What format of data? What type? Well, it’s up to the developer. It can be as simple as a single data type (e.g. a text string in a text editor application), or a complex object graph, where (for example) a document consists of data that’s displayed in a tabular and/or graphical format, along with other data that helps make up a complete document. Document-based applications typically enable the user to “File > New” (Command+N), “File > Save” (Command+S), close (Command+W), and print (Command+P) documents.

When creating a new project, the Mac OS programming beginner will select one of the “Mac OS X” templates. For the first few projects, choose the Application > Cocoa Application template. Then, you can choose, with a check box, to create a document-based application, and/or use Core Data in your application.

A Mac OS app that doesn’t use either documents or Core Data gets created with the following:

  • Application delegate – similar in function and purpose to the iOS implementation
  • MainMenu nib – holds the menu choices, and a window object for the user interface

A Mac OS app that is based on the document-based template gets created with the following:

  • MyDocument controller class – similar in concept to an iOS view controller class
  • MainMenu nib – holds only the menu choices
  • MyDocument nib – holds the window, the view (user interface), and works with the MyDocument controller

A Mac OS app that uses Core Data but isn’t a document-based app gets created with the following:

  • Application delegate
  • MainMenu nib
  • Core Data model

A Mac OS app that uses Core Data and the document-based template gets created with the following:

  • MyDocument controller class
  • MainMenu nib
  • MyDocument nib
  • Core Data model

.

Memory management

Mac OS apps offer automatic memory management. This is new for iOS (4.x and earlier) developers, who must know and use memory management techniques.

Therefore, in Mac OS apps, you do not have to be concerned with reference counting, retain/release, and so on.

.

Example apps introduction

We have a number of example apps today:

  • HelloWorld
  • Bound slider
  • Professors base
  • Professors save and load

.

One is a real simple “hello world” app, that highlights a few differences between a Mac OS app and an iOS app.

The third app is actually one that we configure with a base level of functionality, and then add on some features in a subsequent version. In between, we look at an example that clearly shows the Cocoa Bindings feature.

.

HelloWorld sample app

HelloWorld The first sample app is HelloWorld. You can get it from the example apps download area.

It is a simple app, where you can enter text into a text field, click a button, and the text gets displayed in another text field.

Along the way, we do a few things that enable you to see how a Mac OS app is, or can be, quite different from an iOS app. Build your own app as you go through the sample app. Choose the “Cocoa Application” template, and don’t select any options for “document” or “Core Data”. Here are a few highlights of what you’ll experience:

Template contents: As you have learned, this kind of app includes an app delegate, a MainMenu nib, and little else. Open up the MainMenu nib, and you’ll see a “Window” object in the document window.

Controller class: To do some work, you’ll have to add a controller class. Click the Classes group, and add a new class. You will chose a “Cocoa Class”, specifically the “Objective-C class” (a subclass of NSObject). You will then add a reference to this new class in Interface Builder, by dragging an “NSObject” into the MainMenu nib’s document window, and setting its class name (to the name of the class you just created) on the Identity Inspector.

In your new class, you need to reference an NSTextField for the input area, another NSTextField for the output area, and a button. The text fields need to be marked with IBOutlet, and you’ll need to declare a method to handle the button click.

Back in Interface Builder, with the MainMenu.nib, add the two NSTextFields and the NSButton. Specifically:

  • The “input area” NSTextField will be a multi-line wrapping NSTextField
  • The “output area” NSTextField will be a multi-line label NSTextField
  • The NSButton will be a “Push Button”

To replicate the full functionality of the HelloWorld sample app, you’ll have to add in the parts for speech synthesis. The program code is well-documented, so look at it for what-when-where-why info.

An iOS app’s screen is a constant size, and we’re always working with a screen at a time. A Mac OS app can have a resizable window area. Our sample app anticipates this, by enabling the user to resize the window. Smooth resizing is implemented mostly in Interface Builder, by setting properties in the Size Inspector. Focus on looking in this inspector, and experiment with your own version of the app.

For a bit more fun, our sample app allows resizing only to the same height-width proportion as the background picture. To do this, we implement a delegate method that helps us.

.

Information about user interface objects (controls)

The iOS programmer will notice differences in AppKit user interface objects – e.g. buttons, text fields, and so on – that are conceptually related to UIKit objects. For best results, do not assume that an AppKit object has the same properties and instance methods as its related UIKit object. Also, when you look in the documentation for a class member, you may not find what you’re looking for. In that situation, follow the object inheritance hierarchy, and look in the object’s parent, repeating this action until you find what you’re looking for.

For example, an AppKit NSTextField has the following inheritance hierarchy:

  • NSTextField inherits from…
  • NSControl, which inherits from…
  • NSView, which inherits from…
  • NSResponder, which inherits from…
  • NSObject

To repeat, if you’re looking for a class member in NSTextField, and cannot find what you’re looking for, look in NSControl. Repeat with NSView etc. as required.

To help you get started working with text fields, note the following:

  • To read the contents of an NSTextField, send the instance the “stringValue” message
  • To write the contents of an NSTextField, send the instance the “setStringValue” message

.

Professors sample app (“Professors base”)

ProfLoad The second sample app is Professors. You can download the base version (ProfessorsBase) from the example apps download area.

Your iOS apps often included UITableView based views. This Professors sample app uses a Mac OS NSTableView to display information about a list of professors.

While the iOS UITableView is a one-column table, an NSTableView enables the display of multiple columns. Each column can display text (the default), or other data types. For our simple example, we’ll stick with text display.

When we worked with an iOS UITableView, we implemented delegate and data source methods to handle table view operations. Although we could do that in a Mac OS app, we won’t – instead, we’ll use Cocoa Bindings, a Mac OS technology that can simplify table view operations. You’ll see that you can create a functional table view with little or no code.

.

Introducing Cocoa Bindings

Cocoa Bindings can simplify table view operations, and any other situation where you want to bind user interface objects to data model objects.

The data model objects can be simple – a string or an int for example – or they can be more complex and real-world – like an array, dictionary, or Core Data managed object graph. For best results, expose the data model object(s) as properties.

To introduce Cocoa Bindings, get the “Bound Slider” example app. It has a user interface with a slider and a text label. When the slider value changes, the text label is updated with the new value. The view controller has an int property, and both user interface objects are bound to the property. The Bindings Inspector enables you to establish the binding.

The following image illustrates the binding relationship:

Cocoa Bindings - bound slider

While the example app and the diagram above shows you how to bind to a simple and distinct data model object (i.e. the int), you may also need to bind to a collection of objects, to display for example, a table view (as in today’s Professors… example apps).

For this scenario, Cocoa offers an Array Controller object. The Array Controller is added to the window nib, and a reference is made from it to your data model object, which in today’s example, is an array called “professors” in the view controller. The professors array is exposed as a property, and holds instances of “professor” objects as its elements.

Then, user interface objects are bound to the Array Controller, by specifying a “key path” to the data model object properties. Ideal for use with a table view, an Array Controller has an “arrangedObjects” instance variable, which represents the data model object collection (as a result of its reference to – in today’s example – the “professors” array in the view controller). The user interface object specifies the arrangedObjects instance variable, and then the property name in a data model object.

Alternatively, if the user interface object is to be bound to a specific object (rather than the whole collection), the Array Controller’s “selection” instance variable (inherited from NSArrayController’s superclass, NSObjectController) is the binding key. This scenario is used for one-of user interface elements, like a text label or an image.

The following image illustrates the binding relationship in the “Professors base” example app:

Cocoa Bindings - array controller

.

Continue working on the  “Professors base” app

Now that you understand more about Cocoa Bindings, let’s continue building the app.

The first HelloWorld sample app was not a document-based application. Professors is, so as you’re following along, create a new Cocoa Application, and chose the “document” option. Notice that you get a MyDocument class and nib.

If you build and run, you will see that you get a fairly functional app from the template. You are able to create new (Command+N) document instances, close (Command+W) them, and so on. You can try saving (Command+S), but the save method hasn’t yet been implemented.

The next thing we want to do is to create a model class, “Professor”. This class will have properties for name, office, and photo. Nothing much is new here, when compared to the task in an iOS app. The class includes an init method overload that enables a fully working instance object to be returned.

Then, we will create an array that will be the data source for the table view. We do that in the MyDocument class. We configure it as a property, so that it will work with Cocoa Bindings.

Note: the screen captures are from Xcode 3.x on Mac OS X 10.6.x. They are a bit out of date, but not much. Time permitting, they will be updated. 

.

ProfBase1 Now, we can create the user interface. It has two buttons (add a professor to the table view; delete a professor), a table view, and an image view (in which we’ll add photo support later). See the screen image on the right. On the window object’s Attributes Inspector, the “Resize” control has been unchecked/cleared, because we don’t want the user resizing the window.

After laying out the interface, do this: Drag an array controller from the library into the document window. On the Attributes Inspector, set the Class Name to “Professor”, and add the keys for the fullName, office, and photo properties. The keys match up to the model object’s properties, and are used by Cocoa Bindings. See the screen image below / to the left.

ProfBase2

What did you just do? You added an array controller, and told it to work with “Professor” objects. In a minute, we’ll connect it to the “professors” array in the MyDocument class.

ProfBase3 An array controller manages a collection of objects in a situation where we use Cocoa Bindings. The Cocoa Bindings technologies do a number of things for us, including reducing code dependencies, and synchronizing views when model data changes (and vice-versa). The array controller is a helper object, similar in a way that the “fetched results controller” is a helper object in an iOS app that uses Core Data and a UITableView.

So, the array controller knows it is working with “Professor” objects. Let’s connect it to the “professors” array in the MyDocument class. Click the Bindings Inspector (green rectangle and circle). In the Controller Content area, bind to File’s Owner (i.e. the MyDocument class), and enter “professors” in the model key path dropdown list. The other properties are fine as they are. See the screen image to the right.

The final array controller configuration task is to connect the “add” and “delete” buttons. Click to show the Connections Inspector. In the Received Actions area, click-and-drag the “add:” outlet to the add button in the user interface. Click-and-drag the “remove:” outlet to the delete button in the user interface. Your inspector panel should look similar to the example below.

ProfBase8

OK… now it’s time to connect the table view columns with the “Professor” properties fullName and office. Double-click the table view column for the professor’s name, and the window should look similar to the screen capture image below, on the left. Now, in the Bindings Inspector for that column, set these properties, so that they appear similar to the screen capture image below, on the right.

  • Bind to Array Controller
  • Controller Key is arrangedObjects (which is the “professors” array we just pointed to above)
  • The Model Key Path is fullName (that’s what we want to show up in the table view column)

ProfBase4 ProfBase5

Continue by configuring the office column. Double-click the table view column for the professor’s office, and the window should look similar to the screen capture image below, on the left. Now, in the Bindings Inspector for that column, set these properties, so that they appear similar to the screen capture image below, on the right.

  • Bind to Array Controller
  • Controller Key is arrangedObjects
  • The Model Key Path is office

ProfBase6 ProfBase7

ProfBase9 Save your work. At this point, you’ve configured an array controller helper object, and configured it to bind to the “professors” array. Run your app, and you will see that you can add and remove professors from the list.

The final thing we want to do with this app is to add the ability to drag-and-drop a professor’s photo into the image area of the app. It turns out to be easy to do. Click to select the image view. In its Bindings Inspector, set these properties, so that they appear similar to the screen capture image to the right.

  • Bind to Array Controller
  • Controller Key is selection this time; we’re relying on a selected professor in the table view list
  • The Model Key Path is photo
  • Conditionally Sets Editable is checked

Run your application again. Make sure you have created and selected (highlighted) a professor on your list. Now, open a Finder window that contains some photos, or a web page in Safari. Click, drag, and drop a photo into the image box on the app.

.

Professors sample app, enhanced (“Professors save and load”)

The third sample app extends Professors. You can download the enhanced version, ProfessorsSaveLoad, from the example apps download area.

As the name suggests, it adds the ability to save your list of professors as a document, and to load it up again.

We are going to use “archiving”, the same technology that we used on the iOS apps many weeks ago. To review, we have to configure the model class to know how to archive itself, and then we’ll implement methods in the controller that will take care of the saving and loading.

The first task is to edit the Professor .h interface to conform to the NSCoding protocol. Next, in its .m implementation, implement the initWithCoder: and encodeWithCoder: methods, as shown below.

Code1

Next, we write some code for some existing MyDocument controller methods that we’ve ignored up to now.

The dataOfType:error: method implements saving. It runs when the user chooses “Save” from the File menu (or presses Command+S), provides a file name, and clicks the save button. To make it work, make it return an NSData as shown below:

Code2

The readFromData:dataOfType:error method implements loading. It runs when the user chooses “Open” from the File menu (or presses Command+O), selects a file/document, and clicks the open button. To make it work, read (load) the data, place it into the “professors” property, and return YES, as shown below:

Code3

Finally, we need to change some settings for the overall app that it and Mac OS will be able it to recognize the documents the app works with. Expand/open the “Targets” group, right-click the “Professors save and load” app, and choose Get Info. Click the Properties tab. This sheet enables us to configure information about the documents that this app works with. Here’s what to do. For more information, consult the reference documentation.

Near the top, change the “Identifier” to a unique identifier – use some of your name for this, and the last part will be the app’s name.

In the Document Types list, change the only entry, so that it has the following properties:

  • Name – Professors document
  • Extensions – profs
  • OS Types – empty/blank/null

Close the dialog box to save your changes. See below for a before and after view of this dialog box.

Before…

Target - file extension config

After…

Target - file extension config

Now, run your app. The save and load/open operations should work.

.

Summary

This Mac OS programming introduction has shown you some of the similarities with iOS programming, and it has highlighted a few interesting capabilities that are available only on Mac OS apps. Take your time reviewing today’s notes, and recreate the examples on your own.
.


.

.

.

Categories: 2011 Fall DPS913, Mac OS X

Creating sequence diagrams

October 31, 2011 Leave a comment

In this document, we briefly describe a few different ways to create sequence diagrams.

This post was most recently updated in October 2013. It is intended for programmers in a degree-level academic program that focuses on software development. This post will likely be updated a few times in the near future, to add more detail and information.

.

A brief survey of sequence diagram creation toolsets

Students in Seneca’s ICT software development programs learn to use Rational Rose to create sequence diagrams, and other software system design modeling assets.

Visual Studio 2012 Ultimate Edition now includes tooling that enables the creation of sequence diagrams.

Online, there are many tools that are offered, in both free and pay-for versions. In the following section, we’ll look at one of these tools.

.

Websequencediagrams.com

Websequencediagrams.com is an online tool that enables you to create and save sequence diagrams. Its syntax is easy and discoverable. It enables you to save your work in a PNG image, a PDF, or as source text. It also provides online tooling, via an API.

A recent post by the author (Web service security principles – OAuth 2.0 example) included two sequence diagrams.

The source text for the first diagram is below:

.

participant Resource\nOwner as RO
participant "Web\nBrowser" as ClientUI
participant Client\nApp as Client
participant Authorization\nServer as AS
participant Resource\nServer as RS

RO->ClientUI: [1] want protected resource
activate RO
activate ClientUI

ClientUI->Client: [2] GET protected resource
activate Client

Client-->ClientUI: [3] HTTP 302 redirect
deactivate Client
deactivate ClientUI

ClientUI->AS: [4] GET authentication UI
activate ClientUI
activate AS
AS-->ClientUI: [5] respond with authentication UI

ClientUI-->RO: [6] view authentication UI

RO->ClientUI: [7] enter credentials
ClientUI->AS: [8] POST the credentials

AS-->ClientUI: [9] HTTP 302 redirect to redirection endpoint
deactivate AS
deactivate ClientUI

ClientUI->Client: [10] GET auth grant exchanged
activate ClientUI
activate Client

Client->AS: [11] POST request access token
activate AS

AS-->Client: [12] respond with access token
deactivate AS

Client-->ClientUI: [13] HTTP 302 redirect
deactivate Client
deactivate ClientUI

ClientUI->Client: [14] GET protected resource
activate Client
activate ClientUI

Client->RS: [15] GET protected resource (with access token)
activate RS

RS-->Client: [16] respond with the resource
deactivate RS

Client-->ClientUI: [17] render resource in browser

deactivate Client
ClientUI-->RO: [18] view the resource
deactivate ClientUI

deactivate RO

.

The source text for the second diagram is below:

.

participant Resource\nOwner as RO
participant "Web\nBrowser" as ClientUI
participant Client\nApp as Client
participant Authorization\nServer as AS
participant Resource\nServer as RS

RO->ClientUI: [1] want protected resource
activate RO
activate ClientUI

ClientUI->Client: [2] GET protected resource
activate Client

Client->RS: [15] GET protected resource (with access token)
activate RS

RS-->Client: [16] respond with the resource
deactivate RS

Client-->ClientUI: [17] render resource in browser

deactivate Client
ClientUI-->RO: [18] view the resource
deactivate ClientUI

deactivate RO

.

.

.

.

.

.

.

Introduction to Cocoa collection classes

September 19, 2011 Leave a comment

Cocoa has classes that hold collections of objects. We work with two today (array, dictionary) and later, we will work with a third (set).

Read more…

Model objects for iOS apps – introduction

September 19, 2011 Leave a comment

This note will help you design useful and simple model objects for your iOS app. It is intended for the entry-level iOS app developer. A future post will enable you to improve the design of your model object, to accommodate more complex requirements.

Read more…

Declared Properties in Objective-C

September 19, 2011 Leave a comment

In Objective-C classes, declared properties provide a convenient syntax for the creation of instance variable accessors (i.e. getters and setters).

Read more…

How to use Apple developer documentation

September 5, 2011 Leave a comment

This document will teach you how to use Apple developer documentation efficiently and effectively.

Read more…

Follow

Get every new post delivered to your Inbox.

Join 45 other followers