初始化并分配多个变量/对象|目标C [英] Initialize and allocate multiple variables/objects | Objective C

查看:89
本文介绍了初始化并分配多个变量/对象|目标C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何处理:

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

到我只能

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

这将使它们全部成为xmlObject,但不会分配每个。任何帮助将不胜感激。

This makes all of them xmlObjects, but does not allocate each one. Any help would be much appreciated.

推荐答案

如果您有一组相关对象,并且能够迭代它们比能够简洁地访问它们更重要,您应该将它们粘贴在集合中:

If you've got a set of related objects, and being able to iterating over them is more important than being able to access them concisely, you should stick them in a collection:

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

然后,而不是 system_dictionary 您使用 system_info [@ dictionary] 。 (如果重要的话,您甚至可以使用KVC使事情更简洁。)当然,如果这不适合您的用例,将它们放在字典的第一位就是愚蠢的。

Then, instead of system_dictionary you use system_info[@"dictionary"]. (You could even use KVC to make things more concise, if that's important.) Of course if that doesn't fit your use case, it would be silly to stick them in a dictionary in the first place.

在任何其他用例中,您所做的都是使用普通的惯用的Objective C方法来声明三个 xmlObject s。当然,如果您真的想知道是否有解决方法,但是大多数方法都很傻。

In any other use case, what you've done is the normal, idiomatic Objective C way to declare three xmlObjects. If you really want to know if there are ways around it, of course there are, but most of them are pretty silly.

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

A better solution might be to switch languages. Python, Ruby, Applescript, ObjC++, eero, etc. all let you access the ObjC runtime just as easily as ObjC, and have more concise idiomatic ways of doing things. For example, in 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代码。

Another reasonable option, if you have to initialize 500 of these things in a row, is to write some simple code that generates your ObjC code.

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

But if you really want to stay in ObjC, here are some of the silly solutions:

您可以减少<$ c的数量只需执行以下操作即可将$ c> xmlObject 从6出现到4:

You can cut the number of xmlObject appearances from 6 to 4 just by doing this:

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:] -在您可以执行任何有用的操作之前。

You probably don't want to use the result of [xmlObject alloc]. That's a block of enough memory to construct an xmlObject, connected to the xmlObject class, but otherwise completely uninitialized. You have to call an initializer—typically -[init], but often something like -[initWithValue: andOtherValue:]—before you can do anything useful with it.

因此,大多数惯用的ObjC代码将充满这样的调用:

So, most idiomatic ObjC code will be full of calls like this:

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

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

Also, unless you're using ARC (or GC), you usually want to autorelease the object as soon as it's initialized; otherwise, you have to manage the memory manually (and that's as hard to do properly as it is to say 10 times fast). So, if you ever have to deal with non-ARC code, you'll see this:

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

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

Fortunately, many classes provide class constructors that do everything in one:

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

如果存在这些便捷方法,则应使用它们,但请习惯于 alloc] init]; (和 alloc] init] autorelease]; ,如果您必须处理

You should use these convenience methods when they exist, but get used to the alloc] init]; (and alloc] init] autorelease];, if you ever have to deal with pre-ARC/pre-GC code) idiom, because you will need it.

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

The same is true in all other languages with ObjC runtime bindings; e.g., in Python, you do Bar.barWithFoo(foo) when possible, Bar.alloc().initWithFoo_(foo) otherwise.

同时,您之所以不能这样:

Meanwhile, the reason you can't something like this:

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

...或…

system_domain = system_description = system_type = [xmlObject alloc];

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

… is that the only reasonable interpretation of this would be to set all three objects to the same instance of xmlObject. If you only call alloc once, only one thing gets allocated.

最后,将ObjC命名为坏样式小写的第一个字母的类;它应该是 XMLObject (或者也许是 XmlObject ,但是Apple喜欢将首字母缩写词和首字母缩写明确化),而不是 xmlObject 。而且,除了非常简单的项目外,大多数人喜欢给所有类一个2或3个字母的前缀(例如Apple的NS),以便区分来自不同子项目,第三方库等的类。 ,因此 ALIXMLObject可能会更好。

Finally, it's considered bad style to name ObjC classes with a lowercase first letter; it should be XMLObject (or maybe XmlObject, but Apple likes to make acronyms and initialisms explicit), not xmlObject. And, except in very simple projects, most people like to give all of their classes a 2- or 3-letter prefix (like Apple's NS), to make it possible to distinguish classes coming from different subprojects, third-party libraries, etc., so 'ALIXMLObject` might be even better.

这篇关于初始化并分配多个变量/对象|目标C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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