我有一个有多种方法的课程。我可以从另一个对象实例化这个类并调用这些方法。当我添加一个新方法时,我不能从类中的另一个方法或其他类中调用它。如果我删除参数然后我可以看到该方法,但当我添加它时,我看不到该方法。我在这里和互联网上搜索过,我已经确定我要导入该类,并且该方法是在.h文件中定义的。我对可能出现的问题感到茫然。
界面:
@interface npcgGetCharacterData : NSObject
@property (strong, nonatomic) NSFetchedResultsController *fetchedResults;
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property BOOL debug;
- (NSMutableString *)getRandomRace;
- (NSMutableString *)getRandomAlignment;
- (int)getRandomLevel;
- (NSMutableString *)getRandomNPCType;
- (NSMutableString *)getRandomNPCClass:(NSString *)entityClassType;
@end
实施:
#import "npcgGetCharacterData.h"
#import "RaceEntity+Properties.h"
#import "AlignmentEntity+Properties.h"
#import "NPCTypeEntity+Properties.h"
@implementation npcgGetCharacterData
@synthesize fetchedResults, managedObjectContext, debug;
- (NSMutableString *)getRandomRace {
NSString *randomRace = [NSString alloc];
NSArray *resultsArray;
NSInteger intCount;
RaceEntity *raceEntity;
[self setupFetchedResultsController:@"RaceEntity" entityName:@"raceName" entityWhere:@"raceName != 'Any Race'"];
debug = NO;
[self performFetch];
resultsArray = [fetchedResults fetchedObjects];
intCount = [resultsArray count];
raceEntity = [resultsArray objectAtIndex:arc4random() % intCount];
randomRace = raceEntity.raceName;
self.fetchedResults = nil;
return randomRace.mutableCopy;
}
- (NSMutableString *)getRandomAlignment {
NSString *randomAlignment = [NSString alloc];
NSArray *resultsArray;
NSInteger intCount;
AlignmentEntity *alignmentEntity;
[self setupFetchedResultsController:@"AlignmentEntity" entityName:@"alignmentName" entityWhere:@"alignmentName != 'Any Alignment'"];
debug = NO;
[self performFetch];
resultsArray = [fetchedResults fetchedObjects];
intCount = [resultsArray count];
alignmentEntity = [resultsArray objectAtIndex:arc4random() % intCount];
randomAlignment = alignmentEntity.alignmentName;
self.fetchedResults = nil;
return randomAlignment.mutableCopy;
}
- (NSMutableString *)getRandomNPCType {
NSString *randomNPCType = [NSString alloc];
NSArray *resultsArray;
NSInteger intCount;
NPCTypeEntity *npcTypeEntity;
[self setupFetchedResultsController:@"NPCTypeEntity" entityName:@"npcTypeName" entityWhere:@"npcTypeName != 'Any Type'"];
debug = NO;
[self performFetch];
resultsArray = [fetchedResults fetchedObjects];
intCount = [resultsArray count];
npcTypeEntity = [resultsArray objectAtIndex:arc4random() % intCount];
randomNPCType = npcTypeEntity.npcTypeName;
self.fetchedResults = nil;
return randomNPCType.mutableCopy;
}
- (NSMutableString *)getRandomNPCClass:(NSString *)entityClassType {
NSString *randomNPCClass = [NSString alloc];
return randomNPCClass.mutableCopy;
}
- (int)getRandomLevel {
int intRandomLevel = 0;
intRandomLevel = (NSInteger)arc4random_uniform(20) + 1;
return intRandomLevel;
}
- (void)setupFetchedResultsController:(NSString *)entityType entityName:(NSString *)entityName entityWhere:(NSString *)entityWhere{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityType];
if (entityWhere != nil) {
[request setPredicate:[NSPredicate predicateWithFormat:entityWhere]];
}
NSSortDescriptor *entityGroup = [[NSSortDescriptor alloc] initWithKey:entityName ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
NSSortDescriptor *entityNameSort = [[NSSortDescriptor alloc] initWithKey:entityName ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
request.sortDescriptors = [NSArray arrayWithObjects:entityGroup, entityNameSort, nil];
self.fetchedResults = [[NSFetchedResultsController alloc]
initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
}
- (void)performFetch {
if (self.fetchedResults) {
if (self.fetchedResults.fetchRequest.predicate) {
if (self.debug) NSLog(@"[%@ %@] fetching %@ with predicate: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), self.fetchedResults.fetchRequest.entityName, self.fetchedResults.fetchRequest.predicate);
} else {
if (self.debug) NSLog(@"[%@ %@] fetching all %@ (i.e., no predicate)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), self.fetchedResults.fetchRequest.entityName);
}
NSError *error;
[self.fetchedResults performFetch:&error];
if (error) NSLog(@"[%@ %@] %@ (%@)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), [error localizedDescription], [error localizedFailureReason]);
if (self.debug) {
NSLog(@"Number of Objects = %i", [[self.fetchedResults fetchedObjects] count]);
}
} else {
if (self.debug) NSLog(@"[%@ %@] no NSFetchedResultsController (yet?)", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
}
}
@end
调用方法。这是来自不同的对象:
- (IBAction)GenerateNPCClick:(id)sender {
npcgGenerateAttribute *nga = [npcgGenerateAttribute alloc];
npcgGetCharacterData *ngc = [npcgGetCharacterData alloc];
ngc.managedObjectContext = self.managedObjectContext;
int intValue = 0;
NSMutableString *textViewHolder = [NSMutableString alloc];
for (NSInteger i = 0; i < 6; i++) {
intValue = nga.generate4D6;
if (i == 0) {
textViewHolder = [NSMutableString stringWithFormat:@"%d", intValue];
} else {
[textViewHolder appendString:@"\n"];
[textViewHolder appendString:[NSMutableString stringWithFormat:@"%d", intValue]];
}
}
NSMutableString *strRace = ngc.getRandomRace;
[textViewHolder appendString:@"\n"];
[textViewHolder appendString:@"Race: "];
[textViewHolder appendString:strRace];
NSMutableString *strAlignment = ngc.getRandomAlignment;
[textViewHolder appendString:@"\n"];
[textViewHolder appendString:@"Alignment: "];
[textViewHolder appendString:strAlignment];
int intLevel = ngc.getRandomLevel;
[textViewHolder appendString:@"\n"];
[textViewHolder appendString:@"Level: "];
[textViewHolder appendString:[NSMutableString stringWithFormat:@"%d", intLevel]];
NSMutableString *strNPCType = ngc.getRandomNPCType;
[textViewHolder appendString:@"\n"];
[textViewHolder appendString:@"NPC Type: "];
[textViewHolder appendString:strNPCType];
[ngc getRandomNPCClass:@"TestString"];
self.textViewMessageSpace.text = textViewHolder;
}
给我带来麻烦的方法是getRandomNPCClass方法。所有其他getRandom方法都在工作。
忘记包含错误消息:属性'getRandomNPCClass'不在'npcgGetCharacterData *'类型的对象上。当我尝试从另一个对象调用它时,我得到了这个。
感谢您的帮助。
答案 0 :(得分:1)
问题是如何创建对象:
npcgGetCharacterData *ngc = [npcgGetCharacterData alloc];
您正在混淆初始化消息,因此这就是您在尝试将其用作正确初始化对象时遇到麻烦的原因。由于您尚未定义init方法,因此将使用NSObject的init方法初始化对象:
npcgGetCharacterData *ngc = [[npcgGetCharacterData alloc]init];
此外,我希望记住关于访问者的Objective-C约定与Java和C ++不同。你不要调用getter getRandomNPCClass,而是调用randomNPCClass。