Theeaxe

lynda Command Line - Main.m

Apr 30th, 2014
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  main.m
  3. //  Test-Command-Line
  4. //
  5. //  Created by Masen Beliveau on 4/27/14.
  6. //  Copyright (c) 2014 KidCO. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "Employee.h"
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14.     @autoreleasepool {
  15.        
  16.         // Creating Custom Class Section
  17.         Player *playerObject = [[Player alloc] init];
  18.         NSLog(@"The score is %i", [playerObject score]);
  19.        
  20.         Player *secondPlayer = [[Player alloc] initWithInteger:9999];
  21.          NSLog(@"The 2nd score is %i", [secondPlayer score]);
  22.        
  23.        
  24.         //Using Collections Section
  25.        
  26.        
  27.         //Make a variable that can contain mutliple numbers
  28.         int multipleVariables[5];
  29.        
  30.         //Set the value of those variables
  31.         multipleVariables[0] = 10;
  32.         multipleVariables[1] = 100;
  33.         multipleVariables[2] = 1000;
  34.         multipleVariables[3] = 10000;
  35.         multipleVariables[4] = 100000;
  36.        
  37.         //Option 2: Uses less lines of code and is much quicker
  38.         //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
  39.         int multipleVariables2[5] = { 10, 100, 1000, 10000, 100000 };
  40.        
  41.         //To test Option 2
  42.         NSLog(@"The 3rd variable is %i", multipleVariables2[2]);
  43.        
  44.         //You can also do this with strings, similiar to Python's Lists
  45.         NSString *myStringArray[5] = {@"first", @"second", @"third", @"fourth", @"fifth" };
  46.        
  47.         //To test the myStringArray
  48.         NSLog(@"The fifth string is %@", myStringArray[4]);
  49.        
  50.         //To make an array, you do the following. To end it, you just need to make sure nil is at the end
  51.         NSArray *myArray = [[NSArray alloc]initWithObjects:@"one", @"two", @"three", nil];
  52.        
  53.         //Long way to test NSArray
  54.         NSLog(@"The 2nd entry is %@", [myArray objectAtIndex:2]);
  55.        
  56.         //Short way to test NSArray
  57.         NSLog(@"The 2nd entry is %@", myArray[2]);
  58.        
  59.         //You can even add an NSDate to a NSArray
  60.         NSDate *myDate = [[NSDate alloc] init];
  61.         NSArray *myArray2 = [[NSArray alloc]initWithObjects:@"four", @"five", @"six", myDate, nil];
  62.        
  63.         //Test out myArray
  64.         NSLog(@"The date is %@", [myArray2 objectAtIndex:3]);
  65.        
  66.         //To just add a date to an Array, simply make it Mutable
  67.         NSMutableArray *myMutableArray = [[NSMutableArray alloc]initWithObjects:@"seven", @"eight", @"nine", nil];
  68.         [myMutableArray addObject:myDate];
  69.        
  70.         //Print the 3nd spot
  71.         NSLog(@"The 3rd spot is %@", myMutableArray[2]);
  72.        
  73.         //To remove the 3rd spot (or any, in the future), do the following
  74.         [myMutableArray removeObjectAtIndex:2];
  75.        
  76.         //Now, print the 3rd spot
  77.         NSLog(@"The new date is %@", myMutableArray[2]);
  78.        
  79.         //If you want to make an array quicker, more easier to read, but un-mutable, do the following
  80.         NSArray *myShorthandArray = @[ @"yellow", @"orange", @"black" ];
  81.         //You won't even need nil at the end
  82.         NSLog(@"My favorite color is %@", myShorthandArray[0]);
  83.        
  84.         //How to write a NSDictionary
  85.         NSDictionary *states = [[NSDictionary alloc]initWithObjectsAndKeys:
  86.                                 @"Arizona",@"AZ",
  87.                                 @"Massachusetts",@"MA",
  88.                                 @"California",@"CA",
  89.                                 @"New York",@"NY",
  90.                                 @"Florida",@"FL",
  91.                                 nil];
  92.         //Test it
  93.         NSString *whereILive = @"MA";
  94.         NSLog(@" %@ is for %@", whereILive, [states objectForKey:whereILive]);
  95.        
  96.         //Shorter way to print it
  97.         NSLog(@" %@ stands for %@", whereILive, states[@"MA"]);
  98.        
  99.         //How to make a mutable Dictionary
  100.         NSMutableDictionary *myMutableStates = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
  101.                                                 @"Arizona",@"AZ",
  102.                                                 @"Massachusetts",@"MA",
  103.                                                 @"California",@"CA",
  104.                                                 @"New York",@"NY",
  105.                                                 @"Florida",@"FL",
  106.                                                 nil];
  107.        
  108.         //How to add something to your Mutable Dictionary
  109.         [myMutableStates setObject:@"Nevada" forKey:@"NV"];
  110.        
  111.         NSLog(@"The Mutable Dictionary added %@", myMutableStates[@"NV"]);
  112.        
  113.         //The shorter version to make a NSDictionary. NOTE: the key goes before the object, unlike before, where it goes object, then key (REMEMBER THAT!)
  114.         NSDictionary *shorthandDictionary = @{ @"AZ" : @"Arizona", @"MA" : @"Massachusetts" };
  115.        
  116.         //Run the shorthandDictionary
  117.         NSLog(@"The state I live in is %@", shorthandDictionary[@"MA"]);
  118.        
  119.         //Enumeration, seeing what files are in a directory
  120.         NSString *soundPath = @"/System/Library/Sounds";
  121.         NSFileManager *fileManager = [[NSFileManager alloc] init];
  122.        
  123.         //Now, create an array
  124.         NSArray *soundFiles = [fileManager subpathsAtPath:soundPath];
  125.        
  126.         //Last part, the for loop
  127.         for (NSString *file in soundFiles) {
  128.             NSLog(@"The file name is %@", file);
  129.        
  130.            
  131.         //Using Collections Section
  132.        
  133.         //Using the NSFileManager again
  134.         NSFileManager *fileManager = [[NSFileManager alloc] init];
  135.         NSString *pdfPath = @"/Users/Masen/Downloads/P.E.R.T_Study_Guide.pdf";
  136.            
  137.             if ([fileManager fileExistsAtPath:pdfPath]) {
  138.                 NSLog(@"The PDF Exists");
  139.             } else {
  140.                 NSLog(@"The PDF doesn't Exist");
  141.                 return 1;
  142.             }
  143.            
  144.         //How to get an NSDictionary of attributes
  145.         NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:pdfPath error:nil];
  146.            
  147.         for (NSString *key in fileAttributes) {
  148.         NSLog(@"The attribute key is: %@  and the value is %@ ", key, fileAttributes[key]);
  149.             }
  150.        
  151.         //Simple Directory Funtion Call
  152.         NSString *homeDirectory = NSHomeDirectory();
  153.            
  154.         //NSString for Downloads
  155.         NSString *desktopPath = [homeDirectory stringByAppendingPathComponent:@"Downloads"];
  156.         NSLog(@"The path is: %@", desktopPath);
  157.        
  158.         NSString *pertGuidePath = [desktopPath stringByAppendingPathComponent:@"P.E.R.T_Study_Guide.pdf"];
  159.         NSLog(@"The PERT Guide Path is: %@", pertGuidePath);
  160.        
  161.            
  162.         NSLog(@"Calling NSHomeDirectory() result in: %@", homeDirectory);
  163.        
  164.         //Another way to make an NSFileManager
  165.         NSFileManager *simpleFileManager = [NSFileManager defaultManager];
  166.        
  167.         //Now make a NSURL, NOTE: Use the QUICK HELP to see what the different entries are for and what different things you can put
  168.         NSURL *docsDirectory = [simpleFileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil
  169.                                                            create:NO error:nil];
  170.        
  171.         //The main thing that determines what file is gotten from above is what the URLForDirectory is
  172.         NSLog(@"The simpleFileManager method returned: %@", docsDirectory);
  173.            
  174.         //Practice makes PERFECT!
  175.             NSURL *documentsDir = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil
  176.                                     create:NO error:nil];
  177.         //Now, create a full path
  178.             NSURL *full = [documentsDir URLByAppendingPathComponent:@"sample.txt"];
  179.            
  180.         //Load a string
  181.             NSString *content = [[NSString alloc] initWithContentsOfURL:full encoding:NSUTF8StringEncoding error:nil];
  182.            
  183.         //Print it
  184.             NSLog(@"The string is: %@", content);
  185.        
  186.         //How to make a Mutatable String
  187.             NSURL *anotherDocumentsDir = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
  188.         //Creat full path again
  189.             NSURL *anotherFull = [anotherDocumentsDir URLByAppendingPathComponent:@"anothersample.txt"];
  190.            
  191.         //Load it, yet again, but with Mutable String
  192.             NSMutableString *anotherContent = [[NSMutableString alloc] initWithContentsOfURL:anotherFull encoding:NSUTF8StringEncoding error:nil];
  193.        
  194.         //New string
  195.             [anotherContent appendString:@"CHANGED!"];
  196.            
  197.         //Destination URL
  198.             NSURL *saveLocation = [anotherDocumentsDir URLByAppendingPathComponent:@"saved.txt"];
  199.             [anotherContent writeToURL:saveLocation atomically:YES encoding:NSUTF8StringEncoding error:nil];
  200.            
  201.         //How to Archive/Encode things into a file NOTE: Some code is needed in Player.h & Player.m
  202.         //Create the Employee Object
  203.             Employee *bob = [[Employee alloc] init];
  204.             [bob setFirstName:@"Bob"];
  205.             [bob setLastName:@"Laso"];
  206.             [bob setEmployeeNumber:12345];
  207.             [bob setHireDate:[NSDate date]];
  208.            
  209.         //Employee #2
  210.             Employee *alice = [[Employee alloc] init];
  211.             alice.FirstName = @"Alice";
  212.             alice.lastName = @"Naltz";
  213.             alice.employeeNumber = 6789;
  214.             alice.hireDate = [NSDate dateWithString:@"2010-03-24 09:00:00 -800"];
  215.            
  216.         //Print it out to test it
  217.             NSLog(@"First Employee: \n%@", [bob description]);
  218.             NSLog(@"Second Employee: \n%@", [alice description]);
  219.            
  220.         }
  221.        
  222.     }
  223.     return 0;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment