Monday, March 4, 2013

Objective-C 101


 NSString *myString = @"This is my NSString";
    NSString *myString2 = @"This is another NSString";

    NSLog(@"I have just NSLogged this string: %@ ++ %@", myString, myString2);
    
    int i = 10;
    float x = 17.6;
    NSLog(@"This is another NSLog string with %i and %f", i + 200, x);
    
    NSString *userString = @"28";
    NSLog(@"The sum of userString and 10 is %i", [userString intValue] + 10);
    
    NSString *fourthObject = @"Fourth";
    NSArray *myArray = [NSArray arrayWithObjects:@"First", @"Second", @"Third", fourthObject, nil];
    NSLog(@"My array is %@", myArray);
    NSLog(@"The 2nd item in the array is %@", [myArray objectAtIndex:1]);
    
    NSMutableArray *myMutantArray = [NSMutableArray arrayWithObject:@"One"];
    [myMutantArray addObject:@"Two"];
    NSLog(@"My updated array becomes %@", myMutantArray);
    NSLog(@"Location of 'two' is %i",[myMutantArray indexOfObject:@"Two"]);
    [myMutantArray addObjectsFromArray:myArray];
    NSLog(@"My updated array becomes %@", myMutantArray);
    
    NSDictionary *gameData = [NSDictionary dictionaryWithObjectsAndKeys:
                              @"9340", @"highScore",
                              @"10", @"numLivesLeft",
                              @"plasma rifle", @"currentWeapon", nil];
    NSLog(@"Printing game data...%@", gameData);
    NSLog(@"What is my current weapon? %@", [gameData objectForKey:@"currentWeapon"]);

- (IBAction)buttonPressed:(id)sender {
    
    int r = rand();
    
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Hello World Title!"
                          message:[NSString stringWithFormat:@"Congrats! I have launched the msg!This is my random number: %i", r]
                          delegate:nil
                          cancelButtonTitle: @"CancelButton"
                          otherButtonTitles: nil];
    
    [alert show];
}


@interface Zombie : NSObject

@property NSString *name;
@property int age;
@property NSString *characteristics;
    
@end


Zombie *aZombie = [[Zombie alloc] init];
    [aZombie setName:@"Ted"];
    aZombie.age = 12;
    //[aZombie setAge:12];
    [aZombie setCharacteristics:@"likes to eat plants"];
    NSLog(@"The name of the Zombie is %@!", [aZombie name]);
    NSLog(@"%@ is %i years old - as such the hit points are %i", aZombie.name, aZombie.age, [aZombie getHitPoints]);
    NSLog(@"%@", [aZombie characteristics]);

No comments:

Post a Comment