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