Variable type properties and copy

Source: Internet
Author: User

Code:

@import Foundation;/************************************************** * Man class **************************************************/@interfaceMan:nsobject//Potential risk: Use the property modifier copy to modify a property of a mutable type@property (copy, nonatomic) nsmutablestring *name;@end@implementationMans@end/************************************************** * Main function **************************************************/intMainintargcConst Char*argv[]) {@autoreleasepool {nsmutablestring*namemutablestring = [nsmutablestring stringwithstring:@"AAA"]; Mans*man =[[Man alloc] init]; //When a property is assigned a value by its set method, the actual type of the property value will be the immutable type, and its execution is similar to the following statement//man->_name = namemutablestring.copy;Man.name =namemutablestring; //the type of the property is no longer an immutable typeNSLog (@"[man.name IsKindOfClass:NSMutableString.class] =%@", [Man.name iskindofclass:nsmutablestring.class]  ?@"YES":@"NO"); NSLog (@"[man.name IsKindOfClass:NSString.class] =%@", [Man.name iskindofclass:nsstring.class]  ?@"YES":@"NO"); //The program may crash if you execute the following statement//[Man.name setstring:@ "BBB"];    }    return 0;}

Output:

[Man.name iskindofclass:nsmutablestring. class] = no[man.name iskindofclass:nsstring. class] = YES

Summarize:

Use the property modifier with caution copy to modify properties of a mutable type

Variable type properties and copy

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.