SlideShare a Scribd company logo
CS193P                                                                            Assignment 1B
Winter 2010                                                                    Cannistraro/Shaffer

                       Assignment 1B - WhatATool (Part I)
Due Date
This assignment is due by 11:59 PM, January 13.

 Assignment
In this assignment you will be getting your feet wet with Objective-C by writing a small
command line tool. You will create and use various common framework classes in order to
become familiar with the syntax of the language.

The Objective-C runtime provides a great deal of functionality, and the gcc compiler
understands and compiles Objective-C syntax. The Objective-C language itself is a small set of
powerful syntax additions to the standard C environment.

To that end, for this assignment, you’ll be working in the most basic C environment available –
the main() function.

This assignment is divided up into several small sections. Each section is a mini-exploration of a
number of Objective-C classes and language features. Please put each section’s code in a
separate C function. The main() function should call each of the section functions in order.
Next week will add more sections to this assignment.

IMPORTANT: The assignment walkthrough begins on page 3 of this document. The
assignment walkthrough contains all of the section-specific details of what is expected.

The basic layout of your program should look something like this:

#import <Foundation/Foundation.h>

// sample function for one section, use a similar function per section
void PrintPathInfo() {
	      // Code from path info section here
}

int main (int argc, const char * argv[]) {
    NSAutoreleasepool * pool = [[NSAutoreleasePool alloc] init];

	    PrintPathInfo();                  //   Section   1
	    PrintProcessInfo();               //   Section   2
	    PrintBookmarkInfo();              //   Section   3
	    PrintIntrospectionInfo();         //   Section   4

    [pool release];
    return 0;
}

Testing
In most assignments testing of the resulting application is the primary objective. In this case,
testing/grading will be done both on the output of the tool, but also on the code of each
section.

We will be looking at the following:
1. Your project should build without errors or warnings.
2. Your project should run without crashing.

                                            Page 1 of 6
CS193P                                                                           Assignment 1B
Winter 2010                                                                   Cannistraro/Shaffer

3. Each section of the assignment describes a number of log messages that should be printed.
   These should print.
4. Each section of the assignment describes certain classes and methodology to be used to
   generate those log messages – the code generating those messages should follow the
   described methodology, and not be simply hard-coded log messages.

A note about the NSLog function. It will generate a string that is prefixed with a timestamp, the
name of the process, and the pid. It will also automatically append a newline to the log
statement.

        2009-09-23 13:49:42.275 WhatATool[360] Your message here.


This is expected. You are only being graded on the text not generated automatically by NSLog.
You do not need to attempt to suppress what NSLog prints by default.

To help make the output of your program more readable, it would be helpful to put some kind
of header or separator between each of the sections.

Hints
Hints are distributed section by section but generally, the lecture #2 notes provide some very
good clues with regards to Objective-C syntax and commonly used methods.

Another source of good information is the Apple ADC site. In particular, these documents may
be particularly helpful:

http://developer.apple.com/documentation/Cocoa/Conceptual/OOP_ObjC

http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC

http://developer.apple.com/iphone/gettingstarted/docs/objectivecprimer.action


Troubleshooting
Remember that an Objective-C NSString constant is prefixed with an @ sign. For example,
@”Hello World”. The compiler will warn, and your program will crash if you use a C string
where an NSString is required.




                                           Page 2 of 6
CS193P                                                                           Assignment 1B
Winter 2010                                                                   Cannistraro/Shaffer

Assignment Walkthrough

In Xcode, create a new Foundation Tool project. Name it WhatATool.




You will be doing the bulk of your work in the WhatATool.m file where a skeletal implementation
of main() has been provided.

You should build and test at the end of each code-writing section.

Finding Answers / Getting to documentation
Some of the information required for this exercise exists in the class documentation, not in the
course materials. You can use the Xcode documentation window to search for class
documentation as well as conceptual articles.

Open the Xcode documentation window by choosing Help > Documentation.

The leftmost section of the search bar in the documentation window lets you search APIs, Titles
or Full-Text of the documentation. For now, use the API Search. Select an appropriate Doc Set
to search, for example the “Apple iPhone OS 2.0” doc set should contain all the materials you’ll
need for this assignment.




                                           Page 3 of 6
CS193P                                                                             Assignment 1B
Winter 2010                                                                     Cannistraro/Shaffer

Section 1: Strings as file system paths

NSString has a set of methods that allow you to manipulate file system paths. You can find
them in the NSString documentation grouped under the heading “Working with paths”     .

In this section, begin with an NSString constant that is a tilde – the symbolic representation for
your home directory in Unix.

          NSString *path = @"~";

Starting with that string, find a path method that will expand the tilde in the path to the full
path to your home directory. Use that method to make a new string, and log the full path in a
format like:

          My home folder is at '/Users/pmarcos'

NSString has a method that will return an array of path components. Each element in the
returned array is a single component in the original path.

Use this method to get an array of path components for the path you just logged.

Use fast enumeration to enumerate through each item in the returned array, and log each path
component. The result should look something like:
	         /
          Users
	         pmarcos

Section Hints:
    • With string convenience methods, a common pattern is to reuse the same variable:
                NSString *aString = @"Bob";
      	         aString = [aString stringWithSomeModification];



Section 2: Finding out a bit about our own process
Look up the class NSProcessInfo in the documentation.

You will find a class method that will return an NSProcessInfo object. (In fact, you should find a
very handy code sample there as well).

From this object, you can access the name and process identifier (pid) of the process.

Log these pieces of information to the console using NSLog in the format:

          Process Name: 'WhatATool' Process ID: '4556'

Section Hints:
    • We didn’t discuss NSProcessInfo in lecture at all, so you aren’t missing any slides or notes.
      All of the information you need is in the NSProcessInfo class documentation.
    • Comparing the process info you logged with the prefix that NSLog generates can help
      verify you’re logging the correct information.



                                            Page 4 of 6
CS193P                                                                             Assignment 1B
Winter 2010                                                                     Cannistraro/Shaffer



Section 3: A little bookmark dictionary

In this section, you will build a small URL bookmark repository using a mutable dictionary. Each
key is an NSString that serves as the description of the URL, the value is an NSURL.

Create a mutable dictionary that contains the following key/value pairs:

Key (NSString)                    Value (NSURL)
Stanford University               http://www.stanford.edu
Apple                             http://www.apple.com
CS193P                            http://cs193p.stanford.edu
Stanford on iTunes U              http://itunes.stanford.edu
Stanford Mall                     http://stanfordshop.com

Enumerate through the keys of the dictionary. While enumerating through the keys, check each
key to see if it starts with @”Stanford” If it does, retrieve the NSURL object for that key from the
                                       .
dictionary, and log both the key string and the NSURL object in the following format below. If
the key does not start with @”Stanford” no output should be printed.
                                          ,

           Key: 'Stanford University' URL: 'http://www.stanford.edu'

Section Hints:
    • Use +URLWithString: to create the URL instances.
    • NSString has methods to determine if a string has a particular prefix or suffix. Remember
      to log only those keys that start with ‘Stanford’.
    • The methods for creating and adding items to a mutable dictionary are located in the
      Lecture #2 slides. Remember that an NSMutableDictionary is a subclass of NSDictionary
      and so inherits all of its methods.
    • When using a dictionary and fast enumeration, you are enumerating the keys of the
      dictionary. You can use the key to then retrieve the value. NSDictionary also defines a
      method called valueEnumerator that returns an NSEnumerator object which will directly
      enumerate the values. You can use either approach.

Section 4: Selectors, Classes and Introspection
Objective-C has a number of facilities that add to its dynamic object-oriented capabilities.  Many
of these facilities deal with determining and using an object's capabilities at runtime. 

Create a mutable array and add objects of various types to it. Create instance of the classes
we’ve used elsewhere in this assignment to populate the array: NSString, NSURL, NSProcessInfo,
NSDictionary, etc. Create some NSMutableString instances and put them in the array as well.
Feel free to create other kinds of objects also.

Iterate through the objects in the array and do the following:

      1.    Print the class name of the object.
      2.    Log if the object is member of class NSString.
      3.    Log if the object is kind of class NSString.
      4.    Log if the object responds to the selector "lowercaseString".


                                             Page 5 of 6
CS193P                                                                           Assignment 1B
Winter 2010                                                                   Cannistraro/Shaffer

      5. If the object does respond to the lowercaseString selector, log the result of asking the
         object to perform that selector (using performSelector:)

For example, if an array contained an NSString, an NSURL and an NSDictionary the output might
look something like this:

2008-01-10    20:56:03   WhatATool[360]   Class name: NSCFString
2008-01-10    20:56:03   WhatATool[360]   Is Member of NSString: NO
2008-01-10    20:56:03   WhatATool[360]   Is Kind of NSString: YES
2008-01-10    20:56:03   WhatATool[360]   Responds to lowercaseString: YES
2008-01-10    20:56:03   WhatATool[360]   lowercaseString is: hello world!
2008-01-10    20:56:03   WhatATool[360]   ======================================
2008-01-10    20:56:03   WhatATool[360]   Class name: NSURL
2008-01-10    20:56:03   WhatATool[360]   Is Member of NSString: NO
2008-01-10    20:56:03   WhatATool[360]   Is Kind of NSString: NO
2008-01-10    20:56:03   WhatATool[360]   Responds to lowercaseString: NO
2008-01-10    20:56:03   WhatATool[360]   ======================================
2008-01-10    20:56:03   WhatATool[360]   Class name: NSCFDictionary
2008-01-10    20:56:03   WhatATool[360]   Is Member of NSString: NO
2008-01-10    20:56:03   WhatATool[360]   Is Kind of NSString: NO
2008-01-10    20:56:03   WhatATool[360]   Responds to lowercaseString: NO
2008-01-10    20:56:03   WhatATool[360]   ======================================

Section Hints:
   • When you ask various classes, such as NSString, for its class name, you may not get the
      result you expect.  Your logs should report what the runtime reports back to you - don't
      worry if some of the class names may seem strange, these are implementation details of
      the NSString class. Encapsulation at work!




                                           Page 6 of 6

More Related Content

PDF
Bootstrapping iPhone Development
PDF
Handout 00 0
PPTX
C# 101: Intro to Programming with C#
PPT
C sharp
PDF
Object oriented-programming-in-c-sharp
PPTX
PDF
Tutorial c#
PPT
C++ to java
Bootstrapping iPhone Development
Handout 00 0
C# 101: Intro to Programming with C#
C sharp
Object oriented-programming-in-c-sharp
Tutorial c#
C++ to java

What's hot (20)

PPTX
Tech breakfast 18
PDF
Swift Programming Language
PPT
Java for C++ programers
PDF
Swift Tutorial Part 2. The complete guide for Swift programming language
PDF
How to really obfuscate your pdf malware
PDF
2.Getting Started with C#.Net-(C#)
PPT
C#.NET
PPTX
C sharp
PDF
1..Net Framework Architecture-(c#)
PDF
Hack in the Box GSEC 2016 - Reverse Engineering Swift Applications
PPTX
AngularConf2015
PDF
Introduction to mobile reversing
PPT
PPT
Packer Genetics: The selfish code
PDF
Core Java Tutorial
PPT
Csharp
PDF
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
PPTX
API workshop: Deep dive into Java
Tech breakfast 18
Swift Programming Language
Java for C++ programers
Swift Tutorial Part 2. The complete guide for Swift programming language
How to really obfuscate your pdf malware
2.Getting Started with C#.Net-(C#)
C#.NET
C sharp
1..Net Framework Architecture-(c#)
Hack in the Box GSEC 2016 - Reverse Engineering Swift Applications
AngularConf2015
Introduction to mobile reversing
Packer Genetics: The selfish code
Core Java Tutorial
Csharp
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
API workshop: Deep dive into Java
Ad

Viewers also liked (6)

PDF
Mobile development
PDF
01 introduction
PDF
Mume HTML5 Intro
PDF
More! @ ED-MEDIA
PDF
Mume JQueryMobile Intro
PDF
iOS Development Introduction
Mobile development
01 introduction
Mume HTML5 Intro
More! @ ED-MEDIA
Mume JQueryMobile Intro
iOS Development Introduction
Ad

Similar to Assignment1 B 0 (20)

PDF
Assignment2 A
DOCX
Article link httpiveybusinessjournal.compublicationmanaging-.docx
ODP
Aspect-oriented programming in Perl
PDF
Java lab-manual
PPTX
Objective c slide I
PPT
iOS Application Development
DOCX
Comp 220 ilab 5 of 7
PDF
Ts archiving
PDF
27.1.5 lab convert data into a universal format
PPTX
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
DOCX
PRG 421 Education Specialist / snaptutorial.com
PDF
A Project Based Lab Report On AMUZING JOKE
DOCX
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
PDF
Language-agnostic data analysis workflows and reproducible research
PPTX
1. introduction to python
DOCX
OverviewUsing the C-struct feature, design, implement and .docx
DOC
Cis 170 c ilab 7 of 7 sequential files
DOCX
NamingConvention
PPTX
8-Roslyn for microsoft software framework.pptx
PPT
ActionScript 3.0 Fundamentals
Assignment2 A
Article link httpiveybusinessjournal.compublicationmanaging-.docx
Aspect-oriented programming in Perl
Java lab-manual
Objective c slide I
iOS Application Development
Comp 220 ilab 5 of 7
Ts archiving
27.1.5 lab convert data into a universal format
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
PRG 421 Education Specialist / snaptutorial.com
A Project Based Lab Report On AMUZING JOKE
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
Language-agnostic data analysis workflows and reproducible research
1. introduction to python
OverviewUsing the C-struct feature, design, implement and .docx
Cis 170 c ilab 7 of 7 sequential files
NamingConvention
8-Roslyn for microsoft software framework.pptx
ActionScript 3.0 Fundamentals

More from Mahmoud (20)

PDF
مهارات التفكير الإبتكاري كيف تكون مبدعا؟
PDF
كيف تقوى ذاكرتك
PDF
مهارات التعامل مع الغير
PDF
ستيفن كوفي ( ادارة الاولويات ) لايفوتكم
PDF
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائق
PDF
الشخصية العبقرية
PDF
مهارات كتابه السيرة الذاتيه واجتياز المقابله الشخصيه
PDF
مهارات التفكير الإبتكاري كيف تكون مبدعا؟
PDF
مهارات التعامل مع الغير
PDF
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائق
PDF
كيف تقوى ذاكرتك
PDF
ستيفن كوفي ( ادارة الاولويات ) لايفوتكم
PDF
الشخصية العبقرية
PPT
Accident Investigation
PPT
Investigation Skills
PDF
Building Papers
PPT
Appleipad 100205071918 Phpapp02
PPTX
Operatingsystemwars 100209023952 Phpapp01
PDF
A Basic Modern Russian Grammar
PDF
Teams Ar
مهارات التفكير الإبتكاري كيف تكون مبدعا؟
كيف تقوى ذاكرتك
مهارات التعامل مع الغير
ستيفن كوفي ( ادارة الاولويات ) لايفوتكم
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائق
الشخصية العبقرية
مهارات كتابه السيرة الذاتيه واجتياز المقابله الشخصيه
مهارات التفكير الإبتكاري كيف تكون مبدعا؟
مهارات التعامل مع الغير
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائق
كيف تقوى ذاكرتك
ستيفن كوفي ( ادارة الاولويات ) لايفوتكم
الشخصية العبقرية
Accident Investigation
Investigation Skills
Building Papers
Appleipad 100205071918 Phpapp02
Operatingsystemwars 100209023952 Phpapp01
A Basic Modern Russian Grammar
Teams Ar

Recently uploaded (20)

PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Encapsulation theory and applications.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Cloud computing and distributed systems.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPT
Teaching material agriculture food technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
cuic standard and advanced reporting.pdf
Machine learning based COVID-19 study performance prediction
20250228 LYD VKU AI Blended-Learning.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Encapsulation theory and applications.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
MYSQL Presentation for SQL database connectivity
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
NewMind AI Weekly Chronicles - August'25 Week I
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectroscopy.pptx food analysis technology
Cloud computing and distributed systems.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Understanding_Digital_Forensics_Presentation.pptx
Teaching material agriculture food technology
Review of recent advances in non-invasive hemoglobin estimation
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx

Assignment1 B 0

  • 1. CS193P Assignment 1B Winter 2010 Cannistraro/Shaffer Assignment 1B - WhatATool (Part I) Due Date This assignment is due by 11:59 PM, January 13. Assignment In this assignment you will be getting your feet wet with Objective-C by writing a small command line tool. You will create and use various common framework classes in order to become familiar with the syntax of the language. The Objective-C runtime provides a great deal of functionality, and the gcc compiler understands and compiles Objective-C syntax. The Objective-C language itself is a small set of powerful syntax additions to the standard C environment. To that end, for this assignment, you’ll be working in the most basic C environment available – the main() function. This assignment is divided up into several small sections. Each section is a mini-exploration of a number of Objective-C classes and language features. Please put each section’s code in a separate C function. The main() function should call each of the section functions in order. Next week will add more sections to this assignment. IMPORTANT: The assignment walkthrough begins on page 3 of this document. The assignment walkthrough contains all of the section-specific details of what is expected. The basic layout of your program should look something like this: #import <Foundation/Foundation.h> // sample function for one section, use a similar function per section void PrintPathInfo() { // Code from path info section here } int main (int argc, const char * argv[]) { NSAutoreleasepool * pool = [[NSAutoreleasePool alloc] init]; PrintPathInfo(); // Section 1 PrintProcessInfo(); // Section 2 PrintBookmarkInfo(); // Section 3 PrintIntrospectionInfo(); // Section 4 [pool release]; return 0; } Testing In most assignments testing of the resulting application is the primary objective. In this case, testing/grading will be done both on the output of the tool, but also on the code of each section. We will be looking at the following: 1. Your project should build without errors or warnings. 2. Your project should run without crashing. Page 1 of 6
  • 2. CS193P Assignment 1B Winter 2010 Cannistraro/Shaffer 3. Each section of the assignment describes a number of log messages that should be printed. These should print. 4. Each section of the assignment describes certain classes and methodology to be used to generate those log messages – the code generating those messages should follow the described methodology, and not be simply hard-coded log messages. A note about the NSLog function. It will generate a string that is prefixed with a timestamp, the name of the process, and the pid. It will also automatically append a newline to the log statement. 2009-09-23 13:49:42.275 WhatATool[360] Your message here. This is expected. You are only being graded on the text not generated automatically by NSLog. You do not need to attempt to suppress what NSLog prints by default. To help make the output of your program more readable, it would be helpful to put some kind of header or separator between each of the sections. Hints Hints are distributed section by section but generally, the lecture #2 notes provide some very good clues with regards to Objective-C syntax and commonly used methods. Another source of good information is the Apple ADC site. In particular, these documents may be particularly helpful: http://developer.apple.com/documentation/Cocoa/Conceptual/OOP_ObjC http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC http://developer.apple.com/iphone/gettingstarted/docs/objectivecprimer.action Troubleshooting Remember that an Objective-C NSString constant is prefixed with an @ sign. For example, @”Hello World”. The compiler will warn, and your program will crash if you use a C string where an NSString is required. Page 2 of 6
  • 3. CS193P Assignment 1B Winter 2010 Cannistraro/Shaffer Assignment Walkthrough In Xcode, create a new Foundation Tool project. Name it WhatATool. You will be doing the bulk of your work in the WhatATool.m file where a skeletal implementation of main() has been provided. You should build and test at the end of each code-writing section. Finding Answers / Getting to documentation Some of the information required for this exercise exists in the class documentation, not in the course materials. You can use the Xcode documentation window to search for class documentation as well as conceptual articles. Open the Xcode documentation window by choosing Help > Documentation. The leftmost section of the search bar in the documentation window lets you search APIs, Titles or Full-Text of the documentation. For now, use the API Search. Select an appropriate Doc Set to search, for example the “Apple iPhone OS 2.0” doc set should contain all the materials you’ll need for this assignment. Page 3 of 6
  • 4. CS193P Assignment 1B Winter 2010 Cannistraro/Shaffer Section 1: Strings as file system paths NSString has a set of methods that allow you to manipulate file system paths. You can find them in the NSString documentation grouped under the heading “Working with paths” . In this section, begin with an NSString constant that is a tilde – the symbolic representation for your home directory in Unix. NSString *path = @"~"; Starting with that string, find a path method that will expand the tilde in the path to the full path to your home directory. Use that method to make a new string, and log the full path in a format like: My home folder is at '/Users/pmarcos' NSString has a method that will return an array of path components. Each element in the returned array is a single component in the original path. Use this method to get an array of path components for the path you just logged. Use fast enumeration to enumerate through each item in the returned array, and log each path component. The result should look something like: / Users pmarcos Section Hints: • With string convenience methods, a common pattern is to reuse the same variable: NSString *aString = @"Bob"; aString = [aString stringWithSomeModification]; Section 2: Finding out a bit about our own process Look up the class NSProcessInfo in the documentation. You will find a class method that will return an NSProcessInfo object. (In fact, you should find a very handy code sample there as well). From this object, you can access the name and process identifier (pid) of the process. Log these pieces of information to the console using NSLog in the format: Process Name: 'WhatATool' Process ID: '4556' Section Hints: • We didn’t discuss NSProcessInfo in lecture at all, so you aren’t missing any slides or notes. All of the information you need is in the NSProcessInfo class documentation. • Comparing the process info you logged with the prefix that NSLog generates can help verify you’re logging the correct information. Page 4 of 6
  • 5. CS193P Assignment 1B Winter 2010 Cannistraro/Shaffer Section 3: A little bookmark dictionary In this section, you will build a small URL bookmark repository using a mutable dictionary. Each key is an NSString that serves as the description of the URL, the value is an NSURL. Create a mutable dictionary that contains the following key/value pairs: Key (NSString) Value (NSURL) Stanford University http://www.stanford.edu Apple http://www.apple.com CS193P http://cs193p.stanford.edu Stanford on iTunes U http://itunes.stanford.edu Stanford Mall http://stanfordshop.com Enumerate through the keys of the dictionary. While enumerating through the keys, check each key to see if it starts with @”Stanford” If it does, retrieve the NSURL object for that key from the . dictionary, and log both the key string and the NSURL object in the following format below. If the key does not start with @”Stanford” no output should be printed. , Key: 'Stanford University' URL: 'http://www.stanford.edu' Section Hints: • Use +URLWithString: to create the URL instances. • NSString has methods to determine if a string has a particular prefix or suffix. Remember to log only those keys that start with ‘Stanford’. • The methods for creating and adding items to a mutable dictionary are located in the Lecture #2 slides. Remember that an NSMutableDictionary is a subclass of NSDictionary and so inherits all of its methods. • When using a dictionary and fast enumeration, you are enumerating the keys of the dictionary. You can use the key to then retrieve the value. NSDictionary also defines a method called valueEnumerator that returns an NSEnumerator object which will directly enumerate the values. You can use either approach. Section 4: Selectors, Classes and Introspection Objective-C has a number of facilities that add to its dynamic object-oriented capabilities.  Many of these facilities deal with determining and using an object's capabilities at runtime.  Create a mutable array and add objects of various types to it. Create instance of the classes we’ve used elsewhere in this assignment to populate the array: NSString, NSURL, NSProcessInfo, NSDictionary, etc. Create some NSMutableString instances and put them in the array as well. Feel free to create other kinds of objects also. Iterate through the objects in the array and do the following: 1. Print the class name of the object. 2. Log if the object is member of class NSString. 3. Log if the object is kind of class NSString. 4. Log if the object responds to the selector "lowercaseString". Page 5 of 6
  • 6. CS193P Assignment 1B Winter 2010 Cannistraro/Shaffer 5. If the object does respond to the lowercaseString selector, log the result of asking the object to perform that selector (using performSelector:) For example, if an array contained an NSString, an NSURL and an NSDictionary the output might look something like this: 2008-01-10 20:56:03 WhatATool[360] Class name: NSCFString 2008-01-10 20:56:03 WhatATool[360] Is Member of NSString: NO 2008-01-10 20:56:03 WhatATool[360] Is Kind of NSString: YES 2008-01-10 20:56:03 WhatATool[360] Responds to lowercaseString: YES 2008-01-10 20:56:03 WhatATool[360] lowercaseString is: hello world! 2008-01-10 20:56:03 WhatATool[360] ====================================== 2008-01-10 20:56:03 WhatATool[360] Class name: NSURL 2008-01-10 20:56:03 WhatATool[360] Is Member of NSString: NO 2008-01-10 20:56:03 WhatATool[360] Is Kind of NSString: NO 2008-01-10 20:56:03 WhatATool[360] Responds to lowercaseString: NO 2008-01-10 20:56:03 WhatATool[360] ====================================== 2008-01-10 20:56:03 WhatATool[360] Class name: NSCFDictionary 2008-01-10 20:56:03 WhatATool[360] Is Member of NSString: NO 2008-01-10 20:56:03 WhatATool[360] Is Kind of NSString: NO 2008-01-10 20:56:03 WhatATool[360] Responds to lowercaseString: NO 2008-01-10 20:56:03 WhatATool[360] ====================================== Section Hints: • When you ask various classes, such as NSString, for its class name, you may not get the result you expect.  Your logs should report what the runtime reports back to you - don't worry if some of the class names may seem strange, these are implementation details of the NSString class. Encapsulation at work! Page 6 of 6