gpt4 book ai didi

ios - Objective-C 为什么我的类方法对该类中的其他方法或其他对象不可见?

转载 作者:行者123 更新时间:2023-11-29 04:08:39 26 4
gpt4 key购买 nike

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

感谢您的帮助。

最佳答案

问题在于如何创建对象:

npcgGetCharacterData *ngc = [npcgGetCharacterData alloc];

您混淆了初始化消息,因此这就是您在尝试将其用作正确初始化的对象时遇到麻烦的原因。由于您还没有定义 init 方法,因此将使用 NSObject 的 init 方法来初始化对象:

npcgGetCharacterData *ngc = [[npcgGetCharacterData alloc]init];

此外,我想记住 Objective-C 关于访问器的约定与 Java 和 C++ 不同。您不会调用 getter getRandomNPCClass,而是调用 randomNPCClass。

关于ios - Objective-C 为什么我的类方法对该类中的其他方法或其他对象不可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14783425/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com