Forgive me if this is not the best place to post this question. I am working my way through the Big Nerd Ranch iPhone Programming Guide, and following the code along as I go. I am stuck on Chapter 2 (yes already)
I am getting 4 warnings on my possession.m file and it won't let me compile and run it. I am attaching the .m file where the warnings are occurring. I have commented the warnings I am receiving in all CAPS.
If I need to attach the other 2 files (main.m and possession.h) let me know, although it doesn't seem like these warnings are related to the other files. Any help is appreciated....
Code:
#import "Possession.h"
@implementation Possession //***INCOMPLETE IMPLEMENTATION***
@synthesize possessionName, serialNumber, valueInDollars, dateCreated;
- (id)initWithPossessionName:(NSString *)name
valueInDollars:(int)value
serialNumber:(NSString *)sNumber
{
// Call the superclass's designated initializer
self =[super init];
// Did the supeclass's designated initializer fail?
if (!self)
return nil;
// Give the instance variable initial values
[self setPossessionName:name];
[self setSerialNumber:sNumber];
[self setValueInDollars:value];
dateCreated = [[NSDate alloc] init];
//Return the address of the newly initialized object
return self;
}
- (NSString *) description
{
NSString * descriptionString =
[[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d, Recorded on %@",
possessionName,
serialNumber,
valueInDollars,
dateCreated];
return descriptionString;
}
- (id)init
{
return [self initWithPossessionName:@"Possession"
valueInDollars:0
serialNumber:@""];
}
+ (id) randomPossession
{
// Create two arrays with a list of possible adjectives and nouns
// Note: When using NSArray's arrayWithObjects:, you can pass as many
// objects as you like. At the end of that list, you put nil to
// signifify that there are no more objects - otherwise you will crash.
// The nil value is not added to the array, but is used by the method
// to determine the end of the list.
NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@"Fluffy",
@"Rusty",
@"Shiny", nil];
NSArray *randomNounList = [NSArray arrayWithObjects:@"Bear",
@"Spork",
@"Mac", nil];
// Get the index of a random adjective/noun from the lists
// Note: The % operator, called the modulo operator, gives
// you the remainder. So adjectiveIndex is a random number
// from 0 to 2 inclusive, in this case.
int adjectiveIndex =random() % [randomAdjectiveList count]; //***IMPLICIT CONVERSION LOSES INTEGER PRECISION: 'UNSIGNED LONG' TO 'INT'***
int nounIndex = random() % [randomNounList count]; //***IMPLICIT CONVERSION LOSES INTEGER PRECISION: 'UNSIGNED LONG' TO 'INT'***
NSString *randomName = [NSString stringWithFormat:@"%@ %@",
[randomAdjectiveList objectAtIndex:adjectiveIndex],
[randomNounList objectAtIndex:nounIndex]];
int randomValue = random() % 100; //***IMPLICIT CONVERSION LOSES INTEGER PRECISION: 'UNSIGNED LONG' TO 'INT'***
NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
'0' + random() % 10,
'A' + random() % 26,
'0' + random() % 10,
'A' + random() % 26,
'0' + random() % 10];
//Once again, ignore the memory problems with this method
// We use "self" instead of the name of the class in class methods...
// Keep reading to find out why
Possession *newPossession =
[[self alloc] initWithPossessionName:randomName
valueInDollars:randomValue
serialNumber:randomSerialNumber];
return newPossession;
}
@end
You could find a lot by Googling those warnings. The first one says that you have methods declared in the header, but you never wrote the implementations for them. The other three are saying that the function is returning a long integer and you are storing it in an int, which has much smaller range and you can possibly overflow it, lose precision.
Also, keep in mind that random() function has to be seeded to work, otherwise you'll always get the same sequence of numbers. Try using arc4random() instead which is self-seeding and gives a much better random numbers.
You could find a lot by Googling those warnings. The first one says that you have methods declared in the header, but you never wrote the implementations for them. The other three are saying that the function is returning a long integer and you are storing it in an int, which has much smaller range and you can possibly overflow it, lose precision.
Also, keep in mind that random() function has to be seeded to work, otherwise you'll always get the same sequence of numbers. Try using arc4random() instead which is self-seeding and gives a much better random numbers.
Thanks for the reply Baja....
I am still confused as to what methods I declared in the header file but never wrote the implementations for them. Here is the header file, I just double checked my work against the book...
As far as the other 3 warnings goes, I am not sure why the book would say to store the result as an int, I see your point. Wouldn't the % operator bypass that and just give you the remainder though? Or is that not how it works...
You have that method declaration in the header and it's not implemented.
Thanks again Baja. I had that in there originally but I thought the next section of the book said to overwrite that line with -(id)init, which is what I did. I must have read that wrong, I will go back and check