私有属性

来源:互联网 发布:java远程调用接口 编辑:程序博客网 时间:2024/05/21 17:39
如果类的属性不直接通过点语法赋值可以写成私有的
- (IBAction)testPropertyButtonClick:(id)sender {    TestObject *testObj = [[TestObject alloc] init];    testObj.name = @"name";    NSLog(@"testObj.name: %@", testObj.name);    [testObj setPrivateNameWithString:@"private name"];}//  TestObject.h#import <Foundation/Foundation.h>@interface TestObject : NSObject{    NSString *_name;}@property (copy, nonatomic) NSString *name;- (void)setPrivateNameWithString:(NSString *)str;@end//  TestObject.m#import "TestObject.h"@interface TestObject (){    NSString *_privateName;}@property (copy, nonatomic) NSString *privateName;@end@implementation TestObject@synthesize name = _name;@synthesize privateName = _privateName;- (void)setPrivateNameWithString:(NSString *)str{    self.privateName = str;    NSLog(@"self.privateName: %@", self.privateName);}- (void)dealloc{    self.name = nil;    self.privateName = nil;    [super dealloc];}@end

原创粉丝点击