I keep reading that dot syntax is possible but I keep getting errors that the struct does not contain members I am referencing. Perhaps its not the dot syntax so I have included details of what I am doing in hopes of a solution:
// MobRec.h - used as the objects in the MobInfo array
#import <Foundation/Foundation.h>
@interface MobRec : NSObject {
@public NSString *mName;
@public int mSpeed;
}
@property (nonatomic, retain) NSString *mName;
@property (nonatomic) int mSpeed;
// MobDefs.h - array of MobRecords
@interface Mobdefs : NSObject {
@public NSMutableArray *mobInfo;
}
@property(assign) NSMutableArray *mobInfo; // is this the right property?
-(void) initMobTable;
@end
// MobDefs.m
#import "Mobdefs.h"
#import "Mobrec.h"
@implementation Mobdefs
@synthesize mobInfo;
-(void) initMobTable
{
// if I use traditional method I get may not respond
[mobInfo objectAtIndex:0 setmName: @"doug"];
// if I use dot syntax I get struct has no member named mName
mobInfo[1].MName = @"eric";
}
// main.h
MobDefs *mobdef;
// main.m
mobdef = [[Mobdefs alloc] init];
[mobdef initM开发者_运维技巧obTable];
although both methods should work I get erros on both. What am I doing wrong? My best thoughts have been that I am using the wrong @property but I think I have tried all. I am performing alloc in main. Ideally I would like to for this use dot syntax and cant see why its not allowing it.
A couple of things: (edit: original point #1 removed due to error)
Although the dot syntax is supported, the array index syntax for
NSArray
is not. Thus, your call tomobInfo[1]
will not be the same as[mobInfo objectAtIndex:1];
Instead,mobInfo
will be treated as a simple C-style array, and that call would be almost guaranteed to result in a crash.You should not define variables in your header file as you do in main.h. The line
MobDefs *mobdef;
belongs somewhere in main.m.
edit: Here is how it should look:
MobRec.h
@interface MobRec : NSObject {
NSString *mName;
int mSpeed;
}
@property (nonatomic, retain) NSString *mName;
@property (nonatomic) int mSpeed;
MobRec.m
@implementation MobRec
@synthesize mName;
@synthesize mSpeed;
@end
MobDefs.h
@interface MobDefs : NSObject {
NSMutableArray *mobInfo;
}
@property(assign) NSMutableArray *mobInfo;
-(void) initMobTable;
@end
MobDefs.m
#import "MobDefs.h"
#import "MobRec.h"
@implementation MobDefs
@synthesize mobInfo;
-(void) initMobTable
{
// option 1:
[(MobRec*)[mobInfo objectAtIndex:0] setMName:@"doug"];
// option 2:
(MobRec*)[mobInfo objectAtIndex:0].mName = @"eric";
// option 3:
MobRec *mobRec = [mobInfo objectAtIndex:0];
mobRec.mName = @"eric";
}
main.m
MobDef *mobdef = [[MobDefs alloc] init];
[mobdef initMobTable];
...
[mobdef release]; // don't forget!
You need to either cast the object returned by -objectAtIndex:, or use a method call on it:
[[mobInfo objectAtIndex: 0] setMName: @"doug"];
or
((Mobrec *) [mobInfo objectAtIndex: 0]).MName = @"doug";
[mobInfo objectAtIndex:0 setmName: @"doug"];
There is no objectAtIndex:setmName method, so you're going to have to explain what you think this is even supposed to do.
mobInfo[1].MName = @"eric";
Use objectAtIndex to look something up in an NSArray object.
精彩评论