Objective—C 学习1

来源:互联网 发布:全国网络教育统考网 编辑:程序博客网 时间:2024/05/29 14:37

公司要开发IOS端,要求我们快速学习IOS开发的相关知识。这当然没有问题,我很乐意,只可惜我Android还没有写溜呢。管不了这么多了,上!

今天写了一个IOS的加法程序,各种蛋疼,界面设计一团糟,各种关键词不知道,各种方法不知道,各种属性也不知道。真是真是一件相当蛋疼的事情,哥怒了,我要弄死O-C!

现在就开始把O-C中的东西,上网搜集总结一下吧:


一 NSLog的格式:

%@    对象

%d, %i 整形

%f  浮点型/double型

%u 无符号整形

%x ,%X 二进制整形

%o 八进制整形

%zu   ????????????

%p  指针

%e   浮点/双字(科学计算)

%g 浮点/双字

%s C字符串

%.*s  Pascal字符串

%c  字符

%C unichar  ????????????

%lld 64位长整形

%llu 无符号正整形

%Lf  64位双字



在程序中经常见到@xxxx 什么的,这些都是神马玩意儿??!!搜集整理一下吧。


@

@是 OC在C语言上增加的新特性,表示这个字符串英爱作为Cococa的NSString处理。


@interface

@implementation

@public

@protected

@private


@property

@synthesize

@dynamic

参考:http://blog.csdn.net/haishu_zheng/article/details/12873151

@property (参数1,参数2) 类型 名字;

Objective-C语言关键词,@property与@synthesize配对使用。xcode4.5以及以后的版本,@synthesize可以省略


example:

@property NSString * name;
@property NSString * sex;
@property NSInteger *age;
表示声明了三个属性: name,sex,age, 默认生成3个对应的 setter 和 getter 方法
在. m 文件中:
@implementation Person
@synthesize name = _name;
@synthesize sex = _sex;
@synthesize age = _age;
@end



@end

@"string"


@autoreleasepool  &@@autorelease 

参考:http://blog.sina.com.cn/s/blog_47615df70101g02j.html

自动释放池  自动释放。autorelease只会让计数减一,并没有销毁对象,销毁对象由@autoreleasepool完成。

@autoreleasepool可以嵌套使用,以栈的形式保存。


对象alloc后,必须释放,可以release,也可以使用自动释放池和自动释放。




NOTICE:谁申请,谁释放。



@class

参考:http://www.cnblogs.com/martin1009/archive/2012/06/24/2560218.html

只是声明,具体内容需等到用到的时候才会检查被引用的类。



@protocal

参考:http://www.cnblogs.com/hxxy2003/archive/2011/10/24/2222838.html

@protocol是Objective-C中的接口定义方式,也就是说在一个类中通过@protocol定义接口,然后在另一个类中去实现这个接口,这也叫“代理”模式, 这种模式在ios开发中经常是会用到的。


@optional

@required

@selector()

参考:http://fstoke.me/blog/?p=2647

            http://blog.sina.com.cn/s/blog_735065f90100yopd.html

指针还是才CallBack???


@encode()

参考:http://blog.csdn.net/yhawaii/article/details/8222973





还有这些东西(虽然我还不知道这些都是干啥滴):

retain

weak

strong

atomic

nonatomic

readwrite

readonly

copy





看一下这个:

nonatomic vs atomic

“atomic” is the default if it is not specified. We always use “nonatomic”. You can use this attribute to specify that accessor methods are not atomic. (There is no keyword to denote atomic.) Properties are atomic by default so that synthesized accessors provide robust access to properties in a multithreaded environment—that is, the value returned from the getter or set via the setter is always fully retrieved or set regardless of what other threads are executing concurrently.

retain vs. copy vs. assign

  • “assign” is the default. In the setter that is created by @synthesize, the value will simply be assigned to the attribute. My understanding is that “assign” should be used for non-pointer attributes.
  • “retain” is needed when the attribute is a pointer to an object. The setter generated by @synthesize will retain (aka add a retain count) the object. You will need to release the object when you are finished with it.
  • “copy” is needed when the object is mutable. Use this if you need the value of the object as it is at this moment, and you don’t want that value to reflect any changes made by other owners of the object. You will need to release the object when you are finished with it because you are retaining the copy.

Is that all? Nope… apparently, we can also specify our property’s access rights.

readwrite vs readonly

“readwrite” is the default. When you @synthesize, both a getter and a setter will be created for you. If you use “readonly”, no setter will be created. Use it for a value you don’t want to ever change after the instantiation of the object.  So if for any reason, you need a property to be readonly, it will look something like this:-








0 0