所以在我的模型中我有以下代码...我成功地能够返回每个单独的值。我想知道我怎么能够返回整个speakerTable [] ...也许有些建议。谢谢!
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;
}
答案 0 :(得分:6)
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];
注意:这是直接输入的,所以随意提及/纠正错别字或错误
答案 1 :(得分:2)
方法arrayWithArray
接受NSArray
作为参数,而不是C数组。