gpt4 book ai didi

cocoa - 如何将整数传递给模型类

转载 作者:行者123 更新时间:2023-12-03 17:14:44 24 4
gpt4 key购买 nike

我在这方面遇到了一些麻烦!我确实查看了很多帖子,但无法使其发挥作用。以前在我的应用程序中,我通过将数据分配给属性来设法在类之间传递数据。我不确定这里哪里出了问题,因此我们将不胜感激。我知道下面的代码不太好,我正在查找字典来提供帮助,但现在我想让它正常工作。

- (IBAction)checkAnswers:(id)sender
{
// The following if statements check user input and checks to see if it is the right answer.
// The .text parts are uitextfield properties and then shows an image of a tick if correct.
// It then assigns one to a variable and sums them up at the end (blag)
if ([eyepiece.text isEqualToString:@"Eyepiece"]) {
UIImage *image = [UIImage imageNamed:@"Tick.png"];
[eyepieceTick setImage:image];
a = 1;
}

if ([focussingKnobs.text isEqualToString:@"Focussing knobs"]) {
UIImage *image = [UIImage imageNamed:@"Tick.png"];
[focussingTick setImage:image];

b = 1;
}

if ([objectiveLens.text isEqualToString:@"Objective lenses"]) {
UIImage *image = [UIImage imageNamed:@"Tick.png"];
[objectiveTick setImage:image];
c = 1;
}

if ([stage.text isEqualToString:@"Stage"]) {
UIImage *image = [UIImage imageNamed:@"Tick.png"];
[stageTick setImage:image];
d = 1;
}
if ([mirror.text isEqualToString:@"Mirror"]) {
UIImage *image = [UIImage imageNamed:@"Tick.png"];
[mirrorTick setImage:image];
e = 1;
}

blag = a + b + c + d + e;

// Here I update a label with the score

finalScore = [[NSString alloc] initWithFormat:@"%D", blag];
[score setText:finalScore];

// This is probably where I'm going wrong. I'm allocating a model class called
// Level_3_Brain and trying to assign a new property in that class (cellsLevelThree)
// with the score.

// Level_3_Brain *level = [[Level_3_Brain alloc] init];
// level.cellsLevelThree = blag;

// Updated to
[Level_3_Brain sharedInstanceOfLevel3].cellsLevelThree = blag;


// I then set them all back to zero so that the score doesn't go above 5
a = 0, b = 0, c = 0, d = 0, e = 0;
blag = 0;

}

我的模型类 .h 现在有:

@interface Level_3_Brain : NSObject 

+ (id)sharedInstanceOfLevel3;

@property (nonatomic) int cellsLevelThree;

@end

我的 .m 现在有来自 Singleton 文章的代码:

@implementation Level_3_Brain
@synthesize cellsLevelThree;

static Level_3_Brain *sharedInstanceOfLevel3 = nil;

// Get the shared instance and create it if necessary.
+ (Level_3_Brain *)sharedInstanceOfLevel3 {
if (sharedInstanceOfLevel3 == nil)
{
sharedInstanceOfLevel3 = [[super allocWithZone:NULL] init];
}



return sharedInstanceOfLevel3;


}

// We can still have a regular init method, that will get called the first time the Singleton is used.

- (id)init
{
self = [super init];

if (self) {
// Work your initialising magic here as you normally would
}

NSLog(@"%@", cellsLevelThree);

return self;
}

// Your dealloc method will never be called, as the singleton survives for the duration of your app.
// However, I like to include it so I know what memory I'm using (and incase, one day, I convert away from Singleton).
-(void)dealloc
{
// I'm never called!
// [super dealloc];
}

/ We don't want to allocate a new instance, so return the current one.
+ (id)allocWithZone:(NSZone*)zone {
return [self sharedInstanceOfLevel3];
}

// Equally, we don't want to generate multiple copies of the singleton.
- (id)copyWithZone:(NSZone *)zone {
return self;
}


@end

不幸的是,我现在收到错误“在 id 类型的对象上找不到属性“cellsLevelThree”。请帮忙!!

最佳答案

您在评论中正确地指出了您“出错”的地方:问题不在于您错误地设置了值,而在于您将其设置在本地的全新实例上方法,并在退出该方法时立即丢弃。

一般来说,您的模型类应该在应用程序启动时创建,并在应用程序的整个生命周期内保持可用。这通常可以使用singletons来完成。 .

关于cocoa - 如何将整数传递给模型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10121798/

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