zoukankan      html  css  js  c++  java
  • The iPhone Developer's Cookbook(2)

    The iPhone Developer's Cookbook读书笔记,我会慢慢翻译的。

    Property
    OC automatically builds methods when you @synthesize properties.Notice the capitalization of the second word in the set method. By convention, OC expects setters to use a method named setInstance: where the first letter of the instance variable name is capitalized.

    Property Attributes
    The default behavior for properties is assign.

    Setting the property's attribute to retain does two things.
    First, it retains the passed object upon assignment.
    Second, it releases the previous value before a new assignment is made.
    To create a retained property, add the attribute between parentheses in the declaration:
    @property (retain) NSString *make;

    A third attribute called copy sends a copy message to the passed object, retains it, and releases any previous value.
    @property (copy) NSString *make;

    When you develop in a multithreaded environment, you want to use atomic methods. Xcode synthesizes atomic methods to automatically lock objects before they are accessed or modified and unlock them after. There is no atomic keyword.
    All methods are synthesized atomically by default.
    You can, however, state the opposite, allowing OC to create accessors that are nonatomic.
    @property (nonatomic, retain) NSString *make;
        Marking your properties nonatomic does speed up access, but you might run into problems should two competing threads attempt to modify the same property at once.Atomic properties, with their lock/unlock behavior, ensure that an object update completes from start to finish before that property is released to another read or change.

    Simple Memory Management.
    Memory management comes down to two simple rules.
    At creation, every object has a retain count of one.
    At release, every object has a retain count of zero.
    Complicating matters is OC's autorelease pool

    Creating Objects
        Any time you create an object using the alloc/init pattern, you build it with a retain count of one. It doesn't matter which class you use or what object you build, alloc/init produces  a +1 count.
        For locally scoped variables, if you do not release the object before the end of a method, the object leaks. Your reference to that memory goes away, but the memory itself remains allocated. The retain count remains at +1.

    The proper way to use an alloc/init pattern is to create, use, and then release. Releasing brings the retain count down to 0.
    Autorelease objects do not require an explicit release statement for locally scoped variables. Sending the autorelease message to an object marks it for autorelease. When the autorelease pool drains at the end of each event loop, it sends release to all the objects it owns.
    NSArray *array = [[[NSArray alloc] init] autorelease];
    By convention, all class object-creation methods return an autoreleased object.

    Creating Autoreleased Objects
    As a rule, whenever you ask another method to create an object, it's good programming practice to return that object autoreleased.

    Note
    Avoid assigning properties to themselves, for example, myCar.colors = myCar.colors.The release-then-retain behavior of properties may cause the object to deallocate before it can be reassigned and re-retained.

    You can send retain to autorelease objects just like any other object. Retaining objects set to autorelease allows them to persist beyond a single method. Once retained, an autorelease object is just as subject to memory leaks as one that you created using alloc/init.
    By creating a reference, you can both use a retained object through its lifetime and be able to release it when you're done. Set references via an instance variable (preferred) or a static variable defined within your class implementation. If you want to keep things simple and reliable, use retained properties built from those instance variables.

    As a general rule of thumb, Apple recommends you avoid using properties in your functions. Instead, use instance variables directly.

    As a rule of thumb, if you build an object using any method whose name includes alloc, new, create, or copy, you maintain responsibility for releasing the object. Unlike class convenience methods, methods that include these words generally do not return autoreleased objects.
    Sending a copy message to an object, for example, duplicates it. Copy returns an object with a retain count of +1 and no assignment to the autorelease pool. Use copy when you want to duplicate and make changes to an object while preserving the original. Note that for the most part, OC produces shallow copies of collections like arrays and dictionaries. It copies the structure of the collection, and maintains the addresses for each pointer, but does not preform a deep copy of the items stored within.

    When using plain (nonproperty) instance variables or assign style properties, send release at deallocation time.

    The UIApplication and UIDevice classes let you access information about the currently running application and the device hardware it is running on. They do so by offering singletons, that is , a sole instance of a class in the current process.

  • 相关阅读:
    java 取汉字首字母
    详解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)
    详解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)(上)
    IntelliJ IDEA maven项目new里没有package
    IntelliJ IDEA上操作GitHub
    IntelliJ IDEA部署tomcat时Edit Configuration Deployment无artifact选项
    Java开发需掌握的常用Linux命令(持续更新)
    Spring JDBC 示例
    java 获取日期的几天前,几个月前和几年前
    Anaconda3(0)环境基本使用
  • 原文地址:https://www.cnblogs.com/leelike/p/2250470.html
Copyright © 2011-2022 走看看