//
// Possession.m
// RandomPossessions
//
// Created by Paxton Harman on 1/14/11.
// Copyright 2011 Santa Barbara City College. All rights reserved.
//
#import "Possession.h"
@implementation Possession
@synthesize possessionName, serialNumber, valueInDollars, dateCreated;
@end
- (id)initWithPossessionName:(NSString *)name;
valueindollars:(int)value
serialNumber:(NSString *)sNumber
{
// Call the superclass's designated initializer
self = [super init];
// Did the superclass's designated initializer fail?
if (!self)
return nil;
// Give the instance variables initial values
[self setPossessionsName:name];
[self setSerialNumber:sNumber];
[self setValueInDollars:value];
dateCreated = [[NSDate alloc] init];
// Return the address of the newly initialized object
return self;
}
- (id)initWithPossessionName:(NSString *)name
{
return [self initWithossessionName:name
valueInDollars:0
serialnNumber:@""];
}
@end
- (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)randomPossesion {
// 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
// signify that there are no more objects - otherwise you will crash.
// The nil value is not added to the array, but it used by the method
// to determine the end of the list.
NSArray *randomAdjectiveList = [NSArray array WithObjects:@"Fluffy",
@"Rusty",
@"Shiny", nil];
NSArray *randomNounList = [NSArray arrayWithObjects:@"Bear",
@"Spork",
@"Mac", nil];
// Get the index of 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];
int nounIndex = random() % [randomNounList count];
NSString *randomName = [NSString stringWithFormat:@"%@ %@",
[randomAdjectiveList objectAtIndex:adjectiveIndex],
[randomNounList objectAtIndex:nounIndex]];
int randomValue = random() % 100;
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 *newPossesion =
[[self alloc] initWithPossessionName:randomName
valueInDollars:randomValue
serialNumber:randomSerialNumber];
return newPossession;
}
在第14行和第19行,它给了我“不完全实施占有”和“以美元计价”之前的'{'。任何帮助?新的可可
答案 0 :(得分:2)
您有两个@end
指令。两者都不在正确的地方。它们应该是 - 令人惊讶的 - 实施块的结束。
在此方法签名中,删除分号(最好是空行):
- (id)initWithPossessionName:(NSString *)name;
valueindollars:(int)value
serialNumber:(NSString *)sNumber
这个错误令Jacob Relkin和Adam Eberbach感到困惑,导致他们试图解决实际上并不存在的问题。
在便捷初始值设定项- (id)initWithPossessionName:(NSString *)name
和-init
中,您正在调用一个不存在的方法 - 您在一个调用的选择器中存在多个拼写错误,以及一些大写问题如果不是额外的分号,你实际上要定义的那个。
答案 1 :(得分:0)
雅各布是对的。你也应该这样做
@interface Possession (private)
- (id)initWithPossessionName:(NSString *)name;
@end
在执行块之前。方法声明不能出现在实现中;他们必须进入界面。您的函数原型正在向您的类广告,因此它们在这个意义上是私有的。但私人在这里不是一个特殊的名字,你可以说
@interface Possession (frobozz)
如果你想要的话。只是不
@interface Possession
因为这会复制可能位于.h文件中的接口定义。 所以整个事情必须看起来像
@import "Possession.h"
// public prototypes, properties, ivars etc. in the .h file
@interface Possession (private)
// prototypes here
@end
@implementation Possession
// all your code here
@end
// end is the last thing in the file