Неиспользованный переменный вопрос - PullRequest
0 голосов
/ 08 сентября 2011

У меня есть следующий код в файле реализации:

#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]];

     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

Для descriptionString я получаю ошибку неиспользованной переменной, а для строки, которая читает «dateCreated]», я получаю поток 1: Программа получила сигнал: ошибка «SIGABRT», которая открывает отладчик. Для строки, следующей сразу за мной, я получаю, что Control достигает конца ошибки недействительной функции.

Вот заголовочный файл:

#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

Вот основной файл:

#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;
    }

1 Ответ

0 голосов
/ 09 сентября 2011

Для метода описания,

  1. локальная переменная descriptionString никогда не используется иначе, чем создается.
  2. это не пустой метод, и вы ничего не возвращаете
  3. Вероятно, SIGABRT вызван одним из следующих значений :ionionName, serialNumber, valueInDollars, dateCreated равен nil.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...