首页 > 解决方案 > 如何在 main.m 中引用私有实例变量

问题描述

我正在学习objective-c,如下面的代码所示,我创建了一个带有私有实例变量部分的接口。在 main.m 中,创建了一个指针实例变量,如下所示。我想知道的是如何向私有实例变量之一发送消息?如下所示:

[ptr->_address XXX]..IS IT POSSIBLE

2-以及如何使用初始化私有实例变量

[[xxx alloc] initWithxxx]……IS IT POSSIBLE??

主文件

    MeAsImpl *ptr = [[MeAsImpl alloc] initWithCountry:@"DEU" andCity:@"KA" andAddress:@"xyzstrasse7"];

    NSLog(@"chip:[ %@ ]", ptr->_address);
    NSLog(@"chip:[ %p ]", structChipInitializerSegment);
    NSLog(@"chip:[ %@ ]", structChipInitializerSegment->chipName.self);
    NSLog(@"chip:[ %p ]", structChipInitializerSegment->chipName.self);
    NSLog(@"chip:[ %p ]", structChipInitializerSegment->numOfTotalChannels);

.h

#import "ProtocolKidsSchedules.h"
#ifndef MeAsImpl_h
#define MeAsImpl_h

#endif /* MeAsImpl_h */

@interface MeAsImpl : NSObject<ProtocolKidsSchedules> {
    id<ProtocolKidsSchedules> _signee;
   NSString *_country;
    NSString *_city;
    NSString *_address;
}

@property (nonatomic, retain) id<ProtocolKidsSchedules> signee;
@property (nonatomic, retain) MeAsImpl *owner;
@property (nonatomic, retain)  NSString *country;
@property (nonatomic, retain)  NSString *city;
@property (nonatomic, retain)  NSString *address;

-(NSString *) signeeOfTheContract: (id<ProtocolKidsSchedules>) signee;
-(id) initWithCountry: (NSString *) country andCity: (NSString *) city andAddress: (NSString *) address;
-(id) initWithOnlyCity: (NSString *) city;
-(id) initWithOnlyAddress: (NSString *) address;

@end

.m

#import <Foundation/Foundation.h>
#import "MeAsImpl.h"

@implementation MeAsImpl
@synthesize country, city, address;

-(BOOL)areChoresCompleted:(BOOL)completed {
   return completed;
}

-(BOOL)areHomeWorkCompleted:(BOOL)completed {
    return completed;
}

-(BOOL)doDerserveCookies:(BOOL)doDeserve {
        return doDeserve;
}

-(NSString *)signeeOfTheContract:(id<ProtocolKidsSchedules>)signee {
   return @"Signee is: ";
}
-(id)initWithCountry:(NSString *)country andCity:(NSString *)city andAddress:(NSString *)address {
    self->_countery = country;
    self->_city = city;
    self->_address = address;
    return self;
}

-(id)initWithOnlyCity:(NSString *)city {
    self->_city = city;
   return self;
}
-(id)initWithOnlyAddress:(NSString *)address {
    self->_address = address;
    return self;
}

@end

标签: objective-c

解决方案


推荐阅读