0%

深入剖析 iOS 编译 Clang / LLVM

前言

2000年,伊利诺伊大学厄巴纳-香槟分校(University of Illinois at Urbana-Champaign 简称UIUC)这所享有世界声望的一流公立研究型大学的 Chris Lattner(他的 twitter @clattner_llvm ) 开发了一个叫作 Low Level Virtual Machine 的编译器开发工具套件,后来涉及范围越来越大,可以用于常规编译器,JIT编译器,汇编器,调试器,静态分析工具等一系列跟编程语言相关的工作,于是就把简称 LLVM 这个简称作为了正式的名字。Chris Lattner 后来又开发了 Clang,使得 LLVM 直接挑战 GCC 的地位。2012年,LLVM 获得美国计算机学会 ACM 的软件系统大奖,和 UNIX,WWW,TCP/IP,Tex,JAVA 等齐名。

Chris Lattner 生于 1978 年,2005年加入苹果,将苹果使用的 GCC 全面转为 LLVM。2010年开始主导开发 Swift 语言。

iOS 开发中 Objective-C 是 Clang / LLVM 来编译的。

swift 是 Swift / LLVM,其中 Swift 前端会多出 SIL optimizer,它会把 .swift 生成的中间代码 .sil 属于 High-Level IR, 因为 swift 在编译时就完成了方法绑定直接通过地址调用属于强类型语言,方法调用不再是像OC那样的消息发送,这样编译就可以获得更多的信息用在后面的后端优化上。

LLVM是一个模块化和可重用的编译器和工具链技术的集合,Clang 是 LLVM 的子项目,是 C,C++ 和 Objective-C 编译器,目的是提供惊人的快速编译,比 GCC 快3倍,其中的 clang static analyzer 主要是进行语法分析,语义分析和生成中间代码,当然这个过程会对代码进行检查,出错的和需要警告的会标注出来。LLVM 核心库提供一个优化器,对流行的 CPU 做代码生成支持。lld 是 Clang / LLVM 的内置链接器,clang 必须调用链接器来产生可执行文件。

阅读全文 »

概述

Objc Runtime使得C具有了面向对象能力,在程序运行时创建,检查,修改类、对象和它们的方法。Runtime是C和汇编编写的,这里http://www.opensource.apple.com/source/objc4/可以下到苹果维护的开源代码,GNU也有一个开源的runtime版本,他们都努力的保持一致。苹果官方的Runtime编程指南

Runtime函数

Runtime系统是由一系列的函数和数据结构组成的公共接口动态共享库,在/usr/include/objc目录下可以看到头文件,可以用其中一些函数通过C语言实现Objective-C中一样的功能。苹果官方文档https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/index.html 里有详细的Runtime函数文档。

Class和Object基础数据结构

Class

objc/runtime.h中objc_class结构体的定义如下:

阅读全文 »

Windows10.jpeg

一、概述

###1.安装系统前的准备工作
###2.安装系统的步骤
###3.安装完成后安装驱动和软件

二、具体过程

1.安装系统前的准备工作

需要准备的有:
  • 迅雷软件
    使用迅雷软件下载系统镜像文件。
  • U盘启动盘制作工具
    制作U盘启动盘
  • U盘
    制作成启动盘后进入PE系统装机
  • Windows10系统镜像文件
    需要安装的系统文件
  • 一台可以正常运行的Windows电脑
    制作U盘启动盘
准备阶段需要完成的操作:

1.打开系统镜像下载地址,选择合适的系统镜像进行下载。

阅读全文 »

首先如果遇到应用卡顿或者因为内存占用过多时一般使用Instruments里的来进行检测。但对于复杂情况可能就需要用到子线程监控主线程的方式来了,下面我对这些方法做些介绍:

Time Profiler

可以查看多个线程里那些方法费时过多的方法。先将右侧Hide System Libraries打上勾,这样能够过滤信息。然后在Call Tree上会默认按照费时的线程进行排序,单个线程中会也会按照对应的费时方法排序,选择方法后能够通过右侧Heaviest Stack Trace里双击查看到具体的费时操作代码,从而能够有针对性的优化,而不需要在一些本来就不会怎么影响性能的地方过度优化。

Allocations

这里可以对每个动作的前后进行Generations,对比内存的增加,查看使内存增加的具体的方法和代码所在位置。具体操作是在右侧Generation Analysis里点击Mark Generation,这样会产生一个Generation,切换到其他页面或一段时间产生了另外一个事件时再点Mark Generation来产生一个新的Generation,这样反复,生成多个Generation,查看这几个Generation会看到Growth的大小,如果太大可以点进去查看相应占用较大的线程里右侧Heaviest Stack Trace里查看对应的代码块,然后进行相应的处理。

Leak

阅读全文 »

Layout Objects

使用UICollectionViewFlowLayout类进行具体的布局实现,类的官方文档说明:https://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionViewFlowLayout_class/Reference/Reference.html#//apple_ref/occ/cl/UICollectionViewFlowLayout,进一步定制可参考官方这个文档:https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html#//apple_ref/doc/uid/TP40012334-CH3-SW4

Cells和其它View

collection view管理者cells,supplementary views和decoration views

  • cells:和tableview的cell类似。
  • Supplementary views:相当于tableview的section header和footer views,不同的是数量和位置由布局控制
  • Decoration views:装饰用

基于内容的布局计算自定义布局

子类重写的方法可以参考官方文档:https://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionViewLayout_class/Reference/Reference.html

阅读全文 »

  • before we start, what I have to say ,the post is not an original article. but the learning experience shows that imitation is very important.

let’s start

As we all know ,iOS 11 brings a ton of exciting new features. One of them is the new Files app,which allows users to manage files stored locally and in iCloud. In this post, we learn how your app can take advantage of this cool new feature.

So When You Want to Be a Poet

When you have created a great new app that encourages people to write more poetry. You’ve written several new poems and you’re excited to share them.
poem-collection-3.png

And now, Each poem created within your app is stored inside your app’s Documents folder as a simple text file. You’re excited about the new Files app in iOS 11 and you would like to give your users the ability to manage their poems from within Files. Naturally, you jump into the Files app to start managing your files.

empty-files.png

So there must be something wrong. Your poems do not appear in the Files app. Fortunately, there’s an easy fix.

阅读全文 »

本文深入探讨了 iOS 应用的架构设计,包括工程结构、数据流控制模式(MVC/MVVM/VIPER)的选择,以及代码规范等内容,帮助开发者构建可维护、可扩展的 iOS 应用。

阅读全文 »

  • before we start, what we have to konw is that iPhone X is the future.

iphone-x.png

##Creating apps for iPhone X

iPhone X features an all-screen Super Retina display for more immersive app experiences and Face ID,a secure new way to unlock,authenticate,and pay. The TrueDepth camera works with ARKit,and the A11 Bionic chip is designed for CoreML and Metal2.

###Super Retina display
And now ,let’s try to figure out the Retina display. With iPhone X,the device is the display. An all-new 5.8’’ Super Retina display employs new technologies to precisely follow the curves of the design,clear to the elegantly rounded corners, so your apps look incredible. It supports High Dynamic Range (HDR) with a 1,000,000 to 1 contrast ratio, wide color support, and the best color accuracy in the industry, enabling amazing viewing experiences.

So we have to make sure we are ready to take advantage of the Super Retina display by respecting safe areas, supporting adaptive layouts,and more.

###Run and Test your apps

Testing on a device and trying to find and address UI issues in our app ,and make sure your app looks great on iPhone X.

Enable full screen native resolution. Our app will run in Full Screen Display Mode on iPhone X if your project’s base SDK is set to iOS 11 and you have a Launch Storyboard or iPhone X launch image.

阅读全文 »

简介

简单的说run loop是事件驱动的一个大循环,如下代码所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main(int argc, char * argv[]) {

     //程序一直运行状态

     while (AppIsRunning) {

          //睡眠状态,等待唤醒事件

          id whoWakesMe = SleepForWakingUp();

          //得到唤醒事件

          id event = GetEvent(whoWakesMe);

          //开始处理事件

          HandleEvent(event);

     }

     return 0;

}
阅读全文 »

打算在项目中大面积使用RAC来开发,所以整理一些常用的实践范例和比较完整的api说明方便开发时随时查阅

声明式编程泛型Declarative programming

函数反应式编程是声明式编程的子编程范式之一

高阶函数

需要满足两个条件

  • 一个或者多个函数作为输入。
  • 有且仅有一个函数输出。

Objective-c里使用block作为函数

阅读全文 »