初始化并分配多个变量/对象|目标C.

时间:2012-10-02 17:45:58

标签: objective-c variables initialization alloc

我该怎么做呢:

xmlObject *system_domain  = [xmlObject alloc] 
xmlObject *system_description = [xmlObject alloc]
xmlObject *system_type = [xmlObject alloc]

进入我只需编写xmlObject和[xmlObject alloc]一次的东西。

xmlObject *system_domain, *system_description, *system_type = [xmlObject alloc]

这使得所有这些都是xmlObjects,但不会分配每个。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

如果你有一组相关的对象,并且能够迭代它们比简单地访问它们更重要,你应该把它们放在一个集合中:

NSMutableDictionary *system_info = [NSMutableDictionary dictionary];
for (id key in @[@"domain", @"description", @"type"]) {
  system_info[key] = [xmlObject alloc];
}

然后,您使用system_dictionary代替system_info[@"dictionary"]。 (你甚至可以使用KVC来使事情变得更简洁,如果这很重要的话。)当然,如果这不适合你的用例,那么首先将它们粘贴在字典中是愚蠢的。

在任何其他用例中,您所做的是正常的,惯用的Objective C方式来声明三个xmlObject。如果你真的想知道周围是否有办法,当然有,但大多数都很傻。

更好的解决方案可能是切换语言。 Python,Ruby,Applescript,ObjC ++,eero等都可以像ObjC一样轻松访问ObjC运行时,并且有更简洁的惯用方法。例如,在Python中:

system_domain = xmlObject.alloc()
system_description = xmlObject.alloc()
system_type = xmlObject.alloc()

甚至:

system_domain, system_description, system_type = [xmlObject.alloc() for _ in range(3)]

另一个合理的选择,如果你必须连续初始化500个这样的东西,就是写一些生成你的ObjC代码的简单代码。

但如果你真的想留在ObjC,这里有一些愚蠢的解决方案:

只需执行以下操作即可将xmlObject次数从6减少到4:

xmlObject *system_domain = [xmlObject alloc],
     *system_description = [xmlObject alloc],
            *system_type = [xmlObject alloc];

或者3:

id system_domain = [xmlObject alloc];
id system_description = [xmlObject alloc];
id system_type = [xmlObject alloc];

或者1:

Class x = xmlObject;
id system_domain = [x alloc];
id system_description = [x alloc];
id system_type = [x alloc];

或者:

id makeXmlObject() { return [xmlObject alloc]; }
...

id system_domain = makeXmlObject();
id system_description = makeXmlObject();
id system_type = makeXmlObject();

一些附注:

您可能不想使用[xmlObject alloc]的结果。这是一块足够的内存来构造xmlObject,连接到xmlObject类,但完全没有初始化。您必须先调用初始值设定项(通常为-[init],但通常类似-[initWithValue: andOtherValue:]),然后才能对其执行任何有用的操作。

所以,大多数惯用的ObjC代码都会充满这样的调用:

Foo *foo = [[Foo alloc] init];
Bar *bar = [[Bar alloc] initWithFoo:foo];

此外,除非您使用ARC(或GC),否则您通常希望在对象初始化后autorelease;否则,你必须手动管理内存(这很难做到正确,因为它说快10倍)。所以,如果你必须处理非ARC代码,你会看到:

Bar *bar = [[[Bar alloc] initWithFoo:foo] autorelease];

幸运的是,许多类提供的类构造函数可以完成所有操作:

NSString *s = [NSString stringWithUTF8String:c]; // ARC or no ARC

你应该使用这些便利方法,但是习惯于alloc] init];(以及alloc] init] autorelease];,如果你必须处理ARC之前/之前的GC代码)习惯用法,因为你需要它。

在使用ObjC运行时绑定的所有其他语言中也是如此;例如,在Python中,您尽可能Bar.barWithFoo(foo),否则Bar.alloc().initWithFoo_(foo)

同时,你不能这样的原因:

xmlObject *system_domain, *system_description, *system_type = [xmlObject alloc];

......或......

system_domain = system_description = system_type = [xmlObject alloc];

...对此唯一合理的解释是将所有三个对象都设置为xmlObject相同实例。如果您只调用一次alloc,则只会分配一件事。

最后,用小写的第一个字母命名ObjC类被认为是不好的风格;它应该是XMLObject(或者可能是XmlObject,但Apple喜欢将首字母缩写词和首字母缩略词明确化,而不是xmlObject。而且,除了非常简单的项目之外,大多数人都希望为他们的所有类提供2或3个字母的前缀(如Apple的NS),以便区分来自不同子项目,第三方库等的类。 ,所以'ALIXMLObject`可能会更好。

答案 1 :(得分:0)

你必须做类似的事情:

xmlObject *system_domain = [xmlObject alloc],
     *system_description = [xmlObject alloc],
            *system_type = [xmlObject alloc];

这并不比你的第一个版本好。