4/13/2010

Cocoa Maker 7 - Libraries and Frameworks

James (MrGeckosMedia.com), joined by, Eduardo (MegaEduX.com), and Nick, explains libraries and frameworks.

11:56


Example code from episode.
Libraries
Libraries included, /usr/lib/libcrypto.dylib and /usr/lib/libssl.dylib
#import <Foundation/Foundation.h>
#import <openssl/evp.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
 NSString *string = @"Hello";
 
 OpenSSL_add_all_algorithms();
 unsigned char outbuf[EVP_MAX_MD_SIZE];
 unsigned int templen, inlen = [string length];
 const char *input = [string UTF8String];
 EVP_MD_CTX ctx;
 
 const EVP_MD *digest = EVP_md5();
 if(!digest) {
  NSLog(@"cannot get digest with name MD5");
  return 1;
 }
 
 EVP_MD_CTX_init(&ctx);
 EVP_DigestInit(&ctx,digest);
 if(!EVP_DigestUpdate(&ctx,input,inlen)) {
  NSLog(@"EVP_DigestUpdate() failed!");
  EVP_MD_CTX_cleanup(&ctx);
  return 1;   
 }
 if (!EVP_DigestFinal(&ctx, outbuf, &templen)) {
  NSLog(@"EVP_DigesttFinal() failed!");
  EVP_MD_CTX_cleanup(&ctx);
  return 1;
 }
 EVP_MD_CTX_cleanup(&ctx);
 
 NSMutableString *md5 = [NSMutableString string];
 for (int i=0; i<templen; i++) {
  [md5 appendFormat:@"%x", outbuf[i]];
 }
 NSLog(@"MD5 of %@ is %@", string, md5);
 
    [pool drain];
    return 0;
}
Frameworks
Frameworks included, AddressBook.framework
#import <Foundation/Foundation.h>
#import <AddressBook/AddressBook.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
 ABAddressBook *addressBook = [ABAddressBook sharedAddressBook];
 NSArray *people = [addressBook people];
 for (int i=0; i<[people count]; i++) {
  ABPerson *person = [people objectAtIndex:i];
  NSString *firstName = [person valueForProperty:kABFirstNameProperty];
  NSString *lastName = [person valueForProperty:kABLastNameProperty];
  NSString *name = nil;
  if (firstName!=nil) {
   name = firstName;
   if (lastName!=nil)
    name = [name stringByAppendingFormat:@" %@", lastName];
  }
  if (name!=nil)
   NSLog(@"Person named: %@", name);
 }
 
    [pool drain];
    return 0;
}

Keynote used in this Episode
Keynote in PDF Format

3/22/2010

Cocoa Maker 6 - Arrays, Dictionaries, and Property Lists

James (MrGeckosMedia.com), joined by, Eduardo (MegaEduX.com), and John (@ZimiPoder), shows how Dictionaries, Arrays and Property Lists works in Cocoa.

12:40


Example code from episode.
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    NSLog(@"Array");
    NSArray *array = [NSArray arrayWithObjects:@"Orange", @"Apple", @"Banana", nil];
    for (int i=0; i<[array count]; i++) {
        NSLog(@"%@", [array objectAtIndex:i]);
    }
    
    NSLog(@"Sorting Array");
    array = [array sortedArrayUsingSelector:@selector(compare:)];
    for (int i=0; i<[array count]; i++) {
        NSLog(@"%@", [array objectAtIndex:i]);
    }
    
    NSLog(@"Saving Property List");
    [array writeToFile:[@"~/Desktop/array.plist" stringByExpandingTildeInPath] atomically:YES];
    
    NSLog(@"Mutable Array");
    NSMutableArray *mutableArray = [NSMutableArray array];
    [mutableArray addObject:@"Apples"];
    [mutableArray addObject:@"Oranges"];
    [mutableArray addObject:@"Bananas"];
    for (int i=0; i<[mutableArray count]; i++) {
        NSLog(@"%@", [mutableArray objectAtIndex:i]);
    }
    
    NSLog(@"Removing object Oranges");
    [mutableArray removeObjectAtIndex:1];
    for (int i=0; i<[mutableArray count]; i++) {
        NSLog(@"%@", [mutableArray objectAtIndex:i]);
    }
    
    NSLog(@"Dictionary");
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Oranges", @"Name", @"Fruit", @"Type", [NSNumber numberWithBool:YES], @"Seeds", nil];
    NSLog(@"Adding %@ with name %@", [dictionary objectForKey:@"Type"], [dictionary objectForKey:@"Name"]);
    [mutableArray insertObject:dictionary atIndex:1];
    for (int i=0; i<[mutableArray count]; i++) {
        NSLog(@"%@", [mutableArray objectAtIndex:i]);
    }
    
    NSLog(@"Saving Property List using NSPropertyListSerialization");
    NSData *xmlData = [NSPropertyListSerialization dataFromPropertyList:mutableArray format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];
    [xmlData writeToFile:[@"~/Desktop/mutableArray.plist" stringByExpandingTildeInPath] atomically:YES];
    
    NSLog(@"Mutable Dictionary");
    NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];
    [mutableDictionary setObject:@"Salad" forKey:@"Name"];
    [mutableDictionary setObject:@"Veggie" forKey:@"Type"];
    [mutableDictionary setObject:[NSDate date] forKey:@"Time"];
    [mutableDictionary setObject:[NSNumber numberWithBool:NO] forKey:@"Seeds"];
    NSLog(@"Adding %@ with name %@", [mutableDictionary objectForKey:@"Type"], [mutableDictionary objectForKey:@"Name"]);
    [mutableArray insertObject:mutableDictionary atIndex:0];
    for (int i=0; i<[mutableArray count]; i++) {
        NSLog(@"%@", [mutableArray objectAtIndex:i]);
    }
    
    NSLog(@"Saving Binary Property List");
    NSData *binaryData = [NSPropertyListSerialization dataFromPropertyList:mutableArray format:NSPropertyListBinaryFormat_v1_0 errorDescription:nil];
    [binaryData writeToFile:[@"~/Desktop/bina    ry.plist" stringByExpandingTildeInPath] atomically:YES];
    
    [pool drain];
    return 0;
}

Keynote used in this Episode
Keynote in PDF Format

3/19/2010

Cocoa Maker 5 - File Management

James (MrGeckosMedia.com), joined by, Noah (RockntheSweater.com), and Karl, explains how file managing works in c and cocoa.

7:49


Example code from episode.
Files C Example
#import <Foundation/Foundation.h>

BOOL file_exist(const char *fileName) {
    FILE *file = fopen(fileName, "r");
    if (file) {
        fclose(file);
        return YES;
    }
    return NO;
}

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    const char *ourFile = [[@"~/Desktop/file.txt" stringByExpandingTildeInPath] UTF8String];
    FILE *file;
    
    if (!file_exist(ourFile)) {
        file = fopen(ourFile, "w");
        time_t currTime = time(NULL);
        fprintf(file, "%s: We just made this file.\n", ctime(&currTime));
    } else {
        file = fopen(ourFile, "a");
    }
    time_t currTime = time(NULL);
    fprintf(file, "%s: Here is a new line in this file.\n", ctime(&currTime));
    fclose(file);

    
    [pool drain];
    return 0;
}
Files Cocoa Exmaple
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *ourFile = [@"~/Desktop/file.txt" stringByExpandingTildeInPath];
    NSFileHandle *file;
    
    if (![manager fileExistsAtPath:ourFile]) {
        [manager createFileAtPath:ourFile contents:nil attributes:nil];
        file = [NSFileHandle fileHandleForWritingAtPath:ourFile];
        [file writeData:[[NSString stringWithFormat:@"%@: Here is a new file.\n", [NSDate date]] dataUsingEncoding:NSUTF8StringEncoding]];
    } else {
        file = [NSFileHandle fileHandleForWritingAtPath:ourFile];
        [file seekToEndOfFile];
    }
    [file writeData:[[NSString stringWithFormat:@"%@: Here is a new line.\n", [NSDate date]] dataUsingEncoding:NSUTF8StringEncoding]];
    [file closeFile];
    
    [pool drain];
    return 0;
}

Keynote used in this Episode
Keynote in PDF Format

2/22/2010

Cocoa Maker 4 - Loops and Goto

James (MrGeckosMedia.com), joined by, Eduardo (MegaEduX.com), and Karl, teaches how loops, arguments, and goto works in Cocoa.

15:35


Example code from episode.
Loops Example
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    int i=0;
    while (i!=2) {
        NSLog(@"%d", i);
        i++;
    }
    i=0;
    do {
        NSLog(@"%d", i);
        i++;
    } while (i!=3);
    
    NSArray *anArray = [NSArray arrayWithObjects:@"Apples", @"Oranges", @"Grapes", @"Bananas", nil];
    for (int d=0; d<[anArray count]; d++) {
        NSLog(@"%@", [anArray objectAtIndex:d]);
    }
    
    NSString *theObject;
    for (theObject in anArray) {
        NSLog(@"For In: %@", theObject);
    }
    
    NSEnumerator *theEnumerator = [anArray objectEnumerator];
    while (theObject = [theEnumerator nextObject]) {
        NSLog(@"Enumerator: %@", theObject);
    }
    
    [pool drain];
    return 0;
}
Goto Example
#import <Foundation/Foundation.h>

void testGoto() {
    NSString *theString = [NSString new];
    if (1==12) {
        goto Error;
    }
    goto Cleanup;
Error:
    NSLog(@"An error occurred");
    goto Cleanup;
Cleanup:
    NSLog(@"Releasing the string");
    [theString release];
}

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    testGoto();
    
    [pool drain];
    return 0;
}

Keynote used in this Episode
Keynote in PDF Format

2/06/2010

Cocoa Maker 3 - Documentation

James (MrGeckosMedia.com), joined by, Eduardo (MegaEduX.com), Martin (QueenZSoftware.com), John (@zimipoder), and Garrett, teaches how to read the documentation that comes with xcode.

10:48


Keynote used in this Episode
Keynote in PDF Format

1/31/2010

Cocoa Maker 2 - Syntax

James (MrGeckosMedia.com), joined by, Eduardo (MegaEduX.com), Noah (RockntheSweater.com), and Martin (QueenZSoftware.com), teaches about the syntax of cocoa, how equations work in cocoa, how to do if statements, functions, and classes.


33:56


Example code from episode.
Example 1 - If Statements and Equations
#import <Foundation/Foundation.h>

int times(int value, int by);

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int value = 1230;
    NSLog(@"Value: %d", value);
    NSLog(@"Value: %d", value%500);
    NSLog(@"Value: %d", value/100);
    NSLog(@"Value: %d", times(value, 4232));
    
    if (value>1242) {
        NSLog(@"%d is greater then 1242", value);
    } else if (value>=1242) {
        NSLog(@"%d is greater then or equal to 1242", value);
    } else if (value<1240) {
        NSLog(@"%d is less then 1242", value);
    } else if (value<=1240) {
        NSLog(@"%d is less then or equal to 1242", value);
    }
    [pool drain];
    return 0;
}

int times(int value, int by) {
    int returnValue = value*by;
    return returnValue;
}


Example 2 - Classes and Methods
#import <Foundation/Foundation.h>
#import "MGMCalculation.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    MGMCalculation *calculation = [[MGMCalculation alloc] init];
    [calculation setAmount:572];
    [calculation setCurrencyExchange:3];
    int amount = [calculation calculate];
    NSLog(@"%d", amount);
    [calculation release];
    
    [pool drain];
    return 0;
}
MGMCalculation.h
#import <Foundation/Foundation.h>

@interface MGMCalculation : NSObject {
    int amount;
    int currencyExchange;
}
- (int)amount;
- (void)setAmount:(int)theAmount;

- (int)currencyExchange;
- (void)setCurrencyExchange:(int)theCurrencyExchange;

- (int)calculate;
@end
MGMCalculation.m
#import "MGMCalculation.h"

@implementation MGMCalculation
- (id)init {
    if (self = [super init]) {
        amount = 0;
        currencyExchange = 0;
    }
    return self;
}

- (int)amount {
    return amount;
}
- (void)setAmount:(int)theAmount {
    amount = theAmount;
}

- (int)currencyExchange {
    return currencyExchange;
}
- (void)setCurrencyExchange:(int)theCurrencyExchange {
    currencyExchange = theCurrencyExchange;
}

- (int)calculate {
    return amount*currencyExchange;
}
@end

Keynote used in this Episode
Keynote in PDF Format

1/25/2010

Cocoa Maker Wallpapers

I made a icon for Cocoa Maker along with some wallpapers which I'm allowing you to download. I'm going to be using this wallpaper in my podcast for now on.

Sizes
Download 640x480
Download 1024x768
Download 1280x800
Download 1920x1200

Let me know what you think,
Mr. Gecko

1/24/2010

Cocoa Maker 1 - Getting Started

James (MrGeckosMedia.com), joined by, Eduardo (MegaEduX.com), Nick, and Karl, teaches the basics of installing Xcode, setting up a project and making a "Hello, World!" command line utility.

30:16
Example code from episode.
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSString *name = @"World";
    if (argc>1) {
        name = [[[NSString alloc] initWithBytes:argv[1] length:strlen(argv[1]) encoding:NSUTF8StringEncoding] autorelease];
    }
    NSLog(@"Hello, %@", name);
    [pool drain];
    return 0;
}