Solving the EXC_BAD_ACCESS in WhatATool Part 2
- by Allen
#import <Cocoa/Cocoa.h>
@interface PolygonShape : NSObject
{
int numberOfSides, maximumNumberOfSides, minimumNumberOfSides;
}
@property (readwrite) int numberOfSides, maximumNumberOfSides, minimumNumberOfSides;
@property (readonly) float angleInDegrees, angleInRadians;
@property (readonly) NSString * name;
@property (readonly) NSString * description;
-(id) init;
-(void) setNumberOfSides:(int)sides;
-(void) setMinimumNumberOfSides:(int)min;
-(void) setMaximumNumberOfSides:(int)max;
-(float) angleInDegrees;
-(float) angleInRadians;
-(NSString *) name;
-(id) initWithNumberOfSides:(int) sides minimumNumberOfSides:(int) min maximumNumberOfSides:(int) max;
-(NSString *) description;
-(void) dealloc;
@end
#import "PolygonShape.h"
@implementation PolygonShape
-(id) init
{
return [self initWithNumberOfSides:4 minimumNumberOfSides:3 maximumNumberOfSides:5];
}
@synthesize numberOfSides, minimumNumberOfSides, maximumNumberOfSides, angleInRadians;
-(void) setNumberOfSides:(int)sides
{
numberOfSides = sides;
NSLog(@"The number of sides is off limit so the number of sides is %@.",sides);
}
-(void)setMaximumNumberOfSides:(int)max
{
if (maximumNumberOfSides <= 12)
{
maximumNumberOfSides = max;
}
}
-(void)setMinimumNumberOfSides: (int)min
{
if (minimumNumberOfSides > 2)
{
minimumNumberOfSides = min;
}
}
- (id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min
maximumNumberOfSides:(int)max
{
if(self=[super init])
{
[self setNumberOfSides:(int)sides];
[self setMaximumNumberOfSides:(int)max];
[self setMinimumNumberOfSides: (int)min];
}
return self;
}
-(float) angleInDegrees
{
float anglesInDegrees = (180 * (numberOfSides - 2) / numberOfSides);
return anglesInDegrees;
}
-(float)angleInRadiants
{
float anglesInRadiants = ((180 * (numberOfSides - 2) / numberOfSides) * (180 / M_PI));
return anglesInRadiants;
}
-(NSString *)name
{
NSString * output;
switch (numberOfSides)
{
case 3:
output = @"Triangle";
break;
case 4:
output = @"Square";
break;
case 5:
output = @"Pentagon";
break;
case 6:
output = @"Hexagon";
break;
case 7:
output = @"Heptagon";
break;
case 8:
output = @"Octagon";
break;
case 9:
output = @"Nonagon";
break;
case 10:
output = @"Decagon";
break;
case 11:
output = @"Hendecagon";
break;
case 12:
output = @"Dodecabgon";
break;
default:
output = @"Invalid number of sides: %i is greater than maximum of five allowed.";
}
return output;
}
-(NSString *)description
{
NSString * output;
NSLog(@"Hello I am a %i-sided polygon (aka a %@) with angles of %f degrees (%f radians).",
numberOfSides, output, [self angleInDegrees], [self angleInRadiants]);
return [self description];
}
-(void)dealloc
{
[super dealloc];
}
@end
#import <Foundation/Foundation.h>
#import "PolygonShape.h"
void PrintPathInfo()
{
NSLog(@"Section 1");
NSLog(@"--------------------");
NSString *path = [@"~" stringByExpandingTildeInPath];
NSLog(@"My home folder is at '%@'.", path);
NSArray *pathComponent = [path pathComponents];
for (path in pathComponent)
{
NSLog(@"%@",path);
}
NSLog(@"--------------------");
NSLog(@"\n");
}
void PrintProcessInfo()
{
NSLog(@"Section 2");
NSLog(@"--------------------");
NSString * processName = [[NSProcessInfo processInfo] processName];
int processIdentifier = [[NSProcessInfo processInfo] processIdentifier];
NSLog(@"Process Name: '%@', Process ID: '%i'", processName, processIdentifier);
NSLog(@"--------------------");
NSLog(@"\n");
}
void PrintBookmarkInfo()
{
NSLog(@"Section 3");
NSLog(@"--------------------");
NSArray * keys = [NSArray arrayWithObjects:
@"Stanford University",
@"Apple",
@"CS193P",
@"Stanford on iTunes U",
@"Stanford Mall", nil];
NSArray * objects = [NSArray arrayWithObjects:
[NSURL URLWithString: @"http://www.stanford.edu"],
@"http://www.apple.com",
@"http://cs193p.stanford.edu",
@"http://itunes.stanford.edu",
@"http://stanfordshop.com",nil];
NSMutableDictionary * dictionary = [NSMutableDictionary dictionaryWithObjects:objects forKeys:keys];
NSEnumerator * enumerator = [keys objectEnumerator];
for (id keys in dictionary)
{
NSLog(@"key: '%@', value: '%@'", keys, [dictionary objectForKey:keys]);
}
NSLog(@" ");
NSLog(@"These are the ones that has the prefix 'Stanford'.");
NSLog(@" ");
id object;
while (object = [enumerator nextObject])
{
if ([object hasPrefix: @"Stanford"])
{
NSLog(@"key: '%@', value: '%@'", object, [dictionary objectForKey:object]);
}
}
NSLog(@"--------------------");
NSLog(@"\n");
}
void PrintIntrospectionInfo()
{
NSLog(@"Section 4");
NSLog(@"--------------------");
SEL lowercase = @selector (lowercaseString);
NSMutableArray * array = [NSMutableArray array];
[array addObject: [NSString stringWithString: @"Here is a string"]];
[array addObject: [NSDictionary dictionary]];
[array addObject: [NSURL URLWithString: @"http://www.stanford.edu"]];
[array addObject: [[NSProcessInfo processInfo]processName]];
for (id keys in array)
{
NSLog(@"\n");
NSLog(@"Class Name: %@", [keys className]);
NSLog(@"Is Member of NSString: %@", [keys isMemberOfClass:[NSString class]]?@"Yes":@"No");
NSLog(@"Is Kind of NSString: %@", [keys isKindOfClass:[NSString class]]?@"Yes":@"No");
if ([keys respondsToSelector: lowercase]==YES)
{
NSLog(@"Responds to lowercaseString: %@",[keys respondsToSelector: lowercase]?@"Yes":@"No");
NSLog(@"lowercaseString is: %@", [keys performSelector: lowercase]);
}
else
{
NSLog(@"Responds to lowercaseString: %@",[keys respondsToSelector: lowercase]?@"Yes":@"No" );
}
}
NSLog(@"--------------------");
}
void PrintPolygonInfo()
{
NSMutableArray * array = [NSMutableArray array];
PolygonShape * polygon1 = [[PolygonShape alloc]initWithNumberOfSides:4 minimumNumberOfSides:3 maximumNumberOfSides:7];
[array addObject:polygon1];
[array description];
PolygonShape * polygon2 = [[PolygonShape alloc]initWithNumberOfSides:6 minimumNumberOfSides:5 maximumNumberOfSides:9];
[array addObject:polygon2];
[array description];
PolygonShape * polygon3 = [[PolygonShape alloc]initWithNumberOfSides:12 minimumNumberOfSides:9 maximumNumberOfSides:12];
[array addObject:polygon3];
[array description];
[array release];
[polygon1 release];
[polygon2 release];
[polygon3 release];
}
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
PrintPathInfo();
PrintProcessInfo();
PrintBookmarkInfo();
PrintIntrospectionInfo();
PrintPolygonInfo();
[pool release];
return 0;
}
//The result was "EXC_BAD_ACCESS", but I couldn't figure out how to resolve this problem.