gpt4 book ai didi

objective-c - No Visible @interface for 'GameViewController' 声明选择器 'createCardTypeDeck'

转载 作者:行者123 更新时间:2023-12-03 17:51:29 25 4
gpt4 key购买 nike

我在这些代码行上遇到错误。谢谢

This is the first line of code
[ar exchangeObjectAtIndex:i withObjectAtIndex:n];

这是我得到的错误

use of undeclared identifier 'ar'

下一段代码

-(NSMutableArray*)createCardTypeDeck: {

这是我得到的错误

expected identifier  

这是下一个

-(void)updateCard: (UIButton*) card, cardType: (int) cardType (

这是错误

Expected method body 

这是最后一个

@end

这是一个奇怪的错误

Missing '@end'

感谢大家的帮助如果您还没有 guest ,我是编码新手。

#import "GameViewController.h"

@interface GameViewController ()

@end

@interface NSArray(Shuffle)
-(NSArray *)shuffledArray;
@end

@implementation GameViewController
-(void)shuffleArray: (NSMutableArray*)arr {
NSUInteger count = [arr count];
for (NSUInteger i = 0; i < count; ++i) {
// Select a random element between i and end of array to swap with.
int nElements = count - i;
int n = (arc4random() % nElements) + i;
[ar exchangeObjectAtIndex:i withObjectAtIndex:n]; //this is where I get the first error.)
}
}

-(NSMutableArray*)createCardTypeDeck: { //This is the second one.
NSMutableArray *cardTypes = [NSMutableArray arrayWithCapacity:8];
for (int i = 0; i < 4; i++) {
// Add the same number twice, for a total 8 objects added
[cardTypes addObject:[NSNumber numberWithInteger:i]];
[cardTypes addObject:[NSNumber numberWithInteger:i]];
}
return cardTypes;
}

// Take in /a/ card and the type, so it will work for all cards;
// don't add the card to the view here. Note there is no hard-coding of
// card1..card8 and thus there is NO NEED to duplicate this method 8 times!
-(void)updateCard: (UIButton*) card, cardType: (int) cardType ( (The third error.)
{
// The imageName and UIImage creation could be further extracted but
// this should be sufficient to show how much common code (and copy'n'paste)
// can be eliminated - resulting in shorter and more readable code.
NSString *imageName;
switch (cardType) {
case 0:
imageName = @"cell phone.jpeg";
break;
case 1:
imageName = @"Dinasore.jpeg";
break;
case 2:
imageName = @"jump Rope.jpeg";
break;
case 3:
imageName = @"monkey.jpeg";
break;

default:
image = nil; // but really an error of some sort
break;
}
UIImage *image = [UIImage imageNamed:imageName];
[card setImage:image forState:UIControlStateNormal];
}
- (void)viewDidLoad
{
{
// Create card/cardType deck, values -> [0, 0, 1, 1, 2, 2, 3, 3]
NSMutableArray *cardTypes = [self createCardTypeDeck];
// Shuffle the card types, result e.g. -> [2, 1, 0, 3, 2, 3, 0, 1]
[self shuffleArray: cardTypes];

// At least we only use the names once now
NSArray *cards = [NSArray arrayWithObjects:
card1, card2, card3, card4,
card5, card6, card7, card8, nil];

// For each card, assign it an image and otherwise finish adding it
for (int i = 0; i < 8; i++) {
// Get now shuffled cardType and this index
// (We know that only values 0..3 will appear and each will appear
// exactly twice - as only those values, and that multiplicity,
// have been added to the original array before shuffling.)
int cardType = [[cardTypes objectAtIndex:i] intValue];
// Get the card to apply the changes to, and do so
UIButton* card = [cards objectAtIndex:i];
[self updateCard:card withType:cardType];
// Then add the card view
[self.view addSubview:card];
}











card1Selected.hidden = YES;
card2Selected.hidden = YES;
card3Selected.hidden = YES;
card4Selected.hidden = YES;
card5Selected.hidden = YES;
card6Selected.hidden = YES;
card7Selected.hidden = YES;
card8Selected.hidden = YES;

card1Type = arc4random() %4;
card2Type = arc4random() %4;
card3Type = arc4random() %4;
card4Type = arc4random() %4;
card6Type = arc4random() %4;
card7Type = arc4random() %4;
card8Type = arc4random() %4;


[self Card1SelectedType];
[self Card2SelectedType];
[self Card3SelectedType];
[self Card4SelectedType];
[self Card5SelcetedType];
[self Card6SelectedType];
[self Card7SelectedType];
[self Card8SelectedType];







[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end //and this is the last error

最佳答案

首先,当您调用 [ar exchangeObjectAtIndex:i withObjectAtIndex:n]; 时,您会收到该错误,这意味着编译器不知道不存在名为“ar”的变量。您是否在任何时候创建了名为“ar”的变量?

其次,创建不带参数的函数的方式如下:

- (NSMutableArray*)createCardTypeDeck {

}

或者,如果您确实希望它接受一个参数,例如 NSString,您可以这样做:

- (NSMutableArray*)createCardTypeDeck:(NSString *)someString {

}

createCardTypeDeck 之后有“:”,但没有提供参数,因此要么像上面那样定义一个参数,要么删除 :。

第三,你的 updateCard 方法有一个 ( 而不是 { 作为方法主体,它应该是这样的:

- (void)updateCard: (UIButton*) card, cardType: (int) cardType {

}

解决这个问题也应该可以解决您的@end问题。

关于objective-c - No Visible @interface for 'GameViewController' 声明选择器 'createCardTypeDeck',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25459471/

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