gpt4 book ai didi

iphone - 返回一个静态常量[]

转载 作者:行者123 更新时间:2023-12-03 20:23:20 26 4
gpt4 key购买 nike

因此,在我的模型中,我有以下代码...我能够成功返回每个单独的值。我想知道如何返回整个扬声器表[]...也许一些建议。谢谢!

typedef struct {
NSUInteger speakerID;
NSString * speakerName;
NSString * speakerPosition;
NSString * speakerCompany;
} SpeakerEntry;

static const SpeakerEntry speakerTable [] =
{
{0, @"name", @"position", @"company"},
{1, @"name", @"position", @"company"},
{-1, nil, nil, nil}
};

这可以正常工作...

-(NSString *) stringSpeakerCompanyForId:(NSUInteger) identifier{
NSString * returnString = nil;
if ([self helpCount] > identifier) {
returnString = speakerTable[identifier].speakerCompany;
}
return returnString;

}

这根本不起作用..

-(id) getSpeaker{
//if ([speakerTable[0].speakerName isKindOfClass:[NSString class]])
// NSLog(@"YES");
NSArray * myArray3 = [NSArray arrayWithArray:speakerTable];
return myArray3;

}

最佳答案

arrayWithArray 需要一个 NSArray,而不是一个 C 数组。

第一个有效,因为您像使用 C 数组一样使用它。

或者 - 不使用结构体,而是使用对象:

创建一个名为“Speaker”的类。

在Speaker.h中

@interface Speaker : NSObject {}

@property (nonatomic, assign) NSUinteger id;
@property (nonatomic, copy) NSString name;
@property (nonatomic, copy) NSString position;
@property (nonatomic, copy) NSString company;

- (void)initWithId:(NSUInteger)anId name:(NSString *)aName position:(NSString *)aPosition company:(NSString *)aCompany;

@end

在Speaker.m

#import "Speaker.h"

@implementation Speaker

@synthesize id, name, position, company;

- (void)initWithId:(NSUInteger)anId name:(NSString *)aName position:(NSString *)aPosition company:(NSString *)aCompany {
if (!([super init])) {
return nil;
}
id = anId;
NSString name = [[NSString alloc] initWithString:aName];
NSString position = [[NSString alloc] initWithString:aPosition];
NSString company = [[NSString alloc] initWithString:aCompany];

return self;
}

- (void)dealloc {
[name release];
[position release];
[company release];
[super dealloc];
}

@end

现在,在您的调用代码中,您可以使用以下命令创建不可变的扬声器数组:

Speaker *speaker0 = [[Speaker alloc] initWithId:0 name:@"name0" position:@"position0" company:@"company0"];
Speaker *speaker1 = [[Speaker alloc] initWithId:1 name:@"name1" position:@"position1" company:@"company1"];
Speaker *speakerNull = [[Speaker alloc] initWithId:-1 name:nil position:nil company:nil];

NSArray *speakerArray [[NSArray arrayWithObjects: speaker0, speaker1, speakerNull] retain]

[speaker0 release];
[speaker1 release];
[speakerNull release];

注意:这是直接输入的,因此请随时提及/更正拼写错误或错误

关于iphone - 返回一个静态常量[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6521462/

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