gpt4 book ai didi

ios - 找不到我的方法定义,但我不确定为什么

转载 作者:行者123 更新时间:2023-11-28 22:24:30 26 4
gpt4 key购买 nike

我有一个带标签浏览的 Storyboard。其中一个选项卡是表格 View 。该表由动物“标题”列表填充。当按下标题时,会打开一个详细 View ,显示标题以及声音和动物被点击的次数。我设置了一个 View Controller 。我还有 item.h/m 和 itemstore.h/m。还有一个详细的 View Controller 。我目前的问题是,在项目商店中我设置了两个数组,但马上 xcode 告诉我找不到方法定义。它也给我未声明的标识符错误。

FSAnimalsViewController.h(这是我的 TableView Controller )

#import <AVFoundation/AVAudioPlayer.h>
#import <UIKit/UIKit.h>
#import "FSDetailViewController.h"

@interface FSAnimalsViewController : UITableViewController
{
}
@end

FSAnimalsViewController.m

#import "FSAnimalsViewController.h"
#import "FSItemStore.h"
#import "FSItem.h"

@implementation FSAnimalsViewController
- (id)init
{
// Call the superclass's designated initializer
self = [super initWithStyle:UITableViewStyleGrouped];
if (self)
{
UINavigationItem *n = [self navigationItem];

[n setTitle:@"FoxSays"];

// Create a new bar button item that will send
// addNewItem: to ItemsViewController

[[self navigationItem] setLeftBarButtonItem:[self editButtonItem]];

}
return self;
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[self tableView] reloadData];
}


- (id)initWithStyle:(UITableViewStyle)style
{
return [self init];
}


- (void)tableView:(UITableView *)aTableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
FSDetailViewController *detailViewController = [[FSDetailViewController alloc] init];

NSArray *items = [[FSItemStore defaultStore] allItems];
FSItem *selectedItem = [items objectAtIndex:[indexPath row]];

// Give detail view controller a pointer to the item object in row
[detailViewController setItem:selectedItem];

// Push it onto the top of the navigation controller's stack
[[self navigationController] pushViewController:detailViewController
animated:YES];
}


- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [[[FSItemStore defaultStore] allItems] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Set the cell identifier
static NSString *CellIdentifier = @"BasicCell";

// Reuse the cell from the identifier
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell if it doesn't exist
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Log the row for debugging
NSLog(@"%d", [indexPath row]);

// Get object from store
FSItem *item = [[[FSItemStore defaultStore] allItems] objectAtIndex:[indexPath row]];

// Set label to from property in object
[[cell textLabel] setText:[item title]];

return cell;
}


@end

FSItem.h

#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVAudioPlayer.h>

@interface FSItem : NSObject

@property (nonatomic, copy) NSString *title;
@property (nonatomic) SystemSoundID *sound;
@property (nonatomic) int plays;


- (NSArray *)animals;
- (NSArray *)sounds;

@end

FSItem.m

#import <AudioToolbox/AudioToolbox.h>
#import "FSItem.h"

@implementation FSItem

NSString *title;
SystemSoundID *sound;
int plays;

- (NSArray *)animals
{
NSArray *animals = [NSArray arrayWithObjects:@"Dog",@"Cat",@"Bird",@"Mouse",@"Cow",@"Frog",@"Elephant",@"Duck",@"Fish",@"Seal",@"Fox", nil];

return animals;
}

- (NSArray *)sounds
{
NSArray *sounds = [NSArray arrayWithObjects:
@"Woof.mp3",
@"Meow.mp3",
@"tweet.mp3",
@"Squeak.mp3",
@"Moo.mp3",
@"Croak.mp3",
@"Toot.mp3",
@"Quack.mp3",
@"Blub.mp3",
@"OWOwOw.mp3",
@"Fox.mp3",
nil];

return sounds;
}

@end

FSItemStore.h

#import <AVFoundation/AVAudioPlayer.h>
#import <AudioToolbox/AudioToolbox.h>
#import <Foundation/Foundation.h>
@class FSItem;


@interface FSItemStore : NSObject
{
NSMutableArray *allItems;
}

@property (nonatomic) int i;





+ (FSItemStore *)defaultStore;


- (NSArray *)allItems;
- (NSArray *)animals;
- (NSArray *)sounds;
- (FSItem *)createItem;


@end

FSItemStore.m

#import <AudioToolbox/AudioToolbox.h>
#import "FSItem.h"
#import "FSItemStore.h"

@implementation FSItemStore

int i = 0;

- (NSArray *)allItems
{
return allItems;
}



+ (FSItemStore *)defaultStore;
{
static FSItemStore *defaultStore = nil;
if(!defaultStore)
defaultStore = [[super allocWithZone:nil] init];

return defaultStore;
}



- (FSItem *)createItem
{
FSItem *item = [[FSItem alloc] init];

if (i < [animals count])
{
[item setTitle: [animals objectAtIndex: i]];
[item setSound: [sounds objectAtIndex: i]];
[item setPlays: 0];
i++;

[allItems addObject: item];
}

return item;

}

@end

FSItemStore 似乎是我的问题所在。它说找不到声音和动物的方法定义,声音和动物都是未声明的标识符。有人有什么想法吗?

最佳答案

你的问题是,在你的 H 文件中,你声明你的类将实现一个名为 animals 的方法,该方法将返回一个 NSArray,并且方法调用听起来将返回另一个 NSArray,但在你的 M 文件中你没有实现这些方法。

在您的 FSItemStore.m 中,您应该实现这些方法:

- (NSArray *)animals{
//Do whatever this method is supposed to do, return an NSArray.
}
- (NSArray *)sounds
{
//Do whatever this method is supposed to do, return an NSArray.
}

编辑

如果您假装 FSItemStore 继承了 FSItem 的方法,则必须以这种方式声明接口(interface):

@interface FSItemStore : FSItem  //FSItem instead of NSObject
{
NSMutableArray *allItems;
}

关于ios - 找不到我的方法定义,但我不确定为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19361742/

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