My Octopress Blog

A blogging framework for hackers.

理解 Objective-C Runtime

Objective-C扩展了C语言,并加入了面向对象特性和Smalltalk式的消息传递机制。而这个扩展的核心是一个用C和汇编写的Runtime库。它负责加载类的信息,分发函数消息。从而使得c可以面向对象,变成了Objective-C。 Runtime的核心就是消息传递 (Messaging)

I’m sorry that I long ago coined the term “objects” for this topic because it gets many people to focus on the lesser idea. The big idea is “messaging” – that is what the kernal[sic] of Smalltalk is all about… The key in making great and growable systems is much more to design how its modules communicate rather than what their internal properties and behaviors should be.

– by Alan Kay

Create Universal Framework

xcode很有意思,当选择平台是iOS device时,framework支持的architecture是arm系列的,然而如果是iOS simulator的话,又仅产生支持x86_64系列的architecture。

那么当我们如何编译framework时使它可以支持所有architecture呢

参考资料1

参考资料2

Override Function in Extension


layout: post title: “到底能不能在extension里override一个函数?” date: 2015-07-22 11:12:03 +0800 comments: true

categories: iOS Swift

Swift教程中明确说了,extension并不能override一个已有的函数!可是最近发现有人extension UIImageView时,可以override layoutSubviews(),到底什么鬼??

使用代码来控制AutoLayout

Apple参考文档

创建Constraint

你可以使用NSLayoutConstraint来表示constrains。

如果要创建constraints,一般要使用 constraintsWithVisualFormat:options:metrics:views:

第一个参数是一个visual format string,这种visual format language已经尽可能的是自我解释的。一个view使用一个方括号来代表,view之间的连接用横杠来代表。visual format language的语法参见Visual Format Language

比如说,

上图可以表示为:

[button1]-12-[button2]

如果是标准的Aqua距离的话,可以不用标注数字

[button1]-[button2]

自定义Container View Controllers

多数情况下,container view controller 就像普通的view controller。它管理着view,内容,与其他对象协同工作,并且响应在repsonder chain里的事件。

当你设计一个container的时候,你需要显式的在你的container,和它的子controller之间创建父子关系。如下图。注意,不仅仅controller之间需要显式的指定关系,view之间的关系也需要显式的指定

AIDL in Android

AIDL是用来跨进行通信的。在Android上,跨进程通信需要把对象解构成系统可以识别的基本类型,然后在交给另一个进程使用之前,需要重新组装起来。这些工作是十分枯燥的,AIDL就是为了这个目的而设计的。

注意:当且仅当你允许不同的App的clients需要跨进程来访问你的service,并且需要在service里处理多线程问题的时候,你才应该使用AIDL。如果仅仅是App内部的client来访问服务,并且不需要IPC的话,只要使用Binder就好了。如果是需要IPC,但是不用处理并发的话,那么只要用Messager就好了。只有既要IPC,又要处理并发,才需要用AIDL。总之,只有在必须的时候才使用AIDL