我试图找出是否可以在目标c中使用点语法来访问类变量。
例如,如果我有一个名为ClassA的类,类型为NSObject。
ClassA有一个名为ClassB的类的实例,类型也是NSObject。
ClassB有一个名为myString的变量,类型为NSString;
在具有ClassA实例作为变量的视图控制器中,我试图通过说
来访问“myString”NSString *aString=classA.classB.myString;
这给了我这个错误
error: accessing unknown 'myString' component of a property
如果可以使用点语法访问classB,为什么不能使用字符串变量?
///////////添加编辑//////////
抱歉延误。 评论框也只允许这么多字符,所以我不得不使用anser框。
#import <Foundation/Foundation.h>
@class PromoTrackValueObject;
@class PromoMixValueObject;
@class PromoSkinValueObject;
@class EventsValueObject;
@class BuyValueObject;
@interface PromoValueObject : NSObject {
NSString *promoXMLPath;
NSString *type;
NSString *username;
NSString *entityname;
NSString *userid;
NSString *hasavatar;
NSString *trackbuy_profile;
NSString *bio;
NSString *country;
NSString *url_facebook;
NSString *url_twitter;
NSString *url_discog;
NSString *url_myspace;
NSString *url_chart;
NSString *labelname;
NSString *labelurl;
NSString *labelimg;
NSString *agent_name;
NSString *agent_url;
NSString *agent_img;
NSString *promo_date;
PromoTrackValueObject *promoTrack;
PromoMixValueObject *promoMix;
PromoSkinValueObject *promoSkin; // class containing string property in question is in here
EventsValueObject *events;
BuyValueObject *buy;
}
@property (nonatomic,copy)NSString *promoXMLPath;
@property (nonatomic,copy)NSString *type;
@property (nonatomic,copy)NSString *username;
@property (nonatomic,copy)NSString *entityname;
@property (nonatomic,copy)NSString *userid;
@property(nonatomic,copy)NSString *hasavatar;
@property (nonatomic,copy)NSString *trackbuy_profile;
@property(nonatomic,copy)NSString *bio;
@property(nonatomic,copy)NSString *country;
@property(nonatomic,copy)NSString *url_facebook;
@property(nonatomic,copy)NSString *url_twitter;
@property(nonatomic,copy)NSString *url_discog;
@property(nonatomic,copy)NSString *url_myspace;
@property(nonatomic,copy)NSString *url_chart;
@property(nonatomic,copy)NSString *labelname;
@property(nonatomic,copy)NSString *labelurl;
@property(nonatomic,copy)NSString *labelimg;
@property(nonatomic,copy)NSString *agent_name;
@property(nonatomic,copy)NSString *agent_url;
@property(nonatomic,copy)NSString *agent_img;
@property(nonatomic,copy)NSString *promo_date;
@property(nonatomic,retain)PromoTrackValueObject *promoTrack;
@property(nonatomic,retain)PromoMixValueObject *promoMix;
@property(nonatomic,retain)PromoSkinValueObject *promoSkin;
@property(nonatomic,retain)EventsValueObject *events;
@property(nonatomic,retain)BuyValueObject *buy;
@end
#import "PromoValueObject.h"
@implementation PromoValueObject
@synthesize promoXMLPath;
@synthesize type;
@synthesize username;
@synthesize entityname;
@synthesize userid;
@synthesize hasavatar;
@synthesize trackbuy_profile;
@synthesize bio;
@synthesize country;
@synthesize url_facebook;
@synthesize url_twitter;
@synthesize url_discog;
@synthesize url_myspace;
@synthesize url_chart;
@synthesize labelname;
@synthesize labelurl;
@synthesize labelimg;
@synthesize agent_name;
@synthesize agent_url;
@synthesize agent_img;
@synthesize promoMix;
@synthesize promoSkin;
@synthesize events;
@synthesize buy;
@synthesize promoTrack;
@synthesize promo_date;
- (void)dealloc {
[promoTrack release];
[promoMix release];
[promoSkin release];
[events release];
[buy release];
[promoXMLPath release];
[type release];
[username release];
[entityname release];
[userid release];
[hasavatar release];
[trackbuy_profile release];
[bio release];
[country release];
[url_facebook release];
[url_twitter release];
[url_discog release];
[url_chart release];
[labelname release];
[labelurl release];
[labelimg release];
[agent_name release];
[agent_url release];
[agent_img release];
[promo_date release];
[super dealloc];
}
@end
@interface PromoSkinValueObject : NSObject {
NSString *promo_skin_url; // the string I'm after
NSString *promo_skin_id;
}
@property(nonatomic,retain)NSString *promo_skin_url;
@property(nonatomic,retain)NSString *promo_skin_id;
@end
#import "PromoSkinValueObject.h"
@implementation PromoSkinValueObject
@synthesize promo_skin_url;
@synthesize promo_skin_id;
@end
//尝试在此处添加点语法
这行代码在一个syctizes promoValueObject
的类中NSString *skin=promoValueObject.promoSkin.promo_skin_url;
答案 0 :(得分:5)
点语法被设计为对发出setter / getter角色的方法的方法调用的精确同义词。要使用.
[dot]语法,必须精确指定对象的类型,并且对象必须实现适当的方法。点语法 not 用于以某种方式直接访问实例变量。
因此,你说:
ClassB有一个名为的变量 NSString类型的myString;
这可能就是问题所在。您需要 定义方法以访问myString
或声明属性和@synthesize
方法。两者都可以工作,两者都是有效的(除了使用atomic
时方法综合的细节)。
看来你混淆了“类”和“实例”。您的代码中的所有内容都指向此实例以及您尝试执行this.that.something
something
失败的内容?
问题是您使用@class
转发声明类引用。基本上,在编译器看到完整的类定义之前,编译器只看到Foo *
的任何表达式如@class Foo;
就像对未指定类的类的实例的泛型引用(有点像,但不完全,id
)。
您需要#import包含类声明的文件。从编译器可见性的角度来考虑它。编译具有点语法表达式的文件时,请考虑编译器在编译该表达式之前看到的确切声明集。如果没有看到类的@interface
,则不能在该类的实例上使用点语法。
答案 1 :(得分:0)
使用ivar不能使dot-syntax工作。您需要通过添加
使其成为属性@property(retain) NSString* myString;
到@interface,
@synthesize myString;
到@implementation。
基本上,getter点语法abc.efg
可以转换为[abc efg]
。但要访问ivar,请使用语法abc->efg
,这是一个完全不同的概念。有关详细信息,请参阅“Declared Properties”。