【OC语法快览】四、基础内存管理

Basic Memory Management                                                       

   基础内存管理

If you're writing an application for Mac OS X, you have the option to enable garbage collection. In general, this means that you don't have to think about memory management until you get to more complex cases. 
假设你正在写执行在Mac OS X上的程序,你能够选择开启垃圾回收功能。除非你遇到更复杂的情况。这样你就不用考虑内存管理了。



However, you may not always be working with an environment that supports garbage collection. In that case, you need to know a few basic concepts. 

可是,你的工作环境不一定都支持垃圾回收。

这样的情况下。你须要知道一些基本内存管理的概念。

If you create an object using the manual alloc style, you need to releasethe object later. You should not manually release an autoreleased object because your application will crash if you do. 

假设使用人工alloc方式创建实例对象,稍后须要释放掉它。

不应该人工释放一个能够自己主动释放的对象,否则将导致程序奔溃。

Here are two examples:

这有两个样例: 

// string1 will be released automatically
NSString* string1 = [NSString string];

// must release this when done
NSString* string2 = [[NSString alloc] init];
[string2 release];



For this tutorial, you can assume that an automatic object will go away at the end of the current function. 
在这份材料中,你能够如果。一个自己主动释放的实例对象将会在当前函数结束后自己主动消失。

There's more to learn about memory management, but it will make more sense after we look at a few more concepts.
还有非常多须要学习的内存管理知识,可是在我们掌握很多其它概念之后,学习起来更有效果。






原文:learn_objective_C part 4

原文地址:https://www.cnblogs.com/blfshiye/p/5274554.html