Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.m
- // Test-Command-Line
- //
- // Created by Masen Beliveau on 4/27/14.
- // Copyright (c) 2014 KidCO. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "Employee.h"
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- // Creating Custom Class Section
- Player *playerObject = [[Player alloc] init];
- NSLog(@"The score is %i", [playerObject score]);
- Player *secondPlayer = [[Player alloc] initWithInteger:9999];
- NSLog(@"The 2nd score is %i", [secondPlayer score]);
- //Using Collections Section
- //Make a variable that can contain mutliple numbers
- int multipleVariables[5];
- //Set the value of those variables
- multipleVariables[0] = 10;
- multipleVariables[1] = 100;
- multipleVariables[2] = 1000;
- multipleVariables[3] = 10000;
- multipleVariables[4] = 100000;
- //Option 2: Uses less lines of code and is much quicker
- //You don't even need to put [5], you can just delete that and put in { 10, 100, 1000, 10000, 100000 }; and it would still work just the same
- int multipleVariables2[5] = { 10, 100, 1000, 10000, 100000 };
- //To test Option 2
- NSLog(@"The 3rd variable is %i", multipleVariables2[2]);
- //You can also do this with strings, similiar to Python's Lists
- NSString *myStringArray[5] = {@"first", @"second", @"third", @"fourth", @"fifth" };
- //To test the myStringArray
- NSLog(@"The fifth string is %@", myStringArray[4]);
- //To make an array, you do the following. To end it, you just need to make sure nil is at the end
- NSArray *myArray = [[NSArray alloc]initWithObjects:@"one", @"two", @"three", nil];
- //Long way to test NSArray
- NSLog(@"The 2nd entry is %@", [myArray objectAtIndex:2]);
- //Short way to test NSArray
- NSLog(@"The 2nd entry is %@", myArray[2]);
- //You can even add an NSDate to a NSArray
- NSDate *myDate = [[NSDate alloc] init];
- NSArray *myArray2 = [[NSArray alloc]initWithObjects:@"four", @"five", @"six", myDate, nil];
- //Test out myArray
- NSLog(@"The date is %@", [myArray2 objectAtIndex:3]);
- //To just add a date to an Array, simply make it Mutable
- NSMutableArray *myMutableArray = [[NSMutableArray alloc]initWithObjects:@"seven", @"eight", @"nine", nil];
- [myMutableArray addObject:myDate];
- //Print the 3nd spot
- NSLog(@"The 3rd spot is %@", myMutableArray[2]);
- //To remove the 3rd spot (or any, in the future), do the following
- [myMutableArray removeObjectAtIndex:2];
- //Now, print the 3rd spot
- NSLog(@"The new date is %@", myMutableArray[2]);
- //If you want to make an array quicker, more easier to read, but un-mutable, do the following
- NSArray *myShorthandArray = @[ @"yellow", @"orange", @"black" ];
- //You won't even need nil at the end
- NSLog(@"My favorite color is %@", myShorthandArray[0]);
- //How to write a NSDictionary
- NSDictionary *states = [[NSDictionary alloc]initWithObjectsAndKeys:
- @"Arizona",@"AZ",
- @"Massachusetts",@"MA",
- @"California",@"CA",
- @"New York",@"NY",
- @"Florida",@"FL",
- nil];
- //Test it
- NSString *whereILive = @"MA";
- NSLog(@" %@ is for %@", whereILive, [states objectForKey:whereILive]);
- //Shorter way to print it
- NSLog(@" %@ stands for %@", whereILive, states[@"MA"]);
- //How to make a mutable Dictionary
- NSMutableDictionary *myMutableStates = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
- @"Arizona",@"AZ",
- @"Massachusetts",@"MA",
- @"California",@"CA",
- @"New York",@"NY",
- @"Florida",@"FL",
- nil];
- //How to add something to your Mutable Dictionary
- [myMutableStates setObject:@"Nevada" forKey:@"NV"];
- NSLog(@"The Mutable Dictionary added %@", myMutableStates[@"NV"]);
- //The shorter version to make a NSDictionary. NOTE: the key goes before the object, unlike before, where it goes object, then key (REMEMBER THAT!)
- NSDictionary *shorthandDictionary = @{ @"AZ" : @"Arizona", @"MA" : @"Massachusetts" };
- //Run the shorthandDictionary
- NSLog(@"The state I live in is %@", shorthandDictionary[@"MA"]);
- //Enumeration, seeing what files are in a directory
- NSString *soundPath = @"/System/Library/Sounds";
- NSFileManager *fileManager = [[NSFileManager alloc] init];
- //Now, create an array
- NSArray *soundFiles = [fileManager subpathsAtPath:soundPath];
- //Last part, the for loop
- for (NSString *file in soundFiles) {
- NSLog(@"The file name is %@", file);
- //Using Collections Section
- //Using the NSFileManager again
- NSFileManager *fileManager = [[NSFileManager alloc] init];
- NSString *pdfPath = @"/Users/Masen/Downloads/P.E.R.T_Study_Guide.pdf";
- if ([fileManager fileExistsAtPath:pdfPath]) {
- NSLog(@"The PDF Exists");
- } else {
- NSLog(@"The PDF doesn't Exist");
- return 1;
- }
- //How to get an NSDictionary of attributes
- NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:pdfPath error:nil];
- for (NSString *key in fileAttributes) {
- NSLog(@"The attribute key is: %@ and the value is %@ ", key, fileAttributes[key]);
- }
- //Simple Directory Funtion Call
- NSString *homeDirectory = NSHomeDirectory();
- //NSString for Downloads
- NSString *desktopPath = [homeDirectory stringByAppendingPathComponent:@"Downloads"];
- NSLog(@"The path is: %@", desktopPath);
- NSString *pertGuidePath = [desktopPath stringByAppendingPathComponent:@"P.E.R.T_Study_Guide.pdf"];
- NSLog(@"The PERT Guide Path is: %@", pertGuidePath);
- NSLog(@"Calling NSHomeDirectory() result in: %@", homeDirectory);
- //Another way to make an NSFileManager
- NSFileManager *simpleFileManager = [NSFileManager defaultManager];
- //Now make a NSURL, NOTE: Use the QUICK HELP to see what the different entries are for and what different things you can put
- NSURL *docsDirectory = [simpleFileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil
- create:NO error:nil];
- //The main thing that determines what file is gotten from above is what the URLForDirectory is
- NSLog(@"The simpleFileManager method returned: %@", docsDirectory);
- //Practice makes PERFECT!
- NSURL *documentsDir = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil
- create:NO error:nil];
- //Now, create a full path
- NSURL *full = [documentsDir URLByAppendingPathComponent:@"sample.txt"];
- //Load a string
- NSString *content = [[NSString alloc] initWithContentsOfURL:full encoding:NSUTF8StringEncoding error:nil];
- //Print it
- NSLog(@"The string is: %@", content);
- //How to make a Mutatable String
- NSURL *anotherDocumentsDir = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
- //Creat full path again
- NSURL *anotherFull = [anotherDocumentsDir URLByAppendingPathComponent:@"anothersample.txt"];
- //Load it, yet again, but with Mutable String
- NSMutableString *anotherContent = [[NSMutableString alloc] initWithContentsOfURL:anotherFull encoding:NSUTF8StringEncoding error:nil];
- //New string
- [anotherContent appendString:@"CHANGED!"];
- //Destination URL
- NSURL *saveLocation = [anotherDocumentsDir URLByAppendingPathComponent:@"saved.txt"];
- [anotherContent writeToURL:saveLocation atomically:YES encoding:NSUTF8StringEncoding error:nil];
- //How to Archive/Encode things into a file NOTE: Some code is needed in Player.h & Player.m
- //Create the Employee Object
- Employee *bob = [[Employee alloc] init];
- [bob setFirstName:@"Bob"];
- [bob setLastName:@"Laso"];
- [bob setEmployeeNumber:12345];
- [bob setHireDate:[NSDate date]];
- //Employee #2
- Employee *alice = [[Employee alloc] init];
- alice.FirstName = @"Alice";
- alice.lastName = @"Naltz";
- alice.employeeNumber = 6789;
- alice.hireDate = [NSDate dateWithString:@"2010-03-24 09:00:00 -800"];
- //Print it out to test it
- NSLog(@"First Employee: \n%@", [bob description]);
- NSLog(@"Second Employee: \n%@", [alice description]);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment