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

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

  • Author: 柳大·Poechant(钟超)
  • Email: zhongchao.ustc#gmail.com (# -> @)
  • Blog: Blog.CSDN.net/Poechant
  • Date: May 6th, 2012

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.csdn.net/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.

-

转载请注明来自柳大的CSDN博客:Blog.CSDN.net/Poechant

-

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

钟超

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

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

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

打赏作者

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

抵扣说明:

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

余额充值