So in my model I have the following code… I am successfully able to return each individual value. I want to know how am I able to return the entire speakerTable []… Maybe some advice. Thanks!
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}
};
This works correctly…
-(NSString *) stringSpeakerCompanyForId:(NSUInteger) identifier{
NSString * returnString = nil;
if ([self helpCount] > identifier) {
returnString = speakerTable[identifier].speakerCompany;
}
return returnString;
}
This does not work at all..
-(id) getSpeaker{
//if ([speakerTable[0].speakerName isKindOfClass:[NSString class]])
// NSLog(@"YES");
NSArray * myArray3 = [NSArray arrayWithArray:speakerTable];
return myArray3;
}
arrayWithArrayexpects an NSArray, not a C array.The first one works because you are using it like a C array.
Alternatively – don’t use a struct, use an object instead:
Create a class called Speaker.
In Speaker.h
in Speaker.m
And now in your calling code you can create an immutable array of speakers with:
note: this is typed straight in, so feel free to mention/correct typos or errors