我在实现文件中有以下代码:
#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;
}
答案 0 :(得分:0)
对于描述方法,