Coder Social home page Coder Social logo

Objective-C Tips for Good about technotes HOT 2 OPEN

vvlavida avatar vvlavida commented on September 27, 2024
Objective-C Tips for Good

from technotes.

Comments (2)

vvLavida avatar vvLavida commented on September 27, 2024

方法命名

replaceObjectAtIndex:... withObject:...

运行[mArray replaceObjectAtIndex:1 withObject:@"Hello"]; 数组mArray索引1中的对象已经变成了字符串@"Hello". (这个方法只能援引那些有效的索引,原来索引中应当有对象,我们才可以用这个方法置换进新的对象)。

你可能已经发现了,Objective-C语言的方法名字好似句子填空。当你要使用一个方法,只要你填上你需要的值构成一个有意义的『句子』即可。

这种给方法命名的方法来源于Smalltalk(一种由Xerox公司开发的面向对象的系统),也是Objective-C语言的一大特色,它可以大大增强代码的描述性。当创建自己的方法时,你也应当尽量按照这种方法命名,不仅可以提高代码的可读性,而且使你的程序便于维护。

from technotes.

vvLavida avatar vvLavida commented on September 27, 2024

内存管理
你的程序并非你的设备上唯一运行的程序,而RAM又是珍贵的资源。当你的程序不再需要占用内存的时候,你需要把内存还给系统。即使设备上只运行了你的一个程序,拥挤的内存也会把你的程序挤到角落,设备的相应速度也会变得很慢。

当你的程序创建出一个对象,对象会占用内存,你要在对象不被使用后释放出内存空间。尽管并不是那么好确定一个对象是否不再被使用了。比如在程序执行过程中你的对象会被多个其它对象引用,当被其它对象应用的可能性还存在时就不能销毁这个对象(可能导致程序崩溃或出现不可预见的结果)。

引用计数 (reference counting)

为了处理那些不在被使用的对象,Cocoa给每个对象关联了一个计数器,被称为『引用计数器』。要使对象的引用计数器增加数值,发送一条「retain message」;减少引用计数则发送「release message」。

Cocoa还提供了一个『autorelease pool』的机制,可以发送延迟的释放信息。使用该机制发送自动释放信息需要将对象注册到autorelease pool中。

from technotes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.