新的篇章5

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 的结果是什么?

2.宏定义是在哪个阶段起的做用,编译 还是链接 还是运行时?为什么?

数组  array +4-2  返回的是哪个数?看着这个array是头指针。

符号重定向是哪个阶段报的错

3.屏幕分辨率



4.用过objective-c的反射机制吗?

5.所有的类都继承自,NSObject ,这种单根继承的方式有什么好处吗?

6.用interfaceBuilder 构建视图 和用代码生成视图,各有什么优缺点?

7.objective 不支持私有属性,可以通过什么方法来实现。




答: send message to nil?

0.官方的解释

Programming with Objective-C


https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html

Working with nil

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

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 BOOLtypes. 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 中给 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 RnOperand2 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 

Programming languages[edit source | editbeta]

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 CC++, 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 程序员可以获得更精巧的宏。



3.ios 设备分辨率


http://www.zhihu.com/question/20583941

杨镇波「剪贴箱」已上线 App Store

iOS 设备现有的分辨率如下:
iPhone/iPod Touch
普通屏 320像素 x 480像素 iPhone 1、3G、3GS,iPod Touch 1、2、3
3:2 Retina 屏 640像素 x 960像素 iPhone 4、4S,iPod Touch 4
16:9 Retina 屏 640像素 x 1136像素 iPhone 5,iPod Touch 5

iPad
普通屏 768像素 x 1024像素 iPad 1, iPad2,iPad mini
Retina屏 1536像素 x 2048像素 New iPad,iPad 4

问一:只是分辨率不一样的情况,如一二或者四五相比,开发时提供两种分辨率就可以了,应用自动识别?
:在 iPhone 5 跟 iPod Touch 5 没出之前,对于开发者来说,一二就是分辨率不同的区别而已,因为他们都是3.5寸的屏幕。并且,你细心的话会注意到,一二他们的分辨率是成倍的关系。
这给 iOS 的设计和开发都提供了方便,界面设计只需要设计一套,然后进行等比例放大缩小即可。

为了方便开发人员开发,iOS 中统一使用点(Point)对界面元素的大小进行描述。
所以对于一跟二,只需把屏幕大小都当成  320 x 480点 进行开发即可,点跟像素的换算关系如下:
普屏 1点 = 1像素 
Retina屏 1点 = 2像素 
iPad 上的开发也是这样,只需根据  768 x 1136点 进行开发即可。

而在 iPhone 5 跟 iPod Touch 5 的机器上,对于开发者来说,它的界面大小就是  320 x 568点,你只需根据这个大小去调整你的界面(如果需要调整的话)。
比如:我有一个按钮,要放在屏幕中间的位置,那么在界面大小为 320 x 480点 的设备上,只需设置它的 center 为 (320/2, 480/2) 位置即可,在 3:2 Retina 屏上系统这个按钮也可以正确的显示在中间;
而在屏幕大小为 320 x 568点 的设备上,你就设置它的 center 为 (320/2, 568/2)。
直接通过  [[UIScreen mainScreen] bounds] 即可获取到当前屏幕的基础分辨率,所以不用 hard code。

问二:只是尺寸不一样的情况,如四六相比,开发时完全没有影响?
:开发时只需要关注分辨率即可,iPad 2 跟 iPad mini 屏幕尺寸不同,但是分辨率一致,所以完全没有影响,在 iPad 2 上可正常跑的 app,在 iPad mini 上也可正常跑。

问三:分辨率以及尺寸都不一样的情况,如二三相比,又如何?
:在 问一 中已经回答了



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_copyPropertyListprotocol_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


具体表示信息可参考属性类型编码

声明:未作说明,则本文HamGuy原创,仅代表个人观点,鄙人才疏学浅,难免有纰漏及不足之处,还望不吝赐教,及时指出,谢谢!
版权所有 © 欢迎转载,当然也欢迎在转载时以链接形式注明作者和原始出处!

Tags: IOS

Posted in IOS at 2012/06/26

3.这里写的挺全的

http://zephyrleavesblog.sinaapp.com/?p=676





5.所有的类都继承自,NSObject ,这种单根继承的方式有什么好处吗?

Singly rooted hierarchy

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.

上面的比较偏科普,浅显,又搜到一篇

Singly Rooted Inheritance Hierarchy

A controversial property required for  ObjectOriented languages, according to Alan Kay (see  DefinitionsForOoHeInventedTheTermHeDidntInventTheTerm). Part of the controversy is out of simple disagreement, but it is fueled by speculation that he was more or less just being curmudgeonly by insisting on this aspect of his definition.

Definition: All classes must descend from (be subclasses of) other classes, except the root class which must descend form no one. If you follow the inheritance relation at the end all of them must end in the root class, often called Object.

Since there are OO languages that are not singly rooted, this is patently false.

Even languages that attempt to be singly rooted sometimes fail. For instance, Objective-C is single rooted. However, in order to fix a class hierarchy design flaw, the entire class library was redesigned and re-rooted, but for the sake of backward compatibility, there was a period of time during which it had two roots, each of which was intended to be the sole root.

Objective-C is obviously an OO language, so that single one of many possible counterexamples proves your claim false.

Oh, and also there are class hierarchies wherein the root belongs to itself, rather than to nothing, but you said that's disallowed. But it's absurd to say that something that is otherwise an OO language cannot be called OO just because of that single design choice -- one which has almost no effect whatsoever on most programming done in the language.

It's usually more prudent to avoid absolute claims; for instance, in this case, you might say instead "I think that OO languages are better if they have a singly rooted inheritance hierarchy", and then perhaps people could just debate the pros and cons, rather than doing what I did: finding a single counter-example to disprove an absolute claim -- which is often pretty easy, but doesn't necessarily shed a lot of light on the subject under discussion.

  • This is one of the criteria for OO as given forth by AlanKay. Whether this criteria is a true requirement, or is simply a blatant attempt by Kay to try and exclude C++ (and Java, though Java didn't exist when Kay wrote the paper) from the family of "OO" languages, is an open question. See DefinitionsForOo

Why would the definition at the top of this page exclude Java? All Java classes descend from other classes, except the root class, which is in fact called Object.

OO languages without a  SinglyRootedInheritanceHierarchy:

OO languages with:

OO languages with a singly-rooted inheritance hierarchy for "objects", but which have non-object types in the language that do not derive from the universal root.

Likely many more examples out there.



6.用interfaceBuilder 构建视图 和用代码生成视图,各有什么优缺点?

http://v2ex.com/t/36961

这位博主道出了我的心声

直呼上也搜到了一个问答

http://www.zhihu.com/question/20116289

可以了解一下,竟然是07年写的

What are XIB files?

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
down vote accepted

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.

share | improve this answer



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值