Hi,
I have been attempting to program the iPhone and am relatively new to it. I was trying to complete the assignment 2A on Stanfords CS193P website. It seems to run fine until I tried creating the PolygonShape class and using the class to run its methods. The error I keep getting is segmentation fault and I have no idea how to solve it. I tried to copy and paste code that works into that particular method (PrintPolygonInfo) and it still says the same. I'm appending the polygon class files as well as the header and implementation files for the project. Could someone please tell me what I'm doing wrong. Thanks a lot,
Adheer
I have been attempting to program the iPhone and am relatively new to it. I was trying to complete the assignment 2A on Stanfords CS193P website. It seems to run fine until I tried creating the PolygonShape class and using the class to run its methods. The error I keep getting is segmentation fault and I have no idea how to solve it. I tried to copy and paste code that works into that particular method (PrintPolygonInfo) and it still says the same. I'm appending the polygon class files as well as the header and implementation files for the project. Could someone please tell me what I'm doing wrong. Thanks a lot,
Adheer
Code:
PolygonShape.h:
//
// PolygonShape.h
// Assignment2A Stanford
//
// Created by Adheer Chauhan on 10/21/09.
// Copyright 2009 Drexel University. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface PolygonShape : NSObject //These are for all the instance variables of the class
{
int numberOfSides;
int minimumNumberOfSides;
int maximumNumberOfSides;
}
@property (readwrite) int numberOfSides;//if we didn't specify anything, it would still be readwrite as that is the default
@property (readwrite) int minimumNumberOfSides;
@property (readwrite) int maximumNumberOfSides;
@property (readonly) float angleInDegrees;
@property (readonly) float angleInRadians;
@property (readonly) NSString *name;
@property (readonly) NSString *description;
-(id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max;
@end
PolygonShape.m
//
// PolygonShape.m
// Assignment2A Stanford
//
// Created by Adheer Chauhan on 10/21/09.
// Copyright 2009 Drexel University. All rights reserved.
//
#import "PolygonShape.h"
@implementation PolygonShape
#define MINSIDES 3
#define MAXSIDES 12
#define DEFAULTSIDES 5
@synthesize numberOfSides;
@synthesize minimumNumberOfSides;
@synthesize maximumNumberOfSides;
-(id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max
{
if (self = [super init]) {//This initializes the instance variables to their default values
minimumNumberOfSides = MINSIDES;
maximumNumberOfSides = MAXSIDES;
numberOfSides = DEFAULTSIDES;
[self setNumberOfSides:sides];//This initializes the instance variables using the setter methods
[self setMinimumNumberOfSides:min];
[self setMaximumNumberOfSides:max];
}
return self;
}
//Custom setter method for maximumNumberOfSides
-(void)setMaximumNumberOfSides:(int)max
{
if((max <= 12) && (max >= minimumNumberOfSides))
maximumNumberOfSides = max;
else
NSLog(@"Invalid Maximum Number of Sides %i, must be Greater than or equal to %i and less than or equal to 12", max, minimumNumberOfSides);
}
//Custom setter method for minimumNumberOfSides
-(void)setMinimumNumberOfSides:(int)min
{
if((min > 2) && (min <= maximumNumberOfSides))
minimumNumberOfSides = min;
else
NSLog(@"Invalid Minimum Number of Sides %i, must be Greater than 2 and less than or equal to %i", min, maximumNumberOfSides);
}
//Custom setter method for numberOfSides
-(void)setNumberOfSides:(int)num
{
if((num <= maximumNumberOfSides) && (num >= minimumNumberOfSides))
numberOfSides = num;
else
NSLog(@"Invalid number of sides %i, must be between %i and %i.", num, minimumNumberOfSides, maximumNumberOfSides);
}
-(id)init
{
return [self initWithNumberOfSides:DEFAULTSIDES minimumNumberOfSides:MINSIDES maximumNumberOfSides:MAXSIDES];
}
-(float)angleInDegrees
{
return 180 * (numberOfSides - 2)/numberOfSides;
}
-(float)angleInRadians
{
return (2 * M_PI) * (numberOfSides - 2)/numberOfSides;
}
-(NSString *)name
{
NSString *nameOfPolygon;
switch (numberOfSides)
{
case 3:
nameOfPolygon = @"Triangle";
break;
case 4:
nameOfPolygon = @"Square";
break;
case 5:
nameOfPolygon = @"Pentagon";
break;
case 6:
nameOfPolygon = @"Hexagon";
break;
case 7:
nameOfPolygon = @"Heptagon";
break;
case 8:
nameOfPolygon = @"Octagon";
break;
case 9:
nameOfPolygon = @"Enneagon";
break;
case 10:
nameOfPolygon = @"Decagon";
break;
case 11:
nameOfPolygon = @"Hendecagon";
break;
case 12:
nameOfPolygon = @"Dodecagon";
break;
default:
nameOfPolygon = @"Undefined";
break;
}
return nameOfPolygon;
}
-(NSString *)description
{
return [NSString stringWithFormat:@"Hello, I am a %d-sided polygon (aka a %@) with angles of %@ degrees (%@ radians)", numberOfSides, [self name], [self angleInDegrees], [self angleInRadians]];
}
-(void)dealloc
{
NSLog(@"dealloc is being called");
[super dealloc];
}
@end
Assignment2A Stanford.m:
#import <Foundation/Foundation.h>
#import "PolygonShape.h"
void PrintPathInfo()
{
NSString *path = @"~";
NSString *stringtext = [path stringByExpandingTildeInPath];//path is an instance of class NSString and it is calling method stringByExpandingTildeInPath
//NSLog(@"My home folder is at '%@'", stringtext);
NSArray *pathcomponents = [stringtext pathComponents];//we use the instance stringtext of type NSString containing the pathComponents method
for (NSString *item in pathcomponents)//we go through each element of pathcomponents array and print it using NSLog
{
NSLog([item description]);
}
}
void PrintProcessInfo()
{
NSString *procName = [[NSProcessInfo processInfo] processName];
int procid = [[NSProcessInfo processInfo] processIdentifier];
NSLog(@"Process name: '%@' Process ID: '%d'", procName, procid);
//NSLog(text);
}
void PrintBookmarkInfo()
{
//NSArray *keys = [NSArray arrayWithObjects:@"Stanford University", @"Apple", @"CS193P", @"Stanford on iTunesU", "Stanford Mall", nil];
NSURL *url1 = [NSURL URLWithString:@"http://www.stanford.edu"];
NSURL *url2 = [NSURL URLWithString:@"http://www.apple.edu"];
NSURL *url3 = [NSURL URLWithString:@"http://cs193p.stanford.edu"];
NSURL *url4 = [NSURL URLWithString:@"http://itunes.stanford.edu"];
NSURL *url5 = [NSURL URLWithString:@"http://www.stanfordshop.edu"];
//NSArray *values [NSArray arrayWithObjects:url1, url2, url3, url4, url5, nil];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];//The dictionary at the RHS of the '=' sign is a method that initializes a dictionary
[dictionary setObject:url1 forKey:@"Stanford University"];
[dictionary setObject:url2 forKey:@"Apple"];
[dictionary setObject:url3 forKey:@"CS193P"];
[dictionary setObject:url4 forKey:@"Stanford on iTunes U"];
[dictionary setObject:url5 forKey:@"Stanford Mall"];
//NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"http://www.stanford.edu", @"Stanford University", @"http://www.apple.com", @"Apple", @"http://cs193p.stanford.edu", @"CS193P", @"http://itunes.stanford.edu", @"Stanford on iTunes U", @"http://stanfordshop.com", @"Stanford Mall", nil];
for (id key in dictionary)
{
if([key hasPrefix:@"Stanford"])
{
NSLog(@"Key: %@, Value: %@", key, [dictionary objectForKey:key]);
}
}
}
void PrintIntrospectionInfo()
{
NSMutableArray *array = [NSMutableArray array];
NSString *string = @"This is for Stanford University's iPhone Application Development course";
NSURL *url = [NSURL URLWithString:@"http://www.qualcomm.com"];
NSProcessInfo *procinfo = [NSProcessInfo processInfo];
NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", @"key3", @"key4",nil];
NSArray *values = [NSArray arrayWithObjects:@"value 1", @"value 2", @"value 3", @"value 4",nil];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];
NSMutableString *mutablestring = [NSMutableString string];
mutablestring = @"Programming the iPhone Rocks!!!";
[array addObject:string];
[array addObject:url];
[array addObject:procinfo];
[array addObject:dictionary];
[array addObject:mutablestring];
for (NSString *element in array)
{
NSLog(@"The name of this class is %@", [element className]);
if([element isMemberOfClass:[NSString class]])
NSLog(@"Is Member of NSString: YES");
else
NSLog(@"Is Member of NSString: NO");
if([element isKindOfClass:[NSString class]])
NSLog(@"Is Kind of NSString: YES");
else
NSLog(@"Is Kind of NSString: NO");
SEL sel = @selector(lowercaseString);
if([element respondsToSelector:sel])
{
NSLog(@"Responds to lowercaseSring: YES");
NSLog(@"Lower case string of this element is: %@",[element lowercaseString]);
}
else
NSLog(@"Responds to lowercaseString: NO");
if([[element className] hasSuffix:@"Dictionary"])
{
for(id key in dictionary)
{
NSLog(@"Key: %@, Value: %@",key, [dictionary objectForKey:key]);
}
}
NSLog(@"=========================================");
}
}
void PrintPolygonInfo()
{
NSMutableArray *pArray = [NSMutableArray array];
PolygonShape *pgon = [[PolygonShape alloc] initWithNumberOfSides:4 minimumNumberOfSides:3 maximumNumberOfSides:7];
NSLog(@"Adding this polygon to the array: %@", pgon);
[pArray addObject:pgon];
[pgon release];
pgon = [[PolygonShape alloc] initWithNumberOfSides:6 minimumNumberOfSides:5 maximumNumberOfSides:9];
NSLog(@"Adding this polygon to the array: %@", pgon);
[pArray addObject:pgon];
[pgon release];
pgon = [[PolygonShape alloc] initWithNumberOfSides:12 minimumNumberOfSides:9 maximumNumberOfSides:12];
NSLog(@"Adding this polygon to the array: %@", pgon);
[pArray addObject:pgon];
[pgon release];
for(PolygonShape *p in pArray)
{
p.numberOfSides = 10;
}
[pArray release];
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSLog(@"Result for Section 1\n");
PrintPathInfo(); //Section 1
NSLog(@"Result for Section 2\n");
PrintProcessInfo();//Section 2
NSLog(@"Result for Section 3\n");
PrintBookmarkInfo();//Section 3
NSLog(@"Result for Section 4\n");
PrintIntrospectionInfo();//Section 4
NSLog(@"Result for Assignment 2A");
PrintPolygonInfo();
// NSLog(@"Hello, World!");
[pool drain];
return 0;
}
Assignment2A Stanford_Prefix.pch
//
// Prefix header for all source files of the 'Assignment2A Stanford' target in the 'Assignment2A Stanford' project.
//
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#endif