tags:

views:

255

answers:

3

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 initMobTable];

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: 

[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.

Azeem.Butt
I think he meant `[[mobInfo objectAtIndex:0] setmName: @"doug"];`. The other answers seem to have corrected that.
Daniel Yankowsky
+8  A: 

A couple of things: (edit: original point #1 removed due to error)

  1. Although the dot syntax is supported, the array index syntax for NSArray is not. Thus, your call to mobInfo[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.

  2. 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!
e.James
I'll tack on to this since it isn't really enough for a separate 'answer':4. When you're defining variables in your @interface, you don't need to specify @public for each one. If you put @public before the first one, all subsequent instance variables (until you specify @protected or @private) will be public.
Jeff Kelley
Good point. I removed the `@public` keywords entirely, since they are not needed at all in this case.
e.James
Regarding point 1: mobInfo is a member of MobDefs, not MobRec, so synthesizing it in MobRec doesn't make sense, nor does the sample code you've posted reflect this.
Dewayne Christensen
Very true. Thank you for spotting that. I have removed it from my answer.
e.James
+3  A: 

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";
Ben Gottlieb
I think the property access should be .mName, not .MName, given his declaration. douglasrahn, it's a very bad idea to choose a property name like `mMember.` Every language has its own convention; Obj-C don't use the leading m!
Yuji
There is no convention, and the m prefix is used in several Apple examples--probably written by former PowerPlant fans.
Azeem.Butt
Ah, I've seen mSomething for instance variables, but not for properties:p Could you give me the link? Thanks in advance!
Yuji
usually properties have no leading character; the member variables backing them frequently do, however.
Ben Gottlieb
If you find an example that has such a prefix, file a bug. It is against convention.
bbum