Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

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