问题
I know that alloc and retain will increase the reference count of an object. Is there any other different method that actually increment the reference count? And when/how does dealloc is called?
回答1:
With these the retain count gets increased.
new, however it can be seen as alloc+init.
retain
copy creates new object with retain count=1
mutableCopy creates new object with retain count=1
dealloc is called automatically as soon as retain count reaches to 0.
回答2:
allocallocates an object with retain count 1.- Methods that start with
newalso return an object with retain count 1. retainincrements the count by 1.releaseandautorelease(at the end of the run loop) decrement it by 1.- Methods that start with the name of the class (without prefix) return an autoreleased object, meaning that it will be released at the end of the cycle, if you don't retain it yourself.
- Finally, methods that copy an object (usually start with
copy) also create a copy with retain count 1.
dealloc is called when the retain count of an object drops to 0.
PS. In case you didn't know about it yet, consider using Automatic Reference Counting (ARC).
来源:https://stackoverflow.com/questions/14043994/ios-memory-management-clarifications