开发者

Unused variable question

I have the following code in the implementation file:

#import "Possession.h"


@implementation Possession
@synthesize possessionName,serialNumber, valueInDollars, dateCreated; 

+ (id)randomPossession
{
  // Create an array of three adjectives
    NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@"Fluffy",
                                @"Rusty",
                                @"Shiny", nil];

    // Create an array of three nouns
    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.
    unsigned long adjectiveIndex = rand() % [randomAdjectiveList count];
    unsigned long nounIndex = rand() % [randomNounList count];

     NSString *randomName = [NSString stringWithFormat:@"%@ %@",
                        [randomAdjectiveList objectAtIndex:adjectiveIndex],
                        [randomNounList objectAtIndex:nounIndex]];
开发者_StackOverflow社区
     int randomValue = rand() % 100;

     NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c",
                                'O' + rand() % 10,
                                'A' + rand() % 26,
                                'O' + rand() % 10,
                                'A' + rand() % 26,
                                'O' + rand() % 10];

     // Once again, ignore the memory problems with this method
     Possession *newPossession =
     [[self alloc] initWithPossessionName:randomName 
                      valueInDollars:randomValue 
                        serialNumber:randomSerialNumber];

     return newPossession;

     return [newPossession autorelease];


}

 - (id) initWithPossessionName:(NSString *)name
           valueInDollars:(int)value
             serialNumber:(NSString *)sNumber
{
    // Call the superclass's designated initializer
    [super init];

    // Give the instance variables 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;


 }

- (id) init
{
    return [self initWithPossessionName:@"Possession"
                     valueInDollars:0
                       serialNumber:@""];
}

- (NSString *)description;
{
     NSString *descriptionString = 
     [[NSString alloc] stringWithFormat:@"%@ (%@): Worth $%d, recorded on %@",
     possessionName,
     serialNumber,
     valueInDollars,
     dateCreated];
}


- (void) dealloc
{
    [possessionName release];
    [serialNumber release];
    [dateCreated release];
    [super dealloc];
}




@end

For the descriptionString, I am getting an unused variable error, and for the line which reads "dateCreated],", I am getting a Thread 1: Program received signal: "SIGABRT" error which opens up the debugger. For the line immediately following, I am receiving a Control reaches end of non-void function error.

Here is the header file:

#import <Foundation/Foundation.h>


@interface Possession : NSObject {
    NSString *possessionName;
    NSString *serialNumber;
    int valueInDollars;
    NSDate *dateCreated;
}

 + (id)randomPossession;

 - (id)initWithPossessionName:(NSString *)name
          valueInDollars:(int)value
            serialNumber:(NSString *)number;

 - (id) init;

 @property (nonatomic, copy) NSString *possessionName;
 @property (nonatomic, copy) NSString *serialNumber;
 @property (nonatomic) int valueInDollars;
 @property (nonatomic, readonly) NSDate *dateCreated;

 @end

Here is the main file:

#import <Foundation/Foundation.h>
#import "Possession.h"

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // Create a mutable array, store its address in items variable
    NSMutableArray *items = [[NSMutableArray alloc] init];

    for(int i = 0; i < 10; i++) {
        Possession *p = [Possession randomPossession];
        [items addObject:p];
    }

   for (int i = 0; i < [items count]; i++)
        NSLog(@"%@", [items objectAtIndex:i]);

   [items release];

    // Don't leave items pointed at freed memory!
    items = nil;

    [pool drain];
    return 0;
    }


For the description method,

  1. the local variable descriptionString is never used other then being created.
  2. it's a non-void method and you never return anything
  3. The SIGABRT is likely to be caused by one of the possessionName, serialNumber, valueInDollars, dateCreated being nil.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜