Created
January 23, 2013 02:26
-
-
Save b-adams/4601290 to your computer and use it in GitHub Desktop.
URL, Array, and string-input demonstrations for CS132
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
@brief Demonstrate getting textual user input and adding it to an array, plus displaying array contents. | |
*/ | |
void demo_Array(void); | |
void demo_Array(void) | |
{ | |
//An array of "to do" items, which can be altered | |
NSMutableArray* todoList = nil; | |
//Pointing todoList at an allocated, initialized Mutable Array instance | |
todoList = [[NSMutableArray alloc] init]; | |
//A string to keep track of a single to-do item | |
NSString* todoItem = nil; | |
//A C-style string for storing user input text | |
char userInput[100]; | |
//Loop five times to get user inputs | |
for(int i=0; i<5; i++) | |
{ | |
//Prompt (CS131 style) | |
printf("Enter todo item %d: ", i); | |
//Scan input (CS131 style) | |
scanf("%99s", userInput); | |
//Convert C-string (array of characters) to NSString (String object) | |
todoItem = [[NSString alloc] initWithCString:userInput | |
encoding:NSASCIIStringEncoding]; | |
//Mutate the list by adding an item to it | |
[todoList addObject:todoItem]; | |
} | |
//Mutate the list by changing its order | |
[todoList sortUsingSelector: @selector(localizedCaseInsensitiveCompare:)]; | |
//Loop over every item in the list and display it | |
for(NSString* item in todoList) | |
{ | |
NSLog(@"You should do: %@", item); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
@brief Demonstrate constructing a URL from strings and fetching HTML at the targeted location. | |
*/ | |
void demo_URL(void); | |
void demo_URL(void) | |
{ | |
//A string, like you'd type to go to a server | |
NSString* webserverAddress = @"http://henry.wells.edu/"; | |
//A URL (Uniform Resource Location) object based on that string | |
NSURL* webserverURL = [NSURL URLWithString:webserverAddress]; | |
//A string, like you'd type to finish off a web address | |
NSString* pbkInitiativesAddress = @"~pbk/index.html#Initiatives"; | |
//A URL, based on a relative path and another, base, URL | |
NSURL* pbkURL = [NSURL URLWithString:pbkInitiativesAddress | |
relativeToURL:webserverURL]; | |
//The content you get by loading that URL | |
NSString* contents = [NSString stringWithContentsOfURL:pbkURL | |
usedEncoding:NULL | |
error:NULL]; | |
//Display one particular string version of the URL, as well as the contents it indicated | |
NSLog(@"Contents of %@: \n\n%@", [pbkURL absoluteString], contents); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment