I implement my private properties like this.
MyClass.m
@interface MyClass ()
@property (nonatomic, retain) NSArray *someArray;
@end
@implementation MyClass
@synthesize someArray;
...
That's all you need.
c c++
0.
NSString *testString=nil;
NSInteger length=[testString length];
NSLog(@"%i",length);
2013-09-03 07:05:20.058 Recipes[456:11603] 0
(lldb)
1.1&2 的结果是什么?
数组 array +4-2 返回的是哪个数?看着这个array是头指针。
符号重定向是哪个阶段报的错
3.屏幕分辨率
4.用过objective-c的反射机制吗?
5.所有的类都继承自,NSObject ,这种单根继承的方式有什么好处吗?
6.用interfaceBuilder 构建视图 和用代码生成视图,各有什么优缺点?
7.objective 不支持私有属性,可以通过什么方法来实现。
答: send message to nil?
0.官方的解释
https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html
It’s always a good idea to initialize scalar variables at the time you declare them, otherwise their initial values will contain garbage from the previous stack contents:
BOOL success = NO; |
int magicNumber = 42; |
This isn’t necessary for object pointers, because the compiler will automatically set the variable to nil
if you don’t specify any other initial value:
XYZPerson *somePerson; |
// somePerson is automatically set to nil |
A nil
value is the safest way to initialize an object pointer if you don’t have another value to use, because it’s perfectly acceptable in Objective-C to send a message to nil
. If you do send a message to nil
, obviously nothing happens.
Note: If you expect a return value from a message sent to nil
, the return value will be nil
for object return types, 0
for numeric types, and NO
for BOOL
types. Returned structures have all members initialized to zero.
If you need to check to make sure an object is not nil
(that a variable points to an object in memory), you can either use the standard C inequality operator:
if (somePerson != nil) { |
// somePerson points to an object |
} |
or simply supply the variable:
if (somePerson) { |
// somePerson points to an object |
} |
If the somePerson
variable is nil
, its logical value is 0
(false). If it has an address, it’s not zero, so evaluates as true.
Similarly, if you need to check for a nil
variable, you can either use the equality operator:
if (somePerson == nil) { |
// somePerson does not point to an object |
} |
or just use the C logical negation operator:
if (!somePerson) { |
// somePerson does not point to an object |
} |
这里解释了一下原因
http://www.cnblogs.com/Proteas/archive/2012/12/17/2822521.html
我们知道在 Objective-C 中给 nil 发送消息程序不会崩溃,
Objective-C 是以 C 语言为基础的,
PC 上,在 C 语言中对空指针进行操作,
程序会由于越界访问而出现保护错进而崩溃,
但是 Objective-C 中为什么不会崩溃呢?
原因需要从源代码中寻找,
下面是 objc_msgSend 的 arm 版汇编代码片段:
在 arm 的函数调用过程中,
一般用 r0-r4 传递参数,
用 r0 传递返回值。
对应 objc_msgSend,第一个参数为 self,返回值也是 self,都放在 r0(a1)中。
/********************************************************************
* idobjc_msgSend(idself, SELop, ...)
* On entry: a1 is the message receiver,
* a2 is the selector
********************************************************************/
ENTRY objc_msgSend
# check whether receiver is nil
teq a1, #0
moveq a2, #0
bxeq lr
teq 指令说明:
TEQ
R
n
, Operand2
The TEQ instruction performs a bitwise Exclusive OR operation on the value in Rn and the value of Operand2.
测试 self 是否为空。
moveq 指令说明:
如果self为空,则将 selector 也设置为空。
bx 指令说明:
在 arm 中 bx lr 用来返回到调用子程序的地方(即:返回到调用者),此处是:如果 self 为空,就返回到调用 objc_msgSend 的地方继续执行。
总之:
如果传递给 objc_msgSend 的 self 参数是 nil,该函数不会执行有意义的操作,直接返回。
1.&
http://en.wikipedia.org/wiki/Ampersand#Programming_languages
In the 20th century, following the development of formal logic, the ampersand became a commonly used logical notation for thebinary operator or sentential connective AND. This usage was adopted in computing.
Many languages with syntax derived from C, including C++, Perl,[18] and more differentiate between:
In C, C++, and Go, a prefix "&" is a unary operator denoting the address in memory of the argument, e.g. &x, &func, &a[3]
.
2.
http://zh.wikipedia.org/zh/%E5%B7%A8%E9%9B%86
宏(Macro),是一種批次批量處理的稱謂。
计算机科学裡的宏是一种抽象(Abstraction),它根据一系列预定义的规则替换一定的文本模式。解释器或编译器在遇到宏时会自动进行这一模式替换。对于编译语言,宏展开在编译时发生,进行宏展开的工具常被称为宏展开器。宏这一术语也常常被用于许多类似的环境中,它们是源自宏展开的概念,这包括键盘宏和宏语言。绝大多数情况下,“宏”这个词的使用暗示着将小命令或动作转化为一系列指令。
宏的用途在于自动化频繁使用的序列或者是获得一种更强大的抽象能力——但这常常是一回事。
计算机语言如C语言或汇编语言有简单的宏系统,由编译器或汇编器的预处理器实现。C语言的宏预处理器的工作只是简单的文本搜索和替换,使用附加的文本处理语言如M4,C 程序员可以获得更精巧的宏。
4.关于反射有两种解读
1.
程序中可能会出现大量的if-else或者switch-case来选择一系列类中的某个类进行操作,利用反射(Reflection)就可以避免这些重复判断工作。
反射在Java中封装了类来实现,在Objective-C里面要自己来实现,好在不是很难。
我们的目的是通过传入一个类名字符串,把字符串类名动态转化成类,并对类进行实例化和调用方法等操作。
OC的NSRuntime中提供了将Class name转化为Class的方法"NSClassFromString()"
2.另一种解读
http://hamguy.info/?p=342
今天查阅Apple的参考库才发现,原来Objective-C中其实也有类似Java中的反射机制,即根据类名获取对象的属性列表。
首先引入Objective-C运行时头文件
#import <objc/runtime.h>
然后使用函数class_copyPropertyList和protocol_copyPropertyList来获得类(包括范畴类)或者协议类中的属性列表,遍历该列表,最后通过函数得到属性名字,或者通过函数取得包括属性名字及@encode编码等属性的详细信息。
-(void) getPropertyList{
unsigned int nCount = 0;
objc_objectptr_t *popertylist = class_copyPropertyList([Car class],&nCount);
for (int i = 0; i < nCount; i++) {
objc_objectptr_t property = popertylist[i];
NSLog(@”class Name is = %s && attr = %s”,property_getName(property),property_getAttributes(property));
}
}
运行后,控制台输出为:
class Name is = carName && attr = T@”NSString”,&,N,VcarName
class Name is = carNumber && attr = T@”NSString”,N,VcarNumber
class Name is = bCarChecked && attr = Tc,N,VbCarChecked
class Name is = carEngine && attr = T@”NSString”,R,N,VcarEngine
class Name is = carTire && attr = T@”NSString”,&,N,VcarTire
具体表示信息可参考属性类型编码。
Tags: IOS
Posted in IOS at 2012/06/26
3.这里写的挺全的
http://zephyrleavesblog.sinaapp.com/?p=676
5.所有的类都继承自,NSObject ,这种单根继承的方式有什么好处吗?
The singly rooted hierarchy, in object-oriented programming, is a characteristic of most (but not all) OOP-based programming languages. In most such languages, in fact, all classes inherit directly or indirectly from a single root, usually with a name similar to Object; all classes then form a common inheritance hierarchy.
This idea was introduced first by Smalltalk, the first OOP language, and was since used in most other ones (notably Java and C#).
A notable exception is C++, where (mainly for compatibility with C and efficiency) there is no single object hierarchy. This feature is especially useful for container libraries - they only need to allow putting an Object in a container to allow objects of any class to be put in the container. Containers in C++ have been implemented with multiple inheritance,[1] and with help of template-basedgeneric programming by Bjarne Stroustrup.[2][3] Other object-oriented languages without a singly rooted hierarchy includeObjective-C and PHP.
上面的比较偏科普,浅显,又搜到一篇
6.用interfaceBuilder 构建视图 和用代码生成视图,各有什么优缺点?
http://v2ex.com/t/36961
这位博主道出了我的心声
直呼上也搜到了一个问答
http://www.zhihu.com/question/20116289
可以了解一下,竟然是07年写的
http://speirs.org/blog/2007/12/5/what-are-xib-files.html
7.objective-c 不支持私有属性,可以通过什么方法来实现。
中文真的弱爆了,没搜到。英文就搜到了,哇咔咔,这个我还真没想到
http://stackoverflow.com/questions/5643130/private-property-in-objective-c
36 |
I implement my private properties like this. MyClass.m
That's all you need. |