我有一个有多种方法的类。我可以从另一个对象实例化这个类并调用这些方法。当我添加一个新方法时,我不能从类中的另一个方法或另一个类中调用它。如果我删除参数,那么我可以看到该方法,但是当我将其添加回来时,我看不到该方法。我在这里和互联网上进行了搜索,并确保我正在导入该类并且该方法是在 .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 *”类型的对象上。当我尝试从另一个对象调用它时,我得到了这个。
谢谢你的帮助。