Objective-C point syntax or property value?

I keep reading this point syntax, but I keep getting errors that the structure does not contain the members that I refer to. Perhaps this is not a point syntax, so I have included details of what I am doing in the hope 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 were that I was using the wrong @property, but I think I tried everything. I do the highlighting mostly. Ideally, I would like to use this point-to-point syntax and cannot understand why it does not allow it.

+3
source share
3 answers

: (edit: # 1 - )

  1. , NSArray . , mobInfo[1] , [mobInfo objectAtIndex:1]; mobInfo C-, .

  2. , main.h. MobDefs *mobdef; - main.m.

edit: :

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!
+9

, -AtIndex:, :

[[mobInfo objectAtIndex: 0] setMName: @"doug"];

((Mobrec *) [mobInfo objectAtIndex: 0]).MName = @"doug";
+3

[mobInfo objectAtIndex: 0 setmName: @ "doug" ];

objectAtIndex: setmName, , , .

mobInfo [1].MName = @ "eric";

Use objectAtIndex to view something in an NSArray object.

0
source

Source: https://habr.com/ru/post/1726981/


All Articles