点语法如何在目标 c 中工作 [英] how does dot syntax work in objective c

查看:23
本文介绍了点语法如何在目标 c 中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在目标 c 中使用点语法来访问类变量.

I'm trying to figure out if I can use dot syntax in objective c in order to access class variables.

例如,如果我有一个 NSObject 类型的名为 ClassA 的类.

For example if I have a class named ClassA of type NSObject.

ClassA 有一个名为 ClassB 的类的实例,也是 NSObject 类型.

ClassA has an instance of a class named ClassB also of type NSObject.

ClassB 有一个名为 myString 的 NSString 类型的变量;

And ClassB has a variable named myString of type NSString;

在将 ClassA 实例作为变量的视图控制器中,我试图通过说来访问myString"

In a view controller that has an instance of ClassA as a variable I'm trying to access "myString" by saying

NSString *aString=classA.classB.myString;

这给了我这个错误

 error: accessing unknown 'myString' component of a property

如果 classB 可以用点语法访问,为什么字符串变量不能访问?

if classB can be accessed with the dot syntax why can't the string variable?

///////////添加编辑//////////

///////////added edit//////////

抱歉耽搁了.此外,评论框只允许这么多字符,所以我不得不改用分析框.

Sorry for the delay. Also the comment box only allows so many characters so I had to use the anser box instead.

#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

//尝试在此处添加点语法

//trying to add dot syntax here

这行代码在一个定义 promoValueObject 的类中

This line of code is in a class which sytesizes promoValueObject

NSString *skin=promoValueObject.promoSkin.promo_skin_url;

推荐答案

点语法被设计为作为方法调用的精确同义词,用于扮演 setter/getter 角色的方法.要使用 . [点] 语法,必须准确指定对象的类型并且对象必须实现适当的方法.点语法用于以某种方式直接访问实例变量.

The dot syntax is designed to be an exactly precise synonym for method invocations to methods that play the setter/getter role. To use the . [dot] syntax, the type of the object must be exactly specified and the object must implement the appropriate method. Dot syntax is not used to somehow gain direct access to instance variables.

因此,你说:

ClassB 有一个名为的变量类型为 NSString 的 myString;

And ClassB has a variable named myString of type NSString;

这很可能是问题所在.您需要要么定义访问myString的方法或声明一个属性和@synthesize方法.两者都可以工作并且两者实际上是等效的(使用 atomic 时保留方法合成的细节).

And this is likely the problem. You need to either define methods to access myString or declare a property and @synthesize the methods. Either will work and both are effectively equivalent (save for the details of method synthesis when using atomic).

您似乎混淆了类"和实例".你的代码中的所有内容都指向这个和那个的实例,你正在尝试做类似 this.that.something 的事情,其中​​ something 失败了?

It appears you are confusing "class" and "instance". Everything in your code points to instances of this and that and you are trying to do something like this.that.something where something is failing?

问题在于您使用 @class 来转发声明类引用.基本上,在编译器看到完整的类定义之前,任何像 Foo * 这样编译器只看到 @class Foo; 的表达式就像对实例的通用引用一样属于未指定类型的类(有点像但不完全是 id).

The problem is that you are using @class to forward declare the class reference. Basically, until the full class definition is seen by the compiler, any expression like Foo * where the compiler has only seen @class Foo; acts like a generic reference to an instance of a class of unspecified type (kind of like, but not quite, id).

您需要#import 包含类声明的文件.从编译器可见性的角度考虑.当您编译具有点语法表达式的文件时,请考虑编译器在编译该表达式之前看到的确切声明集.如果没有看到该类的 @interface,则不能在该类的实例上使用点语法.

You need to #import the files containing the declarations of your classes. Think of it in terms of compiler visibility. When you compile the file that has the dot syntax expression, consider the exact set of declarations the compiler has seen prior to that expression being compiled. If the @interface for the class hasn't been seen, you can't use dot syntax on an instance of that class.

这篇关于点语法如何在目标 c 中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆