Amazon.com Widgets Program 15.16
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 12:42:22 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Program Examples
| | |-+  Chapter 15
| | | |-+  Program 15.16
Pages: [1]   Go Down
Print
Author Topic: Program 15.16  (Read 1230 times)
TotalLuck
Full Member
***
Posts: 107



WWW Email




« on: February 15, 2009, 05:48:49 PM »

Code: (Objective-C)
#import <Foundation/Foundation.h>

//create an integer object
#define INTOBJ(v) [NSNumber numberWithInteger: v]

// add a print method to NSSet with the printing catagory
@interface NSSet (Printing);
-(void) print;
@end

@implementation NSSet (Printing);
-(void) print
{
printf("{");  // NOT NSLOG  because we need to print on same line

for (NSNumber *element in self)
printf (" %li ", (long) [element integerValue]);

printf("}\n");
}
@end



int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSMutableSet *set1 = [NSMutableSet setWithObjects: INTOBJ(1),
   INTOBJ(3), INTOBJ(5), INTOBJ(10), nil];
NSMutableSet *set2 = [NSMutableSet setWithObjects: INTOBJ(-5),
   INTOBJ(100), INTOBJ(3), INTOBJ(5), nil];
NSMutableSet *set3 = [NSMutableSet setWithObjects: INTOBJ(12),
   INTOBJ(200), INTOBJ(3), nil];
NSLog(@"set1: ");
[set1 print];

NSLog(@"set2: ");
[set2 print];


// equalty test

if ([set1 isEqualToSet: set2] == YES)
NSLog(@"set1 equals set2");
else
NSLog(@"set1 does not equal set2");

// membership test

if ([set1 containsObject: INTOBJ(10)] == YES)
NSLog(@"set1 contains 10");
else
NSLog(@"set1 does not contain 10");

if ([set2 containsObject:INTOBJ(10)] == YES)
NSLog(@"set2 contains 10");
else
NSLog(@"set2 does not contain 10");

// add and remove objects from mutable set set1

[set1 addObject: INTOBJ(4)];
[set1 removeObject:INTOBJ(10)];
NSLog(@"set1 after adding 4 and removing 10: ");
[set1 print];

//get intersection of two sets

[set1 intersectSet: set2];
NSLog(@"set 1 intersect set2: ");
[set1 print];

//union of two sets

[set1 unionSet: set3];
NSLog(@"set1 union set3: ");
[set1 print];

[pool drain];
    return 0;
}
[/pre]
Logged

Apps available on  iTunes store:
"ADACode"  iPhone and iPad versions, "ADAGuidelines" iPhone & iPad versions,  "Rehabilitation Act of 1973" for iPhone
"APokerTimer" now for iPhone http://bit.ly/h1fAJp
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.