Objective-C Primer(2)Private Methods and Class Properties

本文深入探讨了Objective-C中私有方法的隐藏与使用,以及类属性(properties)的多种实现方式,包括retain、assign、copy等,并解释了nonatomic标识符的含义及其对线程安全的影响。

Objective-C Primer(2)Private Methods and Class Properties

1 What the relationship between C and Objective-C?

If you want, you can just program in C language style and syntax, and there will be no error or warning if you never make mistakes of C programming. Therefore, you must say: Getcha!

Yes! Objective-C is a super set of C language, just like C++ to C.

2 No private methods declaration in .h file?

Yes, you are right. But think it twice. Is it more reasonable to hide or ignore private methods declaration in header file? Every invoker just appears in member methods of .m source code files, so it’s unnecessary to give any clue for private methods in header file.

Do you know how to hide private methods declaration in C++? (Give you a hint: pimpl idiom.)

Now I will show how to do that. The .h is still the one I introduce in Objective-C Primer (1) Get started! But how about .m file?


//
//  TestClass.m
//  ObjectiveCTest
//
//  Created by 超 钟 on 12-5-6.
//  Copyright (c) 2012年 http://blog.youkuaiyun.com/poechant. All rights reserved.
//

#import "TestClass.h"

@implementation TestClass
@synthesize foo;

-(void)privateMethod
{
    NSLog(@"I'm a priavte method, named 1");
}

-(void)someMethod
{
    NSLog(@"some method got called");
    [self privateMethod];
}

But notice that there will be a warning if you invoke a private method below the invoker method. Just like the following.

Resize icon

3 Class properties

As we know at present, in Objective-C, a class consists of an interface which is visible for external users and an implementation for class behavior details.

What about properties?

There are some many identifiers for properties, which will be messed up.

  • retain will increase the reference counter, and of course set the value of the pointer.
  • assign does not increate the reference counter, but also set the value of the pointer.
  • copy will create a new object, which is the same as the source object. But this new object has its own new reference counter.

You should notice that any two of these three identifier could not be used together.

  • nonatomic means this object is not thread safe. So you must guess it. Yes! If you do not use nonatomicand just use the default, the object will be thread safe and there will be scoped lock used internally.

-

转载请注明来自柳大的优快云博客:Blog.youkuaiyun.com/Poechant

-

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钟超

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值