Newbie question. Looking at arrays (ie: dynamically sized) this works:
新手问题。查看数组(即:动态大小),这有效:
NSArray *array;
array = [NSArray arrayWithObjects:
@"one", @"two", nil];
This does not:
这不是:
array = [NSArray arrayWithObjects:
1, 2, nil];
Ok, I get it. This works:
好的我明白了。这有效:
array = [NSArray arrayWithObjects:
[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil];
Its sorta less "on the fly" as C++ / Java. I see the same thing with the init examples I'm reading. For example:
它有点像C ++ / Java那样“在飞行中”。我正在阅读的init示例中看到了同样的事情。例如:
// pseudo objc example
MyVar v = [MyVar init]; // blank
[v setSomething];
[v setSomethingElse];
// use v down here
In C++/Java I'd do:
在C ++ / Java中我会这样做:
MyVar v = new MyVar("foo", "bar", "baz", "quux");
And I'd know that v is ready to go by default. Is there a spirit of ObjC that I should not fight? Should I just expect to write more lines and less "one-liners"?
而且我知道v已经准备就绪了。 ObjC的精神是否我不应该战斗?我是否应该期望写更多的线而不是“单线”?
5 个解决方案
#1
In Objective-C, the "init" method is just a method. Unlike Java or C++ whose constructors are different than other methods. So you can define your own init methods that behave like C++ or Java constructors. For example, you could define an init method that takes several parameters. It might look something like this.
在Objective-C中,“init”方法只是一种方法。与Java或C ++不同,其构造函数与其他方法不同。因此,您可以定义自己的init方法,其行为类似于C ++或Java构造函数。例如,您可以定义一个带有多个参数的init方法。它可能看起来像这样。
MyVar* v = [[MyVar alloc] initWithName:@"foo" andTitle:@"bar"];
// do something with v
[v release];
Common practice is to simply create new methods that perform object initialization, and prefix the method name with "init" for clarity and consistency.
通常的做法是简单地创建执行对象初始化的新方法,并在方法名称前加上“init”,以便清晰和一致。
#2
NSArray
, its modifiable variant NSMutableArray
, and all collection structures (NSDictionary
, NSSet
) are made for storing objects. This is why you see [NSNumber numberWithInt:1]
instead of simply 1
.
NSArray,其可修改的变体NSMutableArray,以及所有集合结构(NSDictionary,NSSet)用于存储对象。这就是为什么你看到[NSNumber numberWithInt:1]而不是1。
For strings, note that an Objective-c string (like @"one"
, including leading @
) is an object of type NSString
, whereas a C string (like "one"
, without @
) is not an object.
对于字符串,请注意Objective-c字符串(如@“one”,包括leading @)是NSString类型的对象,而C字符串(如“one”,不带@)不是对象。
If you want the simplicity of storing simple values in arrays, don't forget that Objective-C is a superset of C. This means that you can use a declaration like:
如果您希望在数组中存储简单值的简单性,请不要忘记Objective-C是C的超集。这意味着您可以使用如下声明:
int array[] = { 1, 2, 3 };
#3
What you get with Objective-C's verbosity is greater readability.
Objective-C的详细程度可以获得更高的可读性。
Sure, while you're writing code, it might be easy to knock up something like you wrote
当然,在你编写代码时,可能很容易就像你写的一样
MyRect rect = new MyRect(1.0, 1.0, 3.0 3.0);
But when you, or more likely someone else, comes to maintain your code then I would argue that this is much easier to read:
但是当你,或者更可能是其他人来维护你的代码时,我会认为这更容易阅读:
MyRect *rect = [[MyRect alloc] initWithX:1.0 Y:1.0 width:3.0 height:3.0];
And in these days of smart editors it isn't that much harder to write.
在智能编辑的这些日子里,写起来并不难。
#4
You can write constructors that take named parameters to get it down to one line:
您可以编写带有命名参数的构造函数,将其缩减为一行:
MyVar *v = [[MyVar alloc] initWithFoo:@"foo" bar:@"bar" baz:@"baz" quux:@"quux"];
#5
Yeah, Objective-C is pretty verbose, so you're going to need to get used to that.
是的,Objective-C非常冗长,所以你需要习惯它。
The MyVar init
case is not quite right; you generally need to do a [[MyVar alloc] init]
or a [MyVar new]
. And in many cases, there are init
variants that take arguments, like
MyVar init案例并不完全正确;你通常需要做[[MyVar alloc] init]或[MyVar new]。在许多情况下,有一些init变量带有参数,比如
MyVar v = [[MyVar alloc] initWithSomething: "foo" somethingElse: "bar"];
If you want something less verbose, where you can get more one liners in, you might want to look into MacRuby, which is a binding between Ruby and Objective-C that gives you access to all of the Cocoa APIs but with a much more compact, high level syntax (and you can go even more compact still with HotCocoa. Of course, there is a bit of a performance penalty using MacRuby, so if you're doing a high performance game it might not be ideal to write your drawing loop in RubyCocoa, but its fine for the vast majority of applications.
如果你想要的东西更简洁,在那里你可以得到更多的一个套,你可能要考虑的MacRuby,这是红宝石和Objective-C,让您可以访问所有的可可的API,但有一个更紧凑之间的绑定,高级语法(你可以使用HotCocoa更加紧凑。当然,使用MacRuby会有一点性能损失,所以如果你正在进行高性能游戏,那么编写绘图循环可能并不理想在RubyCocoa中,但它适用于绝大多数应用程序。
#1
In Objective-C, the "init" method is just a method. Unlike Java or C++ whose constructors are different than other methods. So you can define your own init methods that behave like C++ or Java constructors. For example, you could define an init method that takes several parameters. It might look something like this.
在Objective-C中,“init”方法只是一种方法。与Java或C ++不同,其构造函数与其他方法不同。因此,您可以定义自己的init方法,其行为类似于C ++或Java构造函数。例如,您可以定义一个带有多个参数的init方法。它可能看起来像这样。
MyVar* v = [[MyVar alloc] initWithName:@"foo" andTitle:@"bar"];
// do something with v
[v release];
Common practice is to simply create new methods that perform object initialization, and prefix the method name with "init" for clarity and consistency.
通常的做法是简单地创建执行对象初始化的新方法,并在方法名称前加上“init”,以便清晰和一致。
#2
NSArray
, its modifiable variant NSMutableArray
, and all collection structures (NSDictionary
, NSSet
) are made for storing objects. This is why you see [NSNumber numberWithInt:1]
instead of simply 1
.
NSArray,其可修改的变体NSMutableArray,以及所有集合结构(NSDictionary,NSSet)用于存储对象。这就是为什么你看到[NSNumber numberWithInt:1]而不是1。
For strings, note that an Objective-c string (like @"one"
, including leading @
) is an object of type NSString
, whereas a C string (like "one"
, without @
) is not an object.
对于字符串,请注意Objective-c字符串(如@“one”,包括leading @)是NSString类型的对象,而C字符串(如“one”,不带@)不是对象。
If you want the simplicity of storing simple values in arrays, don't forget that Objective-C is a superset of C. This means that you can use a declaration like:
如果您希望在数组中存储简单值的简单性,请不要忘记Objective-C是C的超集。这意味着您可以使用如下声明:
int array[] = { 1, 2, 3 };
#3
What you get with Objective-C's verbosity is greater readability.
Objective-C的详细程度可以获得更高的可读性。
Sure, while you're writing code, it might be easy to knock up something like you wrote
当然,在你编写代码时,可能很容易就像你写的一样
MyRect rect = new MyRect(1.0, 1.0, 3.0 3.0);
But when you, or more likely someone else, comes to maintain your code then I would argue that this is much easier to read:
但是当你,或者更可能是其他人来维护你的代码时,我会认为这更容易阅读:
MyRect *rect = [[MyRect alloc] initWithX:1.0 Y:1.0 width:3.0 height:3.0];
And in these days of smart editors it isn't that much harder to write.
在智能编辑的这些日子里,写起来并不难。
#4
You can write constructors that take named parameters to get it down to one line:
您可以编写带有命名参数的构造函数,将其缩减为一行:
MyVar *v = [[MyVar alloc] initWithFoo:@"foo" bar:@"bar" baz:@"baz" quux:@"quux"];
#5
Yeah, Objective-C is pretty verbose, so you're going to need to get used to that.
是的,Objective-C非常冗长,所以你需要习惯它。
The MyVar init
case is not quite right; you generally need to do a [[MyVar alloc] init]
or a [MyVar new]
. And in many cases, there are init
variants that take arguments, like
MyVar init案例并不完全正确;你通常需要做[[MyVar alloc] init]或[MyVar new]。在许多情况下,有一些init变量带有参数,比如
MyVar v = [[MyVar alloc] initWithSomething: "foo" somethingElse: "bar"];
If you want something less verbose, where you can get more one liners in, you might want to look into MacRuby, which is a binding between Ruby and Objective-C that gives you access to all of the Cocoa APIs but with a much more compact, high level syntax (and you can go even more compact still with HotCocoa. Of course, there is a bit of a performance penalty using MacRuby, so if you're doing a high performance game it might not be ideal to write your drawing loop in RubyCocoa, but its fine for the vast majority of applications.
如果你想要的东西更简洁,在那里你可以得到更多的一个套,你可能要考虑的MacRuby,这是红宝石和Objective-C,让您可以访问所有的可可的API,但有一个更紧凑之间的绑定,高级语法(你可以使用HotCocoa更加紧凑。当然,使用MacRuby会有一点性能损失,所以如果你正在进行高性能游戏,那么编写绘图循环可能并不理想在RubyCocoa中,但它适用于绝大多数应用程序。