独立开发者的自白:Objective-C最糟糕的13件事
原创转自: http://www.csdn.net/article/2013-12-17/2817832-top-13-worst-things-about-objective-c
本文作者Anton Zherdev他是一位拥有多年开发经验的独立游戏开发者,从专业开发者的角度深入分析。Objective-C,将其与C、Java与其他语言相比,口译Objective-C根据Objective-C的13最糟糕的事情是指出Objective-C与所有开发人员分享缺点。以下为文章全文:
1. 笨拙的语法
在Objective-C在中,需要编写大量代码来声明类或属性。比较以下内容Objective-C和Scala代码片段的效果是不言而喻的。
[js] view plain copy
- @ interface Foo : NSObject
- @property (readonly) int bar;
- - (instancetype)initWithBar:( int )bar;
- + (instancetype)fooWithBar:( int )bar;
- @end
- @implementation Foo
- - (instancetype)initWithBar:( int )bar {
- self = [ super init];
- if (self) {
- _bar = bar;
- }
- return self;
- }
- + (instancetype)fooWithBar:( int )bar {
- return [[self alloc] initWithBar:bar];
- }
- @end
[js] view plain copy
- class Foo {
- private int bar;
- public Foo( int bar) {
- this .bar = bar;
- }
- public int getBar() {
- return bar;
- }
- }
[js] view plain copy
- class Foo(bar : Int)
2. 方括号
前缀括号是一件非常残酷的事情,尽管Objective-C有IDE自动解决此问题会大大降低代码的可读性。
3. 内存管理
与带有垃圾回收器的编程语言相比,Objective-C内存更容易泄漏。尽管垃圾回收器可能会导致程序不定期收集,但也可以通过设置它们来避免它们。留Objective-C在,虽然已经有ARC要解决这个问题,开发人员可以很好地利用它来解决一些问题 弱引用 但是,仍然存在无法解决参考周期的情况。
例如,如果你是 block 中使用self并将其发送到另一个存储block类会导致内存泄漏,并且很难找到。如果您想保存在类字段中block有必要copy来这里,否则,当block调用时,程序也会崩溃。
4. 动态和不安全的类型系统
Objective-C有一个很奇怪的 类型系统 。可以在视图中声明的任何消息都可以发送到id类对象,但无法避免id。
[js] view plain copy
-
@ interface Foo : NSObject
-
- ( void )foo;
-
@end
-
@implementation Foo
-
- ( void )foo{}
-
@end
-
@ interface Bar : NSObject
-
- ( void )bar;
-
@end
-
@implementation Bar
-
- ( void )bar {
-
[(id)self foo]; //OK
-
[(id)self bar]; //OK, but runtime error
-
[(id)self qux]; //Compiler error
-
Foo* foo = self; //OK
-
}
-
@end
5. 不支持泛型
在Objective-C无法查看容器类的所有权,并且编译器无法执行检查。在这方面,Java显然要好得多。
[js] view plain copy
- NSArray* array = [foo array];
- id item = [array objectAtIndex:0]; //item is anything
[js] view plain copy
- List array = foo.getArray();
- A item = array.get(0);
6. 缺少核心库集
在Objective-C在核心库中,缺乏分类设置、字典(maps)、链表、队列等实用集。没有他们,正在进行中 红黑树 类别和词典的开发和管理可能需要大量时间。
另一个问题是缺少几个非常好的功能,特别是缺乏函数式编程能力。Objective还有一个非常好的功能,那就是开发人员可以使用分类轻松扩展核心集。
7. 缺少枚举
尽管Objective-C包含有C枚举,但它只是一组变量,开发人员必须编写代码来实现类似于Java的枚举,例如链接属性。
[js] view plain copy
- enum Foo {
- Foo1(1),
- Foo2(2),
- Foo2(3);
- final int bar;
- Foo( int bar) {
- this .bar = bar;
- }
- }
8. 可怕的block语法
block是Objective-C一个非常强大的功能,但我不知道如何声明它block类型的变量或函数参数。您可以通过查看下面的代码来查看。
[js] view plain copy
- //Declare variable foo
- void (^foo)(id obj, NSUInteger idx, BOOL *stop);
9. 缺少运算符重载
如果不需要算子重载,那不可避免地有些不合适,因为定义向量数学算子是很正常的事情,使代码更具可读性。
[js] view plain copy
- [[[a add:b] sub:[c mul:f]] div:f];
- (a + b - c*f)/f;
10. 匿名类不足
定义协议或接口并不像想象的那么简单。若要轻松实现它,必须首先实现一个完整的类。
[js] view plain copy
-
@protocol I
-
- ( void )f;
-
@end
-
@ interface Foo : NSObject
-
- ( void )foo;
-
- ( void )qux;
-
@end
-
@implementation Foo
-
- ( void )foo {
-
id i = [[Baz alloc] initWithFoo:self];
-
}
-
@end
-
@ interface Baz : NSObject
-
- (instancetype)initWithFoo:(Foo*)foo;
-
@end
-
@implementation B {
-
Foo* _foo;
-
}
-
- (instancetype)initWithFoo:(Foo*)foo {
-
self = [ super init];
-
if (self) {
-
_foo = foo;
-
}
-
return self;
-
}
-
- ( void )f {
-
[_foo bar];
-
}
-
@end
[js] view plain copy
-
interface I {
-
void f();
-
}
-
class Foo {
-
void foo() {
-
I i = new I() {
-
void f() {
-
bar();
-
}
-
};
-
}
-
void bar() {
-
}
-
}
11. 错误的构造函数
使用构造函数创建新对象是很常见的,但是在Objective-C要创建对象,还需要调用两个函数。当然,开发人员可以编写方法来直接避免此问题。
[js] view plain copy
- @ interface Foo : NSObject
- - (instancetype)initWithBar:( int )bar;
- + (instancetype)newWithBar:( int )bar;
- @end
- @implementation Foo {
- int * _bar;
- }
- - (instancetype)initWithBar:( int )bar {
- self = [ super init];
- if (self) {
- _bar = bar;
- }
- return self;
- }
- + (instancetype)newWithBar:( int )bar {
- return [[self alloc] initWithI:bar];
- }
- @end
12. 不透明的数字包装器
在Objective-C除了原始代码外,还需要添加一个 NSNumber类 。
[js] view plain copy
- int a = 1;
- int b = 2;
- int c = a + b;
- NSNumber* aWrap = @(a);
- NSNumber* bWrap = @(b);
- NSNumber* cWrap = @([aWrap intValue] + [bWrap intValue]);
13. 缺乏包裹管理系统
在Objective-C在中,开发人员必须使用前缀来避免类名重叠。此外,有必要在头文件中声明所需的类,以防止头文件冲突和编译失败。
(编译/唐小引 责编/张宁)
文章来源: Anton Zherdev个人博客
本文为CSDN汇编整理,未经许可不得转载。如需转载,请联系market#csdn.net(#换成@)
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除