IPhone iboutlet problems: Copy, retain, and assign Problems

Source: Internet
Author: User

 

(Example A)

FooController.h:

@interface FooController : UIViewController {
    UILabel *fooLabel;
}

@property (nonatomic, retain) IBOutlet UILabel *fooLabel;

@end

FooController.m:

@implementation FooController

@synthesize fooLabel;

@end

But this works also fine (notice: no property and no synthesize):

(Example B)

FooController.h:

@interface FooController : UIViewController {
    IBOutlet UILabel *fooLabel;
}

@end

FooController.m:

@implementation FooController

@end
The difference between the two is not very clear in many Chinese documents. share with everybody is found in stackoverflow.

On Mac OS X, iboutlets are connected like this:

  1. Look for a method called set <outletname>:. If it exists call it.
  2. If no method exists, look for an instance variable named <outletname>, set itWithout retaining.

On iPhone OS, iboutlets are connected like this:

  1. Call [object setvalue: outletvalue forkey: @ "<outletname>"]

The behavior of set value for key is to do something like this:

  1. Look for a method called set <outletname>:. If it exists call it.
  2. If no method exists, look for an instance variable named <outletname>, set it andRetainIt.

If you use a property, you'll fall into"Look for a method called set <outletname> :..."Case on both platforms.

If you just use an instance variable, then you'll have different retain/release behavior on Mac OS x vs iPhone OS.

There's nothing wrong with using an instance variable, you just need to deal with this difference in behavior as you switch between platforms.

Here's a link to full documentation on just this topic.

Http://developer.apple.com/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW6

 

That is to say, Mac OS and iPhone OS handle iboutlets differently by default. No retain on MAC; retain on iPhone.

 

 

 

On Mac OS x, iboutlets are not retained by defaults. This is the opposite of the behavior on iPhone OS: on iPhone OS,

If you don't declare a property it is retained and you must release this property indeallocMethod.

Additionally, the 64-bit runtime can synthesize instance variables using property declarations. That means that someday the instance variables (withIBOutlet) May be omitted.

For these reasons it is more homogeneous and compatible to create always a property and useIBOutletOnly in the property. Unfortunately, it is also more verbose.

In your first example you always have to release the outlet indeallocMethod. In your second example you must release the outlet only with iPhone OS.

 

If the retain property is declared, it must be release in dealloc.

To ensure uniformity and compatibility, we recommend that you declare iboutlet using the property method.

In the first example, you must use release on both the Mac and iPhone, and in the second example, you must only use release on the iPhone.

 

The following is a question about the alias of the Property:

What happens if the variable name is different from property name? Does it matter if different?
The "outletname" name above is defined as what ever is next to the "iboutlet" keyword in source code. if iboutlet is in the @ property, it doesn't matter what the instance variable is named since a setter will be found. if for some reason a setter didn't exist, there wocould be an exception raised when connecting the outlet. if the iboutlet keyword is on the instance variable, and a setter exists with a name that doesn't match, the setter won't get called.

 

The above "outletname" is defined as anything in the source code that is closely related to the iboutlet keyword. If iboutlet is in @ property, no matter what the instance variable name is, it doesn't matter because the setter has been created.

If the setter does not exist for some reason, an exception is reported on the connection to the outlet interface. If the iboutlet is on the instance variable and there is a setter that does not match it, the setter will not be called.

In other words, the setter that matches the instance variable will be called.

 

 

Finally, let's talk about the difference between @ property copy, assign, and retain. The explanation on the internet is confusing..

 

 

Java code 
  1. * Assign: simple value assignment without changing the index count (reference counting ).
  2. * Copy: Create an object with an index count of 1 and release the old object.
  3. * Retain: Release the old object, assign the value of the old object to the input object, and increase the index count of the input object to 1.


It can also be understood as follows:
Java code 
  1. * Use assign: for basic data types (nsinteger) and C data types (INT, float, double, Char, etc)
  2. * Use copy: For nsstring
  3. * Use retain: for other nsobject and its subclass

Assign indicates to pass the reference directly to the property value
Retain indicates that when an object is referenced to a property value, the property value will add one to the reference count, that is, the property will retain an additional reference.
For example.
Nsobject A = [[nsobject alloc] init];
Classb. oneproperty =;
[A release];
If assign is used, the oneproperty of classb cannot be used after a is release.
If it is retian, after a is release, the oneproperty of classb can continue to be used.


Assign is mainly for int, long, And bool raw data types. You can try it. When you use the original data type retain, an error will be reported during compilation.

 

 

Assign is suspected to be a many-to-one relationship. For example, a is assigned to B, C, and D. Then A, B, C, and D direct point to a's memory. The structure is as follows: A-> M, B-> M, C-> M, D-> M.

In release, the m space is directly released.

The retain suspect is also a many-to-one relationship, but adds the reference count on the basis of the assign. The structure is as follows: A-> M, B-> M, C-> M, D-> M.

In release, instead of directly releasing M, check count, count> 0, count --; otherwise, the space of M is released.

Correct the error.


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.